Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 # | 5 # |
| 6 # Usage: | 6 # Usage: |
| 7 # gclient-new-workdir.py <repository> <new_workdir> [<branch>] | 7 # gclient-new-workdir.py <repository> <new_workdir> [<branch>] |
| 8 # | 8 # |
| 9 | 9 |
| 10 import os | 10 import os |
| 11 import shutil | 11 import shutil |
| 12 import subprocess | 12 import subprocess |
| 13 import sys | 13 import sys |
| 14 import textwrap | |
| 15 | |
| 16 | |
| 17 def print_err(msg): | |
| 18 print >> sys.stderr, msg | |
| 14 | 19 |
| 15 | 20 |
| 16 def parse_options(argv): | 21 def parse_options(argv): |
| 17 assert not sys.platform.startswith("win") | 22 if sys.platform == 'win32': |
| 23 print_err( | |
| 24 'This script cannot run on Windows because it uses symlinks.') | |
| 25 sys.exit(1) | |
| 18 | 26 |
| 19 if len(argv) != 3: | 27 if len(argv) != 3: |
| 20 print("usage: gclient-new-workdir.py <repository> <new_workdir>") | 28 usage_msg = '''\ |
| 29 usage: %s <repository> <new_workdir> | |
| 30 | |
| 31 Clone an existing gclient directory, taking care of all sub-repositories | |
| 32 Works similarly to 'git new-workdir'. | |
| 33 | |
| 34 <repository> must be a absolute path | |
| 35 <new_workdir> must not exist | |
| 36 ''' % os.path.basename(argv[0]) | |
| 37 print(textwrap.dedent(usage_msg)) | |
| 21 sys.exit(1) | 38 sys.exit(1) |
|
iannucci
2013/11/13 20:29:10
move this whole if statement body into a usage() f
pgervais
2013/11/13 23:27:54
It depends if execution of usage() is triggered by
| |
| 22 | 39 |
| 23 repository = argv[1] | 40 repository = os.path.abspath(argv[1]) |
| 24 new_workdir = argv[2] | 41 new_workdir = argv[2] |
| 25 | 42 |
| 26 if not os.path.exists(repository): | 43 if not os.path.exists(repository): |
| 27 print("Repository does not exist: " + repository) | 44 print_err('Repository does not exist: ' + repository) |
| 28 sys.exit(1) | 45 sys.exit(1) |
|
iannucci
2013/11/13 20:29:10
then these can become usage(...) calls
| |
| 29 | 46 |
| 30 if os.path.exists(new_workdir): | 47 if os.path.exists(new_workdir): |
| 31 print("New workdir already exists: " + new_workdir) | 48 print_err('New workdir already exists: ' + new_workdir) |
| 32 sys.exit(1) | 49 sys.exit(1) |
| 33 | 50 |
| 34 return repository, new_workdir | 51 return repository, new_workdir |
| 35 | 52 |
| 36 | 53 |
| 37 def main(argv): | 54 def main(argv): |
| 38 repository, new_workdir = parse_options(argv) | 55 repository, new_workdir = parse_options(argv) |
| 39 | 56 |
| 40 gclient = os.path.join(repository, ".gclient") | 57 gclient = os.path.join(repository, '.gclient') |
| 41 if not os.path.exists(gclient): | 58 if not os.path.exists(gclient): |
| 42 print("No .gclient file: " + gclient) | 59 print_err('No .gclient file: ' + gclient) |
| 43 | 60 |
| 44 gclient_entries = os.path.join(repository, ".gclient_entries") | 61 os.makedirs(new_workdir) |
| 45 if not os.path.exists(gclient_entries): | 62 os.symlink(gclient, os.path.join(new_workdir, '.gclient')) |
|
iannucci
2013/11/13 20:29:10
I'm assuming you tried it out and this does, in fa
pgervais
2013/11/13 23:27:54
Yes it does (on gclient sync at least)
| |
| 46 print("No .gclient_entries file: " + gclient_entries) | |
| 47 | |
| 48 os.mkdir(new_workdir) | |
| 49 os.symlink(gclient, os.path.join(new_workdir, ".gclient")) | |
| 50 os.symlink(gclient_entries, os.path.join(new_workdir, ".gclient_entries")) | |
| 51 | 63 |
| 52 for root, dirs, _ in os.walk(repository): | 64 for root, dirs, _ in os.walk(repository): |
| 53 if ".git" in dirs: | 65 if '.git' in dirs: |
| 54 workdir = root.replace(repository, new_workdir, 1) | 66 workdir = root.replace(repository, new_workdir, 1) |
| 55 make_workdir(os.path.join(root, ".git"), | 67 make_workdir(os.path.join(root, '.git'), |
| 56 os.path.join(workdir, ".git")) | 68 os.path.join(workdir, '.git')) |
| 57 | 69 |
| 58 | 70 |
| 59 def make_workdir(repository, new_workdir): | 71 def make_workdir(repository, new_workdir): |
| 60 print("Creating: " + new_workdir) | 72 print('Creating: ' + new_workdir) |
| 61 os.makedirs(new_workdir) | 73 os.makedirs(new_workdir) |
| 62 | 74 |
| 63 GIT_DIRECTORY_WHITELIST = [ | 75 GIT_DIRECTORY_WHITELIST = [ |
| 64 "config", | 76 'config', |
| 65 "info", | 77 'info', |
| 66 "hooks", | 78 'hooks', |
| 67 "logs/refs", | 79 'logs/refs', |
| 68 "objects", | 80 'objects', |
| 69 "packed-refs", | 81 'packed-refs', |
| 70 "refs", | 82 'refs', |
| 71 "remotes", | 83 'remotes', |
| 72 "rr-cache", | 84 'rr-cache', |
| 73 "svn" | 85 'svn' |
| 74 ] | 86 ] |
| 75 | 87 |
| 76 for entry in GIT_DIRECTORY_WHITELIST: | 88 for entry in GIT_DIRECTORY_WHITELIST: |
| 77 make_symlink(repository, new_workdir, entry) | 89 make_symlink(repository, new_workdir, entry) |
| 78 | 90 |
| 79 shutil.copy2(os.path.join(repository, "HEAD"), | 91 shutil.copy2(os.path.join(repository, 'HEAD'), |
| 80 os.path.join(new_workdir, "HEAD")) | 92 os.path.join(new_workdir, 'HEAD')) |
| 81 subprocess.check_call(["git", "checkout", "-f"], | 93 subprocess.check_call(['git', 'checkout', '-f'], |
| 82 cwd=new_workdir.rstrip(".git")) | 94 cwd=new_workdir.rstrip('.git')) |
| 83 | 95 |
| 84 | 96 |
| 85 def make_symlink(repository, new_workdir, link): | 97 def make_symlink(repository, new_workdir, link): |
| 86 if not os.path.exists(os.path.join(repository, link)): | 98 if not os.path.exists(os.path.join(repository, link)): |
| 87 return | 99 return |
| 88 link_dir = os.path.dirname(os.path.join(new_workdir, link)) | 100 link_dir = os.path.dirname(os.path.join(new_workdir, link)) |
| 89 if not os.path.exists(link_dir): | 101 if not os.path.exists(link_dir): |
| 90 os.makedirs(link_dir) | 102 os.makedirs(link_dir) |
| 91 os.symlink(os.path.join(repository, link), os.path.join(new_workdir, link)) | 103 os.symlink(os.path.join(repository, link), os.path.join(new_workdir, link)) |
| 92 | 104 |
| 93 | 105 |
| 94 if __name__ == '__main__': | 106 if __name__ == '__main__': |
| 95 sys.exit(main(sys.argv)) | 107 sys.exit(main(sys.argv)) |
| OLD | NEW |