[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 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 | """ |
| 7 | Tool to perform checkouts in one easy command line! |
| 8 | |
| 9 | Usage: |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 10 | fetch <config> [--property=value [--property2=value2 ...]] |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 11 | |
| 12 | This script is a wrapper around various version control and repository |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 13 | checkout commands. It requires a |config| name, fetches data from that |
| 14 | config in depot_tools/fetch_configs, and then performs all necessary inits, |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 15 | checkouts, pulls, fetches, etc. |
| 16 | |
| 17 | Optional arguments may be passed on the command line in key-value pairs. |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 18 | These parameters will be passed through to the config's main method. |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 19 | """ |
| 20 | |
| 21 | import json |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 22 | import optparse |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 23 | import os |
[email protected] | cc2d3e3 | 2014-08-06 19:47:54 | [diff] [blame] | 24 | import pipes |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 25 | import subprocess |
| 26 | import sys |
[email protected] | cc2d3e3 | 2014-08-06 19:47:54 | [diff] [blame] | 27 | import textwrap |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 28 | |
[email protected] | 6cc97a1 | 2013-04-12 06:15:58 | [diff] [blame] | 29 | from distutils import spawn |
| 30 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 31 | |
| 32 | SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 33 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 34 | ################################################# |
| 35 | # Checkout class definitions. |
| 36 | ################################################# |
| 37 | class Checkout(object): |
| 38 | """Base class for implementing different types of checkouts. |
| 39 | |
| 40 | Attributes: |
| 41 | |base|: the absolute path of the directory in which this script is run. |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 42 | |spec|: the spec for this checkout as returned by the config. Different |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 43 | subclasses will expect different keys in this dictionary. |
| 44 | |root|: the directory into which the checkout will be performed, as returned |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 45 | by the config. This is a relative path from |base|. |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 46 | """ |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 47 | def __init__(self, options, spec, root): |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 48 | self.base = os.getcwd() |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 49 | self.options = options |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 50 | self.spec = spec |
| 51 | self.root = root |
| 52 | |
| 53 | def exists(self): |
| 54 | pass |
| 55 | |
| 56 | def init(self): |
| 57 | pass |
| 58 | |
| 59 | def sync(self): |
| 60 | pass |
| 61 | |
[email protected] | 6cc97a1 | 2013-04-12 06:15:58 | [diff] [blame] | 62 | def run(self, cmd, **kwargs): |
| 63 | print 'Running: %s' % (' '.join(pipes.quote(x) for x in cmd)) |
[email protected] | 38e9461 | 2014-02-12 22:19:41 | [diff] [blame] | 64 | if self.options.dry_run: |
[email protected] | 294c783 | 2015-06-17 16:16:32 | [diff] [blame] | 65 | return '' |
[email protected] | 5a44776 | 2015-06-10 20:01:39 | [diff] [blame] | 66 | return subprocess.check_output(cmd, **kwargs) |
[email protected] | 6cc97a1 | 2013-04-12 06:15:58 | [diff] [blame] | 67 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 68 | |
| 69 | class GclientCheckout(Checkout): |
| 70 | |
[email protected] | d88d7f5 | 2013-04-03 21:09:07 | [diff] [blame] | 71 | def run_gclient(self, *cmd, **kwargs): |
[email protected] | 6cc97a1 | 2013-04-12 06:15:58 | [diff] [blame] | 72 | if not spawn.find_executable('gclient'): |
| 73 | cmd_prefix = (sys.executable, os.path.join(SCRIPT_PATH, 'gclient.py')) |
| 74 | else: |
| 75 | cmd_prefix = ('gclient',) |
| 76 | return self.run(cmd_prefix + cmd, **kwargs) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 77 | |
[email protected] | 5a44776 | 2015-06-10 20:01:39 | [diff] [blame] | 78 | def exists(self): |
| 79 | try: |
| 80 | gclient_root = self.run_gclient('root').strip() |
| 81 | return (os.path.exists(os.path.join(gclient_root, '.gclient')) or |
| 82 | os.path.exists(os.path.join(os.getcwd(), self.root))) |
| 83 | except subprocess.CalledProcessError: |
| 84 | pass |
| 85 | return os.path.exists(os.path.join(os.getcwd(), self.root)) |
| 86 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 87 | |
| 88 | class GitCheckout(Checkout): |
| 89 | |
[email protected] | d88d7f5 | 2013-04-03 21:09:07 | [diff] [blame] | 90 | def run_git(self, *cmd, **kwargs): |
[email protected] | 6cc97a1 | 2013-04-12 06:15:58 | [diff] [blame] | 91 | if sys.platform == 'win32' and not spawn.find_executable('git'): |
[email protected] | cc2b6a1 | 2014-02-20 17:42:59 | [diff] [blame] | 92 | git_path = os.path.join(SCRIPT_PATH, 'git.bat') |
[email protected] | 6cc97a1 | 2013-04-12 06:15:58 | [diff] [blame] | 93 | else: |
| 94 | git_path = 'git' |
| 95 | return self.run((git_path,) + cmd, **kwargs) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 96 | |
| 97 | |
[email protected] | 2560ea7 | 2013-04-04 01:22:38 | [diff] [blame] | 98 | class SvnCheckout(Checkout): |
| 99 | |
| 100 | def run_svn(self, *cmd, **kwargs): |
[email protected] | 6cc97a1 | 2013-04-12 06:15:58 | [diff] [blame] | 101 | if sys.platform == 'win32' and not spawn.find_executable('svn'): |
| 102 | svn_path = os.path.join(SCRIPT_PATH, 'svn_bin', 'svn.exe') |
| 103 | else: |
| 104 | svn_path = 'svn' |
| 105 | return self.run((svn_path,) + cmd, **kwargs) |
[email protected] | 2560ea7 | 2013-04-04 01:22:38 | [diff] [blame] | 106 | |
| 107 | |
[email protected] | d993e78 | 2013-04-11 20:03:13 | [diff] [blame] | 108 | class GclientGitCheckout(GclientCheckout, GitCheckout): |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 109 | |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 110 | def __init__(self, options, spec, root): |
| 111 | super(GclientGitCheckout, self).__init__(options, spec, root) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 112 | assert 'solutions' in self.spec |
[email protected] | 5bde64e | 2014-11-25 22:15:26 | [diff] [blame] | 113 | |
| 114 | def _format_spec(self): |
| 115 | def _format_literal(lit): |
| 116 | if isinstance(lit, basestring): |
| 117 | return '"%s"' % lit |
| 118 | if isinstance(lit, list): |
| 119 | return '[%s]' % ', '.join(_format_literal(i) for i in lit) |
| 120 | return '%r' % lit |
| 121 | soln_strings = [] |
| 122 | for soln in self.spec['solutions']: |
| 123 | soln_string= '\n'.join(' "%s": %s,' % (key, _format_literal(value)) |
| 124 | for key, value in soln.iteritems()) |
| 125 | soln_strings.append(' {\n%s\n },' % soln_string) |
| 126 | gclient_spec = 'solutions = [\n%s\n]\n' % '\n'.join(soln_strings) |
| 127 | extra_keys = ['target_os', 'target_os_only'] |
| 128 | gclient_spec += ''.join('%s = %s\n' % (key, _format_literal(self.spec[key])) |
| 129 | for key in extra_keys if key in self.spec) |
| 130 | return gclient_spec |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 131 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 132 | def init(self): |
| 133 | # Configure and do the gclient checkout. |
[email protected] | 5bde64e | 2014-11-25 22:15:26 | [diff] [blame] | 134 | self.run_gclient('config', '--spec', self._format_spec()) |
[email protected] | 048da08 | 2014-05-06 08:32:40 | [diff] [blame] | 135 | sync_cmd = ['sync'] |
[email protected] | b98f3f2 | 2015-06-15 21:59:09 | [diff] [blame] | 136 | if self.options.nohooks: |
[email protected] | 048da08 | 2014-05-06 08:32:40 | [diff] [blame] | 137 | sync_cmd.append('--nohooks') |
[email protected] | 5439ea5 | 2014-08-06 17:18:18 | [diff] [blame] | 138 | if self.options.no_history: |
| 139 | sync_cmd.append('--no-history') |
[email protected] | 048da08 | 2014-05-06 08:32:40 | [diff] [blame] | 140 | if self.spec.get('with_branch_heads', False): |
| 141 | sync_cmd.append('--with_branch_heads') |
| 142 | self.run_gclient(*sync_cmd) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 143 | |
| 144 | # Configure git. |
| 145 | wd = os.path.join(self.base, self.root) |
[email protected] | 38e9461 | 2014-02-12 22:19:41 | [diff] [blame] | 146 | if self.options.dry_run: |
[email protected] | 2560ea7 | 2013-04-04 01:22:38 | [diff] [blame] | 147 | print 'cd %s' % wd |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 148 | self.run_git( |
| 149 | 'submodule', 'foreach', |
| 150 | 'git config -f $toplevel/.git/config submodule.$name.ignore all', |
| 151 | cwd=wd) |
[email protected] | f2fb5e7 | 2014-04-03 02:36:44 | [diff] [blame] | 152 | self.run_git( |
| 153 | 'config', '--add', 'remote.origin.fetch', |
| 154 | '+refs/tags/*:refs/tags/*', cwd=wd) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 155 | self.run_git('config', 'diff.ignoreSubmodules', 'all', cwd=wd) |
| 156 | |
[email protected] | d993e78 | 2013-04-11 20:03:13 | [diff] [blame] | 157 | |
| 158 | class GclientGitSvnCheckout(GclientGitCheckout, SvnCheckout): |
| 159 | |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 160 | def __init__(self, options, spec, root): |
| 161 | super(GclientGitSvnCheckout, self).__init__(options, spec, root) |
[email protected] | d993e78 | 2013-04-11 20:03:13 | [diff] [blame] | 162 | |
| 163 | def init(self): |
| 164 | # Ensure we are authenticated with subversion for all submodules. |
| 165 | git_svn_dirs = json.loads(self.spec.get('submodule_git_svn_spec', '{}')) |
| 166 | git_svn_dirs.update({self.root: self.spec}) |
| 167 | for _, svn_spec in git_svn_dirs.iteritems(): |
[email protected] | 14f633b | 2014-10-22 10:35:33 | [diff] [blame] | 168 | if svn_spec.get('svn_url'): |
| 169 | try: |
| 170 | self.run_svn('ls', '--non-interactive', svn_spec['svn_url']) |
| 171 | except subprocess.CalledProcessError: |
| 172 | print 'Please run `svn ls %s`' % svn_spec['svn_url'] |
| 173 | return 1 |
[email protected] | d993e78 | 2013-04-11 20:03:13 | [diff] [blame] | 174 | |
| 175 | super(GclientGitSvnCheckout, self).init() |
| 176 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 177 | # Configure git-svn. |
[email protected] | 2560ea7 | 2013-04-04 01:22:38 | [diff] [blame] | 178 | for path, svn_spec in git_svn_dirs.iteritems(): |
| 179 | real_path = os.path.join(*path.split('/')) |
| 180 | if real_path != self.root: |
| 181 | real_path = os.path.join(self.root, real_path) |
| 182 | wd = os.path.join(self.base, real_path) |
[email protected] | 38e9461 | 2014-02-12 22:19:41 | [diff] [blame] | 183 | if self.options.dry_run: |
[email protected] | 2560ea7 | 2013-04-04 01:22:38 | [diff] [blame] | 184 | print 'cd %s' % wd |
[email protected] | 14f633b | 2014-10-22 10:35:33 | [diff] [blame] | 185 | if svn_spec.get('auto'): |
| 186 | self.run_git('auto-svn', cwd=wd) |
| 187 | continue |
| 188 | self.run_git('svn', 'init', svn_spec['svn_url'], cwd=wd) |
| 189 | self.run_git('config', '--unset-all', 'svn-remote.svn.fetch', cwd=wd) |
| 190 | for svn_branch, git_ref in svn_spec.get('git_svn_fetch', {}).items(): |
| 191 | self.run_git('config', '--add', 'svn-remote.svn.fetch', |
| 192 | '%s:%s' % (svn_branch, git_ref), cwd=wd) |
| 193 | for svn_branch, git_ref in svn_spec.get('git_svn_branches', {}).items(): |
| 194 | self.run_git('config', '--add', 'svn-remote.svn.branches', |
| 195 | '%s:%s' % (svn_branch, git_ref), cwd=wd) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 196 | self.run_git('svn', 'fetch', cwd=wd) |
| 197 | |
| 198 | |
[email protected] | 2560ea7 | 2013-04-04 01:22:38 | [diff] [blame] | 199 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 200 | CHECKOUT_TYPE_MAP = { |
| 201 | 'gclient': GclientCheckout, |
[email protected] | d993e78 | 2013-04-11 20:03:13 | [diff] [blame] | 202 | 'gclient_git': GclientGitCheckout, |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 203 | 'gclient_git_svn': GclientGitSvnCheckout, |
| 204 | 'git': GitCheckout, |
| 205 | } |
| 206 | |
| 207 | |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 208 | def CheckoutFactory(type_name, options, spec, root): |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 209 | """Factory to build Checkout class instances.""" |
| 210 | class_ = CHECKOUT_TYPE_MAP.get(type_name) |
| 211 | if not class_: |
| 212 | raise KeyError('unrecognized checkout type: %s' % type_name) |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 213 | return class_(options, spec, root) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 214 | |
| 215 | |
| 216 | ################################################# |
| 217 | # Utility function and file entry point. |
| 218 | ################################################# |
| 219 | def usage(msg=None): |
| 220 | """Print help and exit.""" |
| 221 | if msg: |
| 222 | print 'Error:', msg |
| 223 | |
[email protected] | cc2d3e3 | 2014-08-06 19:47:54 | [diff] [blame] | 224 | print textwrap.dedent("""\ |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 225 | usage: %s [options] <config> [--property=value [--property2=value2 ...]] |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 226 | |
[email protected] | cc2d3e3 | 2014-08-06 19:47:54 | [diff] [blame] | 227 | This script can be used to download the Chromium sources. See |
| 228 | http://www.chromium.org/developers/how-tos/get-the-code |
| 229 | for full usage instructions. |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 230 | |
[email protected] | cc2d3e3 | 2014-08-06 19:47:54 | [diff] [blame] | 231 | Valid options: |
| 232 | -h, --help, help Print this message. |
| 233 | --nohooks Don't run hooks after checkout. |
| 234 | -n, --dry-run Don't run commands, only print them. |
| 235 | --no-history Perform shallow clones, don't fetch the full git history. |
| 236 | |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 237 | Valid fetch configs:""") % os.path.basename(sys.argv[0]) |
[email protected] | 37103c9 | 2015-09-19 20:54:39 | [diff] [blame] | 238 | |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 239 | configs_dir = os.path.join(SCRIPT_PATH, 'fetch_configs') |
| 240 | configs = [f[:-3] for f in os.listdir(configs_dir) if f.endswith('.py')] |
| 241 | configs.sort() |
| 242 | for fname in configs: |
[email protected] | 37103c9 | 2015-09-19 20:54:39 | [diff] [blame] | 243 | print ' ' + fname |
[email protected] | cc2d3e3 | 2014-08-06 19:47:54 | [diff] [blame] | 244 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 245 | sys.exit(bool(msg)) |
| 246 | |
| 247 | |
| 248 | def handle_args(argv): |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 249 | """Gets the config name from the command line arguments.""" |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 250 | if len(argv) <= 1: |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 251 | usage('Must specify a config.') |
[email protected] | e3d147d | 2013-04-03 20:31:27 | [diff] [blame] | 252 | if argv[1] in ('-h', '--help', 'help'): |
| 253 | usage() |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 254 | |
[email protected] | 38e9461 | 2014-02-12 22:19:41 | [diff] [blame] | 255 | dry_run = False |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 256 | nohooks = False |
[email protected] | 5439ea5 | 2014-08-06 17:18:18 | [diff] [blame] | 257 | no_history = False |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 258 | while len(argv) >= 2: |
| 259 | arg = argv[1] |
| 260 | if not arg.startswith('-'): |
| 261 | break |
[email protected] | d88d7f5 | 2013-04-03 21:09:07 | [diff] [blame] | 262 | argv.pop(1) |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 263 | if arg in ('-n', '--dry-run'): |
[email protected] | 38e9461 | 2014-02-12 22:19:41 | [diff] [blame] | 264 | dry_run = True |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 265 | elif arg == '--nohooks': |
| 266 | nohooks = True |
[email protected] | 5439ea5 | 2014-08-06 17:18:18 | [diff] [blame] | 267 | elif arg == '--no-history': |
| 268 | no_history = True |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 269 | else: |
| 270 | usage('Invalid option %s.' % arg) |
[email protected] | d88d7f5 | 2013-04-03 21:09:07 | [diff] [blame] | 271 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 272 | def looks_like_arg(arg): |
| 273 | return arg.startswith('--') and arg.count('=') == 1 |
| 274 | |
| 275 | bad_parms = [x for x in argv[2:] if not looks_like_arg(x)] |
| 276 | if bad_parms: |
| 277 | usage('Got bad arguments %s' % bad_parms) |
| 278 | |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 279 | config = argv[1] |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 280 | props = argv[2:] |
[email protected] | 5439ea5 | 2014-08-06 17:18:18 | [diff] [blame] | 281 | return ( |
| 282 | optparse.Values( |
| 283 | {'dry_run':dry_run, 'nohooks':nohooks, 'no_history': no_history }), |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 284 | config, |
[email protected] | 5439ea5 | 2014-08-06 17:18:18 | [diff] [blame] | 285 | props) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 286 | |
| 287 | |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 288 | def run_config_fetch(config, props, aliased=False): |
| 289 | """Invoke a config's fetch method with the passed-through args |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 290 | and return its json output as a python object.""" |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 291 | config_path = os.path.abspath( |
| 292 | os.path.join(SCRIPT_PATH, 'fetch_configs', config)) |
| 293 | if not os.path.exists(config_path + '.py'): |
| 294 | print "Could not find a config for %s" % config |
[email protected] | 2bf328a | 2013-04-03 21:14:41 | [diff] [blame] | 295 | sys.exit(1) |
| 296 | |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 297 | cmd = [sys.executable, config_path + '.py', 'fetch'] + props |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 298 | result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
[email protected] | 2bf328a | 2013-04-03 21:14:41 | [diff] [blame] | 299 | |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 300 | spec = json.loads(result) |
| 301 | if 'alias' in spec: |
| 302 | assert not aliased |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 303 | return run_config_fetch( |
| 304 | spec['alias']['config'], spec['alias']['props'] + props, aliased=True) |
| 305 | cmd = [sys.executable, config_path + '.py', 'root'] |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 306 | result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 307 | root = json.loads(result) |
| 308 | return spec, root |
| 309 | |
| 310 | |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 311 | def run(options, spec, root): |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 312 | """Perform a checkout with the given type and configuration. |
| 313 | |
| 314 | Args: |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 315 | options: Options instance. |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 316 | spec: Checkout configuration returned by the the config's fetch_spec |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 317 | method (checkout type, repository url, etc.). |
| 318 | root: The directory into which the repo expects to be checkout out. |
| 319 | """ |
| 320 | assert 'type' in spec |
| 321 | checkout_type = spec['type'] |
| 322 | checkout_spec = spec['%s_spec' % checkout_type] |
| 323 | try: |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 324 | checkout = CheckoutFactory(checkout_type, options, checkout_spec, root) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 325 | except KeyError: |
| 326 | return 1 |
| 327 | if checkout.exists(): |
[email protected] | 5a44776 | 2015-06-10 20:01:39 | [diff] [blame] | 328 | print 'Your current directory appears to already contain, or be part of, ' |
| 329 | print 'a checkout. "fetch" is used only to get new checkouts. Use ' |
| 330 | print '"gclient sync" to update existing checkouts.' |
[email protected] | fd79e0d | 2013-04-12 21:34:32 | [diff] [blame] | 331 | print |
| 332 | print 'Fetch also does not yet deal with partial checkouts, so if fetch' |
| 333 | print 'failed, delete the checkout and start over (crbug.com/230691).' |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 334 | return 1 |
[email protected] | 2560ea7 | 2013-04-04 01:22:38 | [diff] [blame] | 335 | return checkout.init() |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 336 | |
| 337 | |
| 338 | def main(): |
[email protected] | b371a1c | 2015-12-04 01:42:48 | [diff] [blame] | 339 | options, config, props = handle_args(sys.argv) |
| 340 | spec, root = run_config_fetch(config, props) |
[email protected] | 3596d58 | 2013-12-13 17:07:33 | [diff] [blame] | 341 | return run(options, spec, root) |
[email protected] | cc02350 | 2013-04-03 20:24:21 | [diff] [blame] | 342 | |
| 343 | |
| 344 | if __name__ == '__main__': |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 345 | try: |
| 346 | sys.exit(main()) |
| 347 | except KeyboardInterrupt: |
| 348 | sys.stderr.write('interrupted\n') |
| 349 | sys.exit(1) |