Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 # | |
| 6 # Usage: | |
| 7 # gclient-new-workdir.py <repository> <new_workdir> [<branch>] | |
| 8 # | |
| 9 | |
| 10 import os | |
| 11 import shutil | |
| 12 import subprocess | |
| 13 import sys | |
| 14 | |
| 15 def parse_options(argv): | |
|
iannucci
2013/11/04 22:30:43
style nit: 2 lines between top-level statements
| |
| 16 if len(argv) != 3: | |
| 17 usage() | |
| 18 return False, "", "" | |
|
iannucci
2013/11/04 22:30:43
I think sys.exit(1) is clearer, instead of passing
| |
| 19 | |
| 20 success = True | |
| 21 repository = argv[1] | |
| 22 new_workdir = argv[2] | |
| 23 | |
| 24 if not os.path.exists(repository): | |
| 25 print("Repository does not exist: " + repository) | |
| 26 success = False | |
| 27 | |
| 28 if os.path.exists(new_workdir): | |
| 29 print("New workdir already exists: " + new_workdir) | |
| 30 success = False | |
| 31 | |
| 32 return success, repository, new_workdir | |
| 33 | |
| 34 def main(argv): | |
| 35 assert not sys.platform.startswith("win") | |
|
iannucci
2013/11/04 22:30:43
nit: I would move this into parse_options, but up
| |
| 36 | |
| 37 success, repository, new_workdir = parse_options(argv) | |
| 38 if not success: | |
| 39 return 1 | |
| 40 | |
| 41 gclient = os.path.join(repository, ".gclient") | |
| 42 if not os.path.exists(gclient): | |
| 43 print("No .gclient file: " + gclient) | |
| 44 | |
| 45 gclient_entries = os.path.join(repository, ".gclient_entries") | |
| 46 if not os.path.exists(gclient_entries): | |
| 47 print("No .gclient_entries file: " + gclient_entries) | |
| 48 | |
| 49 os.mkdir(new_workdir) | |
| 50 os.symlink(gclient, os.path.join(new_workdir, ".gclient")) | |
| 51 os.symlink(gclient_entries, os.path.join(new_workdir, ".gclient_entries")) | |
| 52 | |
| 53 for root, dirs, files in os.walk(repository): | |
| 54 for name in dirs: | |
| 55 if name == ".git": | |
|
iannucci
2013/11/04 22:30:43
Why not just:
if '.git' in dirs:
# do stuff wit
| |
| 56 workdir = root.replace(repository, new_workdir, 1) | |
| 57 make_workdir(os.path.join(root, name), os.path.join(workdir, name)) | |
|
iannucci
2013/11/04 22:30:43
What happens if something breaks half-way through?
atreat
2013/11/04 22:51:35
We aren't touching the original workdir. The only
| |
| 58 | |
| 59 return 0 | |
| 60 | |
| 61 def make_workdir(repository, new_workdir): | |
| 62 print("Creating: " + new_workdir) | |
| 63 os.makedirs(new_workdir) | |
| 64 make_symlink(repository, new_workdir, "config") | |
| 65 make_symlink(repository, new_workdir, "refs") | |
| 66 if os.path.exists(repository + os.sep + "logs"): | |
| 67 os.makedirs(new_workdir + os.sep + "logs") | |
|
iannucci
2013/11/04 22:30:43
os.path.join
| |
| 68 make_symlink(repository, new_workdir, "logs" + os.sep + "refs") | |
| 69 make_symlink(repository, new_workdir, "objects") | |
| 70 make_symlink(repository, new_workdir, "info") | |
| 71 make_symlink(repository, new_workdir, "hooks") | |
| 72 make_symlink(repository, new_workdir, "packed-refs") | |
| 73 make_symlink(repository, new_workdir, "remotes") | |
| 74 make_symlink(repository, new_workdir, "rr-cache") | |
| 75 make_symlink(repository, new_workdir, "svn") | |
|
iannucci
2013/11/04 22:30:43
I wonder if a blacklist would be more correct than
atreat
2013/11/04 22:51:35
The whitelist was explicitly taken from git-new-wo
atreat
2013/11/04 22:51:35
Hmm, I am not sure that a list would make it any m
iannucci
2013/11/04 22:54:51
I'm thinking like:
GIT_DIRECTORY_WHITELIST = ['co
| |
| 76 shutil.copy2(repository + os.sep + "HEAD", new_workdir + os.sep + "HEAD") | |
|
iannucci
2013/11/04 22:30:43
os.path.join
| |
| 77 subprocess.check_call(["git", "checkout", "-f"], cwd=new_workdir.rstrip(".git" )) | |
| 78 | |
| 79 def make_symlink(repository, new_workdir, link): | |
| 80 if not os.path.exists(os.path.join(repository, link)): | |
| 81 return | |
| 82 os.symlink(os.path.join(repository, link), os.path.join(new_workdir, link)) | |
| 83 | |
| 84 def usage(): | |
| 85 print("usage: gclient-new-workdir.py <repository> <new_workdir>") | |
|
iannucci
2013/11/04 22:30:43
Maybe since this is used in exactly 1 place, just
| |
| 86 | |
| 87 if __name__ == '__main__': | |
| 88 sys.exit(main(sys.argv)) | |
| OLD | NEW |