| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 """ | 6 """ |
| 7 Tool to perform checkouts in one easy command line! | 7 Tool to perform checkouts in one easy command line! |
| 8 | 8 |
| 9 Usage: | 9 Usage: |
| 10 fetch <recipe> [--property=value [--property2=value2 ...]] | 10 fetch <recipe> [--property=value [--property2=value2 ...]] |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 | 69 |
| 70 class GitCheckout(Checkout): | 70 class GitCheckout(Checkout): |
| 71 | 71 |
| 72 def run_git(self, *cmd, **kwargs): | 72 def run_git(self, *cmd, **kwargs): |
| 73 print 'Running: git %s' % ' '.join(pipes.quote(x) for x in cmd) | 73 print 'Running: git %s' % ' '.join(pipes.quote(x) for x in cmd) |
| 74 if not self.dryrun: | 74 if not self.dryrun: |
| 75 return subprocess.check_call(('git',) + cmd, **kwargs) | 75 return subprocess.check_call(('git',) + cmd, **kwargs) |
| 76 | 76 |
| 77 | 77 |
| 78 class GclientGitSvnCheckout(GclientCheckout, GitCheckout): | 78 class SvnCheckout(Checkout): |
| 79 |
| 80 def run_svn(self, *cmd, **kwargs): |
| 81 print 'Running: svn %s' % ' '.join(pipes.quote(x) for x in cmd) |
| 82 if not self.dryrun: |
| 83 return subprocess.check_call(('svn',) + cmd, **kwargs) |
| 84 |
| 85 |
| 86 class GclientGitSvnCheckout(GclientCheckout, GitCheckout, SvnCheckout): |
| 79 | 87 |
| 80 def __init__(self, dryrun, spec, root): | 88 def __init__(self, dryrun, spec, root): |
| 81 super(GclientGitSvnCheckout, self).__init__(dryrun, spec, root) | 89 super(GclientGitSvnCheckout, self).__init__(dryrun, spec, root) |
| 82 assert 'solutions' in self.spec | 90 assert 'solutions' in self.spec |
| 83 keys = ['solutions', 'target_os', 'target_os_only'] | 91 keys = ['solutions', 'target_os', 'target_os_only'] |
| 84 gclient_spec = '\n'.join('%s = %s' % (key, self.spec[key]) | 92 gclient_spec = '\n'.join('%s = %s' % (key, self.spec[key]) |
| 85 for key in self.spec if key in keys) | 93 for key in self.spec if key in keys) |
| 86 self.spec['gclient_spec'] = gclient_spec | 94 self.spec['gclient_spec'] = gclient_spec |
| 87 assert 'svn_url' in self.spec | 95 assert 'svn_url' in self.spec |
| 88 assert 'svn_branch' in self.spec | 96 assert 'svn_branch' in self.spec |
| 89 assert 'svn_ref' in self.spec | 97 assert 'svn_ref' in self.spec |
| 90 | 98 |
| 91 def exists(self): | 99 def exists(self): |
| 92 return os.path.exists(os.path.join(os.getcwd(), self.root)) | 100 return os.path.exists(os.path.join(os.getcwd(), self.root)) |
| 93 | 101 |
| 94 def init(self): | 102 def init(self): |
| 103 # Ensure we are authenticated with subversion for all submodules. |
| 104 git_svn_dirs = json.loads(self.spec.get('submodule_git_svn_spec', '{}')) |
| 105 git_svn_dirs.update({self.root: self.spec}) |
| 106 for _, svn_spec in git_svn_dirs.iteritems(): |
| 107 try: |
| 108 self.run_svn('ls', '--non-interactive', svn_spec['svn_url']) |
| 109 except subprocess.CalledProcessError: |
| 110 print 'Please run `svn ls %s`' % svn_spec['svn_url'] |
| 111 return 1 |
| 112 |
| 95 # Configure and do the gclient checkout. | 113 # Configure and do the gclient checkout. |
| 96 self.run_gclient('config', '--spec', self.spec['gclient_spec']) | 114 self.run_gclient('config', '--spec', self.spec['gclient_spec']) |
| 97 self.run_gclient('sync') | 115 self.run_gclient('sync') |
| 98 | 116 |
| 99 # Configure git. | 117 # Configure git. |
| 100 wd = os.path.join(self.base, self.root) | 118 wd = os.path.join(self.base, self.root) |
| 101 if self.dryrun: | 119 if self.dryrun: |
| 102 print "cd %s" % wd | 120 print 'cd %s' % wd |
| 103 self.run_git( | 121 self.run_git( |
| 104 'submodule', 'foreach', | 122 'submodule', 'foreach', |
| 105 'git config -f $toplevel/.git/config submodule.$name.ignore all', | 123 'git config -f $toplevel/.git/config submodule.$name.ignore all', |
| 106 cwd=wd) | 124 cwd=wd) |
| 107 self.run_git('config', 'diff.ignoreSubmodules', 'all', cwd=wd) | 125 self.run_git('config', 'diff.ignoreSubmodules', 'all', cwd=wd) |
| 108 | 126 |
| 109 # Configure git-svn. | 127 # Configure git-svn. |
| 110 self.run_git('svn', 'init', '--prefix=origin/', '-T', | 128 for path, svn_spec in git_svn_dirs.iteritems(): |
| 111 self.spec['svn_branch'], self.spec['svn_url'], cwd=wd) | 129 real_path = os.path.join(*path.split('/')) |
| 112 self.run_git('config', 'svn-remote.svn.fetch', self.spec['svn_branch'] + | 130 if real_path != self.root: |
| 113 ':refs/remotes/origin/' + self.spec['svn_ref'], cwd=wd) | 131 real_path = os.path.join(self.root, real_path) |
| 114 self.run_git('svn', 'fetch', cwd=wd) | 132 wd = os.path.join(self.base, real_path) |
| 133 if self.dryrun: |
| 134 print 'cd %s' % wd |
| 135 self.run_git('svn', 'init', '--prefix=origin/', '-T', |
| 136 svn_spec['svn_branch'], svn_spec['svn_url'], cwd=wd) |
| 137 self.run_git('config', '--replace', 'svn-remote.svn.fetch', |
| 138 svn_spec['svn_branch'] + ':refs/remotes/origin/' + |
| 139 svn_spec['svn_ref'], cwd=wd) |
| 140 self.run_git('svn', 'fetch', cwd=wd) |
| 115 | 141 |
| 116 # Configure git-svn submodules, if any. | |
| 117 submodules = json.loads(self.spec.get('submodule_git_svn_spec', '{}')) | |
| 118 for path, subspec in submodules.iteritems(): | |
| 119 subspec = submodules[path] | |
| 120 ospath = os.path.join(*path.split('/')) | |
| 121 wd = os.path.join(self.base, self.root, ospath) | |
| 122 if self.dryrun: | |
| 123 print "cd %s" % wd | |
| 124 self.run_git('svn', 'init', '--prefix=origin/', '-T', | |
| 125 subspec['svn_branch'], subspec['svn_url'], cwd=wd) | |
| 126 self.run_git('config', '--replace', 'svn-remote.svn.fetch', | |
| 127 subspec['svn_branch'] + ':refs/remotes/origin/' + | |
| 128 subspec['svn_ref'], cwd=wd) | |
| 129 self.run_git('svn', 'fetch', cwd=wd) | |
| 130 | 142 |
| 131 | 143 |
| 132 CHECKOUT_TYPE_MAP = { | 144 CHECKOUT_TYPE_MAP = { |
| 133 'gclient': GclientCheckout, | 145 'gclient': GclientCheckout, |
| 134 'gclient_git_svn': GclientGitSvnCheckout, | 146 'gclient_git_svn': GclientGitSvnCheckout, |
| 135 'git': GitCheckout, | 147 'git': GitCheckout, |
| 136 } | 148 } |
| 137 | 149 |
| 138 | 150 |
| 139 def CheckoutFactory(type_name, dryrun, spec, root): | 151 def CheckoutFactory(type_name, dryrun, spec, root): |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 checkout_type = spec['type'] | 230 checkout_type = spec['type'] |
| 219 checkout_spec = spec['%s_spec' % checkout_type] | 231 checkout_spec = spec['%s_spec' % checkout_type] |
| 220 try: | 232 try: |
| 221 checkout = CheckoutFactory(checkout_type, dryrun, checkout_spec, root) | 233 checkout = CheckoutFactory(checkout_type, dryrun, checkout_spec, root) |
| 222 except KeyError: | 234 except KeyError: |
| 223 return 1 | 235 return 1 |
| 224 if checkout.exists(): | 236 if checkout.exists(): |
| 225 print 'You appear to already have this checkout.' | 237 print 'You appear to already have this checkout.' |
| 226 print 'Aborting to avoid clobbering your work.' | 238 print 'Aborting to avoid clobbering your work.' |
| 227 return 1 | 239 return 1 |
| 228 checkout.init() | 240 return checkout.init() |
| 229 return 0 | |
| 230 | 241 |
| 231 | 242 |
| 232 def main(): | 243 def main(): |
| 233 dryrun, recipe, props = handle_args(sys.argv) | 244 dryrun, recipe, props = handle_args(sys.argv) |
| 234 spec, root = run_recipe_fetch(recipe, props) | 245 spec, root = run_recipe_fetch(recipe, props) |
| 235 return run(dryrun, spec, root) | 246 return run(dryrun, spec, root) |
| 236 | 247 |
| 237 | 248 |
| 238 if __name__ == '__main__': | 249 if __name__ == '__main__': |
| 239 sys.exit(main()) | 250 sys.exit(main()) |
| OLD | NEW |