Edward Lemur | 5c86919 | 2019-10-11 20:08:17 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 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: |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 10 | fetch <config> [--property=value [--property2=value2 ...]] |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 11 | |
| 12 | This script is a wrapper around various version control and repository |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [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, |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 15 | checkouts, pulls, fetches, etc. |
| 16 | |
| 17 | Optional arguments may be passed on the command line in key-value pairs. |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 18 | These parameters will be passed through to the config's main method. |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 19 | """ |
| 20 | |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 21 | from __future__ import print_function |
| 22 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 23 | import json |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 24 | import optparse |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 25 | import os |
iannucci@chromium.org | cc2d3e3 | 2014-08-06 19:47:54 +0000 | [diff] [blame] | 26 | import pipes |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 27 | import subprocess |
| 28 | import sys |
iannucci@chromium.org | cc2d3e3 | 2014-08-06 19:47:54 +0000 | [diff] [blame] | 29 | import textwrap |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 30 | |
Dan Jacques | 209a681 | 2017-07-12 11:40:20 -0700 | [diff] [blame] | 31 | import git_common |
| 32 | |
dpranke@chromium.org | 6cc97a1 | 2013-04-12 06:15:58 +0000 | [diff] [blame] | 33 | from distutils import spawn |
| 34 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 35 | |
| 36 | SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 37 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 38 | ################################################# |
| 39 | # Checkout class definitions. |
| 40 | ################################################# |
| 41 | class Checkout(object): |
| 42 | """Base class for implementing different types of checkouts. |
| 43 | |
| 44 | Attributes: |
| 45 | |base|: the absolute path of the directory in which this script is run. |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 46 | |spec|: the spec for this checkout as returned by the config. Different |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 47 | subclasses will expect different keys in this dictionary. |
| 48 | |root|: the directory into which the checkout will be performed, as returned |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 49 | by the config. This is a relative path from |base|. |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 50 | """ |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 51 | def __init__(self, options, spec, root): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 52 | self.base = os.getcwd() |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 53 | self.options = options |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 54 | self.spec = spec |
| 55 | self.root = root |
| 56 | |
| 57 | def exists(self): |
| 58 | pass |
| 59 | |
| 60 | def init(self): |
| 61 | pass |
| 62 | |
| 63 | def sync(self): |
| 64 | pass |
| 65 | |
Edward Lemur | 7a4ced2 | 2018-01-26 16:26:05 +0100 | [diff] [blame] | 66 | def run(self, cmd, return_stdout=False, **kwargs): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 67 | print('Running: %s' % (' '.join(pipes.quote(x) for x in cmd))) |
wtc@chromium.org | 38e9461 | 2014-02-12 22:19:41 +0000 | [diff] [blame] | 68 | if self.options.dry_run: |
mmoss@chromium.org | 294c783 | 2015-06-17 16:16:32 +0000 | [diff] [blame] | 69 | return '' |
Edward Lemur | 7a4ced2 | 2018-01-26 16:26:05 +0100 | [diff] [blame] | 70 | if return_stdout: |
Raul Tambre | 43271f9 | 2019-07-16 14:03:54 +0000 | [diff] [blame] | 71 | return subprocess.check_output(cmd, **kwargs).decode() |
Edward Lemur | 7a4ced2 | 2018-01-26 16:26:05 +0100 | [diff] [blame] | 72 | else: |
Elly Fong-Jones | 7b29439 | 2019-04-18 18:32:10 +0000 | [diff] [blame] | 73 | try: |
| 74 | subprocess.check_call(cmd, **kwargs) |
| 75 | except subprocess.CalledProcessError as e: |
| 76 | # If the subprocess failed, it likely emitted its own distress message |
| 77 | # already - don't scroll that message off the screen with a stack trace |
| 78 | # from this program as well. Emit a terse message and bail out here; |
| 79 | # otherwise a later step will try doing more work and may hide the |
| 80 | # subprocess message. |
| 81 | print('Subprocess failed with return code %d.' % e.returncode) |
| 82 | sys.exit(e.returncode) |
Edward Lemur | 7a4ced2 | 2018-01-26 16:26:05 +0100 | [diff] [blame] | 83 | return '' |
dpranke@chromium.org | 6cc97a1 | 2013-04-12 06:15:58 +0000 | [diff] [blame] | 84 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 85 | |
| 86 | class GclientCheckout(Checkout): |
| 87 | |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 88 | def run_gclient(self, *cmd, **kwargs): |
dpranke@chromium.org | 6cc97a1 | 2013-04-12 06:15:58 +0000 | [diff] [blame] | 89 | if not spawn.find_executable('gclient'): |
| 90 | cmd_prefix = (sys.executable, os.path.join(SCRIPT_PATH, 'gclient.py')) |
| 91 | else: |
| 92 | cmd_prefix = ('gclient',) |
| 93 | return self.run(cmd_prefix + cmd, **kwargs) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 94 | |
mmoss@chromium.org | 5a44776 | 2015-06-10 20:01:39 +0000 | [diff] [blame] | 95 | def exists(self): |
| 96 | try: |
Edward Lemur | 7a4ced2 | 2018-01-26 16:26:05 +0100 | [diff] [blame] | 97 | gclient_root = self.run_gclient('root', return_stdout=True).strip() |
mmoss@chromium.org | 5a44776 | 2015-06-10 20:01:39 +0000 | [diff] [blame] | 98 | return (os.path.exists(os.path.join(gclient_root, '.gclient')) or |
| 99 | os.path.exists(os.path.join(os.getcwd(), self.root))) |
| 100 | except subprocess.CalledProcessError: |
| 101 | pass |
| 102 | return os.path.exists(os.path.join(os.getcwd(), self.root)) |
| 103 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 104 | |
| 105 | class GitCheckout(Checkout): |
| 106 | |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 107 | def run_git(self, *cmd, **kwargs): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 108 | print('Running: git %s' % (' '.join(pipes.quote(x) for x in cmd))) |
Aaron Gable | bd95f41 | 2017-11-29 11:20:26 -0800 | [diff] [blame] | 109 | if self.options.dry_run: |
| 110 | return '' |
Dan Jacques | 209a681 | 2017-07-12 11:40:20 -0700 | [diff] [blame] | 111 | return git_common.run(*cmd, **kwargs) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 112 | |
| 113 | |
jochen@chromium.org | d993e78 | 2013-04-11 20:03:13 +0000 | [diff] [blame] | 114 | class GclientGitCheckout(GclientCheckout, GitCheckout): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 115 | |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 116 | def __init__(self, options, spec, root): |
| 117 | super(GclientGitCheckout, self).__init__(options, spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 118 | assert 'solutions' in self.spec |
agable@chromium.org | 5bde64e | 2014-11-25 22:15:26 +0000 | [diff] [blame] | 119 | |
| 120 | def _format_spec(self): |
| 121 | def _format_literal(lit): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 122 | if isinstance(lit, str) or (sys.version_info.major == 2 and |
| 123 | isinstance(lit, unicode)): |
agable@chromium.org | 5bde64e | 2014-11-25 22:15:26 +0000 | [diff] [blame] | 124 | return '"%s"' % lit |
| 125 | if isinstance(lit, list): |
| 126 | return '[%s]' % ', '.join(_format_literal(i) for i in lit) |
| 127 | return '%r' % lit |
| 128 | soln_strings = [] |
| 129 | for soln in self.spec['solutions']: |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 130 | soln_string = '\n'.join(' "%s": %s,' % (key, _format_literal(value)) |
| 131 | for key, value in soln.items()) |
agable@chromium.org | 5bde64e | 2014-11-25 22:15:26 +0000 | [diff] [blame] | 132 | soln_strings.append(' {\n%s\n },' % soln_string) |
| 133 | gclient_spec = 'solutions = [\n%s\n]\n' % '\n'.join(soln_strings) |
Dr Alex Gouaillard | e1dd46f | 2016-11-28 15:00:04 +0800 | [diff] [blame] | 134 | extra_keys = ['target_os', 'target_os_only', 'cache_dir'] |
agable@chromium.org | 5bde64e | 2014-11-25 22:15:26 +0000 | [diff] [blame] | 135 | gclient_spec += ''.join('%s = %s\n' % (key, _format_literal(self.spec[key])) |
| 136 | for key in extra_keys if key in self.spec) |
| 137 | return gclient_spec |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 138 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 139 | def init(self): |
| 140 | # Configure and do the gclient checkout. |
agable@chromium.org | 5bde64e | 2014-11-25 22:15:26 +0000 | [diff] [blame] | 141 | self.run_gclient('config', '--spec', self._format_spec()) |
jochen@chromium.org | 048da08 | 2014-05-06 08:32:40 +0000 | [diff] [blame] | 142 | sync_cmd = ['sync'] |
agable@chromium.org | b98f3f2 | 2015-06-15 21:59:09 +0000 | [diff] [blame] | 143 | if self.options.nohooks: |
jochen@chromium.org | 048da08 | 2014-05-06 08:32:40 +0000 | [diff] [blame] | 144 | sync_cmd.append('--nohooks') |
primiano@chromium.org | 5439ea5 | 2014-08-06 17:18:18 +0000 | [diff] [blame] | 145 | if self.options.no_history: |
| 146 | sync_cmd.append('--no-history') |
jochen@chromium.org | 048da08 | 2014-05-06 08:32:40 +0000 | [diff] [blame] | 147 | if self.spec.get('with_branch_heads', False): |
| 148 | sync_cmd.append('--with_branch_heads') |
| 149 | self.run_gclient(*sync_cmd) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 150 | |
| 151 | # Configure git. |
| 152 | wd = os.path.join(self.base, self.root) |
wtc@chromium.org | 38e9461 | 2014-02-12 22:19:41 +0000 | [diff] [blame] | 153 | if self.options.dry_run: |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 154 | print('cd %s' % wd) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 155 | self.run_git( |
| 156 | 'submodule', 'foreach', |
| 157 | 'git config -f $toplevel/.git/config submodule.$name.ignore all', |
| 158 | cwd=wd) |
Torne (Richard Coles) | 08ca04b | 2018-02-08 15:23:08 -0500 | [diff] [blame] | 159 | if not self.options.no_history: |
| 160 | self.run_git( |
| 161 | 'config', '--add', 'remote.origin.fetch', |
| 162 | '+refs/tags/*:refs/tags/*', cwd=wd) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 163 | self.run_git('config', 'diff.ignoreSubmodules', 'all', cwd=wd) |
| 164 | |
jochen@chromium.org | d993e78 | 2013-04-11 20:03:13 +0000 | [diff] [blame] | 165 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 166 | CHECKOUT_TYPE_MAP = { |
| 167 | 'gclient': GclientCheckout, |
jochen@chromium.org | d993e78 | 2013-04-11 20:03:13 +0000 | [diff] [blame] | 168 | 'gclient_git': GclientGitCheckout, |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 169 | 'git': GitCheckout, |
| 170 | } |
| 171 | |
| 172 | |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 173 | def CheckoutFactory(type_name, options, spec, root): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 174 | """Factory to build Checkout class instances.""" |
| 175 | class_ = CHECKOUT_TYPE_MAP.get(type_name) |
| 176 | if not class_: |
| 177 | raise KeyError('unrecognized checkout type: %s' % type_name) |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 178 | return class_(options, spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | ################################################# |
| 182 | # Utility function and file entry point. |
| 183 | ################################################# |
| 184 | def usage(msg=None): |
| 185 | """Print help and exit.""" |
| 186 | if msg: |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 187 | print('Error:', msg) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 188 | |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 189 | print(textwrap.dedent("""\ |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 190 | usage: %s [options] <config> [--property=value [--property2=value2 ...]] |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 191 | |
iannucci@chromium.org | cc2d3e3 | 2014-08-06 19:47:54 +0000 | [diff] [blame] | 192 | This script can be used to download the Chromium sources. See |
| 193 | http://www.chromium.org/developers/how-tos/get-the-code |
| 194 | for full usage instructions. |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 195 | |
iannucci@chromium.org | cc2d3e3 | 2014-08-06 19:47:54 +0000 | [diff] [blame] | 196 | Valid options: |
| 197 | -h, --help, help Print this message. |
| 198 | --nohooks Don't run hooks after checkout. |
iannucci@chromium.org | 78faf8b | 2016-05-09 23:26:37 +0000 | [diff] [blame] | 199 | --force (dangerous) Don't look for existing .gclient file. |
iannucci@chromium.org | cc2d3e3 | 2014-08-06 19:47:54 +0000 | [diff] [blame] | 200 | -n, --dry-run Don't run commands, only print them. |
| 201 | --no-history Perform shallow clones, don't fetch the full git history. |
| 202 | |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 203 | Valid fetch configs:""") % os.path.basename(sys.argv[0])) |
thestig@chromium.org | 37103c9 | 2015-09-19 20:54:39 +0000 | [diff] [blame] | 204 | |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 205 | configs_dir = os.path.join(SCRIPT_PATH, 'fetch_configs') |
| 206 | configs = [f[:-3] for f in os.listdir(configs_dir) if f.endswith('.py')] |
| 207 | configs.sort() |
| 208 | for fname in configs: |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 209 | print(' ' + fname) |
iannucci@chromium.org | cc2d3e3 | 2014-08-06 19:47:54 +0000 | [diff] [blame] | 210 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 211 | sys.exit(bool(msg)) |
| 212 | |
| 213 | |
| 214 | def handle_args(argv): |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 215 | """Gets the config name from the command line arguments.""" |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 216 | if len(argv) <= 1: |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 217 | usage('Must specify a config.') |
dpranke@chromium.org | e3d147d | 2013-04-03 20:31:27 +0000 | [diff] [blame] | 218 | if argv[1] in ('-h', '--help', 'help'): |
| 219 | usage() |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 220 | |
wtc@chromium.org | 38e9461 | 2014-02-12 22:19:41 +0000 | [diff] [blame] | 221 | dry_run = False |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 222 | nohooks = False |
primiano@chromium.org | 5439ea5 | 2014-08-06 17:18:18 +0000 | [diff] [blame] | 223 | no_history = False |
iannucci@chromium.org | 78faf8b | 2016-05-09 23:26:37 +0000 | [diff] [blame] | 224 | force = False |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 225 | while len(argv) >= 2: |
| 226 | arg = argv[1] |
| 227 | if not arg.startswith('-'): |
| 228 | break |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 229 | argv.pop(1) |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 230 | if arg in ('-n', '--dry-run'): |
wtc@chromium.org | 38e9461 | 2014-02-12 22:19:41 +0000 | [diff] [blame] | 231 | dry_run = True |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 232 | elif arg == '--nohooks': |
| 233 | nohooks = True |
primiano@chromium.org | 5439ea5 | 2014-08-06 17:18:18 +0000 | [diff] [blame] | 234 | elif arg == '--no-history': |
| 235 | no_history = True |
iannucci@chromium.org | 78faf8b | 2016-05-09 23:26:37 +0000 | [diff] [blame] | 236 | elif arg == '--force': |
| 237 | force = True |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 238 | else: |
| 239 | usage('Invalid option %s.' % arg) |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 240 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 241 | def looks_like_arg(arg): |
| 242 | return arg.startswith('--') and arg.count('=') == 1 |
| 243 | |
| 244 | bad_parms = [x for x in argv[2:] if not looks_like_arg(x)] |
| 245 | if bad_parms: |
| 246 | usage('Got bad arguments %s' % bad_parms) |
| 247 | |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 248 | config = argv[1] |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 249 | props = argv[2:] |
primiano@chromium.org | 5439ea5 | 2014-08-06 17:18:18 +0000 | [diff] [blame] | 250 | return ( |
iannucci@chromium.org | 78faf8b | 2016-05-09 23:26:37 +0000 | [diff] [blame] | 251 | optparse.Values({ |
| 252 | 'dry_run': dry_run, |
| 253 | 'nohooks': nohooks, |
| 254 | 'no_history': no_history, |
| 255 | 'force': force}), |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 256 | config, |
primiano@chromium.org | 5439ea5 | 2014-08-06 17:18:18 +0000 | [diff] [blame] | 257 | props) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 258 | |
| 259 | |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 260 | def run_config_fetch(config, props, aliased=False): |
| 261 | """Invoke a config's fetch method with the passed-through args |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 262 | and return its json output as a python object.""" |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 263 | config_path = os.path.abspath( |
| 264 | os.path.join(SCRIPT_PATH, 'fetch_configs', config)) |
| 265 | if not os.path.exists(config_path + '.py'): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 266 | print("Could not find a config for %s" % config) |
dpranke@chromium.org | 2bf328a | 2013-04-03 21:14:41 +0000 | [diff] [blame] | 267 | sys.exit(1) |
| 268 | |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 269 | cmd = [sys.executable, config_path + '.py', 'fetch'] + props |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 270 | result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
dpranke@chromium.org | 2bf328a | 2013-04-03 21:14:41 +0000 | [diff] [blame] | 271 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 272 | spec = json.loads(result) |
| 273 | if 'alias' in spec: |
| 274 | assert not aliased |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 275 | return run_config_fetch( |
| 276 | spec['alias']['config'], spec['alias']['props'] + props, aliased=True) |
| 277 | cmd = [sys.executable, config_path + '.py', 'root'] |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 278 | result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 279 | root = json.loads(result) |
| 280 | return spec, root |
| 281 | |
| 282 | |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 283 | def run(options, spec, root): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 284 | """Perform a checkout with the given type and configuration. |
| 285 | |
| 286 | Args: |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 287 | options: Options instance. |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 288 | spec: Checkout configuration returned by the the config's fetch_spec |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 289 | method (checkout type, repository url, etc.). |
| 290 | root: The directory into which the repo expects to be checkout out. |
| 291 | """ |
| 292 | assert 'type' in spec |
| 293 | checkout_type = spec['type'] |
| 294 | checkout_spec = spec['%s_spec' % checkout_type] |
| 295 | try: |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 296 | checkout = CheckoutFactory(checkout_type, options, checkout_spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 297 | except KeyError: |
| 298 | return 1 |
iannucci@chromium.org | 78faf8b | 2016-05-09 23:26:37 +0000 | [diff] [blame] | 299 | if not options.force and checkout.exists(): |
Raul Tambre | c2f74c1 | 2019-03-19 05:55:53 +0000 | [diff] [blame] | 300 | print('Your current directory appears to already contain, or be part of, ') |
| 301 | print('a checkout. "fetch" is used only to get new checkouts. Use ') |
| 302 | print('"gclient sync" to update existing checkouts.') |
| 303 | print() |
| 304 | print('Fetch also does not yet deal with partial checkouts, so if fetch') |
| 305 | print('failed, delete the checkout and start over (crbug.com/230691).') |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 306 | return 1 |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 307 | return checkout.init() |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | def main(): |
luqui@chromium.org | b371a1c | 2015-12-04 01:42:48 +0000 | [diff] [blame] | 311 | options, config, props = handle_args(sys.argv) |
| 312 | spec, root = run_config_fetch(config, props) |
digit@chromium.org | 3596d58 | 2013-12-13 17:07:33 +0000 | [diff] [blame] | 313 | return run(options, spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 314 | |
| 315 | |
| 316 | if __name__ == '__main__': |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 317 | try: |
| 318 | sys.exit(main()) |
| 319 | except KeyboardInterrupt: |
| 320 | sys.stderr.write('interrupted\n') |
| 321 | sys.exit(1) |