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