agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [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: |
| 10 | fetch <recipe> [--property=value [--property2=value2 ...]] |
| 11 | |
| 12 | This script is a wrapper around various version control and repository |
| 13 | checkout commands. It requires a |recipe| name, fetches data from that |
| 14 | recipe in depot_tools/recipes, and then performs all necessary inits, |
| 15 | checkouts, pulls, fetches, etc. |
| 16 | |
| 17 | Optional arguments may be passed on the command line in key-value pairs. |
| 18 | These parameters will be passed through to the recipe's main method. |
| 19 | """ |
| 20 | |
| 21 | import json |
| 22 | import os |
| 23 | import subprocess |
| 24 | import sys |
| 25 | import pipes |
| 26 | |
| 27 | |
| 28 | SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 29 | |
| 30 | |
| 31 | ################################################# |
| 32 | # Checkout class definitions. |
| 33 | ################################################# |
| 34 | class Checkout(object): |
| 35 | """Base class for implementing different types of checkouts. |
| 36 | |
| 37 | Attributes: |
| 38 | |base|: the absolute path of the directory in which this script is run. |
| 39 | |spec|: the spec for this checkout as returned by the recipe. Different |
| 40 | subclasses will expect different keys in this dictionary. |
| 41 | |root|: the directory into which the checkout will be performed, as returned |
| 42 | by the recipe. This is a relative path from |base|. |
| 43 | """ |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 44 | def __init__(self, dryrun, spec, root): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 45 | self.base = os.getcwd() |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 46 | self.dryrun = dryrun |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 47 | self.spec = spec |
| 48 | self.root = root |
| 49 | |
| 50 | def exists(self): |
| 51 | pass |
| 52 | |
| 53 | def init(self): |
| 54 | pass |
| 55 | |
| 56 | def sync(self): |
| 57 | pass |
| 58 | |
| 59 | |
| 60 | class GclientCheckout(Checkout): |
| 61 | |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 62 | def run_gclient(self, *cmd, **kwargs): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 63 | print 'Running: gclient %s' % ' '.join(pipes.quote(x) for x in cmd) |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 64 | if not self.dryrun: |
dpranke@chromium.org | 6b8c91a | 2013-04-03 22:05:06 +0000 | [diff] [blame] | 65 | return subprocess.check_call( |
| 66 | (sys.executable, os.path.join(SCRIPT_PATH, 'gclient.py')) + cmd, |
| 67 | **kwargs) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | class GitCheckout(Checkout): |
| 71 | |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 72 | def run_git(self, *cmd, **kwargs): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 73 | print 'Running: git %s' % ' '.join(pipes.quote(x) for x in cmd) |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 74 | if not self.dryrun: |
| 75 | return subprocess.check_call(('git',) + cmd, **kwargs) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 76 | |
| 77 | |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 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): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 87 | |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 88 | def __init__(self, dryrun, spec, root): |
| 89 | super(GclientGitSvnCheckout, self).__init__(dryrun, spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 90 | assert 'solutions' in self.spec |
| 91 | keys = ['solutions', 'target_os', 'target_os_only'] |
| 92 | gclient_spec = '\n'.join('%s = %s' % (key, self.spec[key]) |
| 93 | for key in self.spec if key in keys) |
| 94 | self.spec['gclient_spec'] = gclient_spec |
| 95 | assert 'svn_url' in self.spec |
| 96 | assert 'svn_branch' in self.spec |
| 97 | assert 'svn_ref' in self.spec |
| 98 | |
| 99 | def exists(self): |
| 100 | return os.path.exists(os.path.join(os.getcwd(), self.root)) |
| 101 | |
| 102 | def init(self): |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 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 | |
dpranke@chromium.org | 8623da3 | 2013-04-04 17:36:01 +0000 | [diff] [blame] | 113 | # TODO(dpranke): Work around issues w/ delta compression on big repos. |
| 114 | self.run_git('config', '--global', 'core.deltaBaseCacheLimit', '1G') |
| 115 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 116 | # Configure and do the gclient checkout. |
| 117 | self.run_gclient('config', '--spec', self.spec['gclient_spec']) |
| 118 | self.run_gclient('sync') |
| 119 | |
| 120 | # Configure git. |
| 121 | wd = os.path.join(self.base, self.root) |
dpranke@chromium.org | 7ca51b3 | 2013-04-03 21:29:50 +0000 | [diff] [blame] | 122 | if self.dryrun: |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 123 | print 'cd %s' % wd |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 124 | self.run_git( |
| 125 | 'submodule', 'foreach', |
| 126 | 'git config -f $toplevel/.git/config submodule.$name.ignore all', |
| 127 | cwd=wd) |
| 128 | self.run_git('config', 'diff.ignoreSubmodules', 'all', cwd=wd) |
| 129 | |
| 130 | # Configure git-svn. |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 131 | for path, svn_spec in git_svn_dirs.iteritems(): |
| 132 | real_path = os.path.join(*path.split('/')) |
| 133 | if real_path != self.root: |
| 134 | real_path = os.path.join(self.root, real_path) |
| 135 | wd = os.path.join(self.base, real_path) |
dpranke@chromium.org | 7ca51b3 | 2013-04-03 21:29:50 +0000 | [diff] [blame] | 136 | if self.dryrun: |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 137 | print 'cd %s' % wd |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 138 | self.run_git('svn', 'init', '--prefix=origin/', '-T', |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 139 | svn_spec['svn_branch'], svn_spec['svn_url'], cwd=wd) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 140 | self.run_git('config', '--replace', 'svn-remote.svn.fetch', |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 141 | svn_spec['svn_branch'] + ':refs/remotes/origin/' + |
| 142 | svn_spec['svn_ref'], cwd=wd) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 143 | self.run_git('svn', 'fetch', cwd=wd) |
| 144 | |
| 145 | |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 146 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 147 | CHECKOUT_TYPE_MAP = { |
| 148 | 'gclient': GclientCheckout, |
| 149 | 'gclient_git_svn': GclientGitSvnCheckout, |
| 150 | 'git': GitCheckout, |
| 151 | } |
| 152 | |
| 153 | |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 154 | def CheckoutFactory(type_name, dryrun, spec, root): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 155 | """Factory to build Checkout class instances.""" |
| 156 | class_ = CHECKOUT_TYPE_MAP.get(type_name) |
| 157 | if not class_: |
| 158 | raise KeyError('unrecognized checkout type: %s' % type_name) |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 159 | return class_(dryrun, spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | ################################################# |
| 163 | # Utility function and file entry point. |
| 164 | ################################################# |
| 165 | def usage(msg=None): |
| 166 | """Print help and exit.""" |
| 167 | if msg: |
| 168 | print 'Error:', msg |
| 169 | |
| 170 | print ( |
| 171 | """ |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 172 | usage: %s [-n|--dry-run] <recipe> [--property=value [--property2=value2 ...]] |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 173 | """ % os.path.basename(sys.argv[0])) |
| 174 | sys.exit(bool(msg)) |
| 175 | |
| 176 | |
| 177 | def handle_args(argv): |
| 178 | """Gets the recipe name from the command line arguments.""" |
| 179 | if len(argv) <= 1: |
| 180 | usage('Must specify a recipe.') |
dpranke@chromium.org | e3d147d | 2013-04-03 20:31:27 +0000 | [diff] [blame] | 181 | if argv[1] in ('-h', '--help', 'help'): |
| 182 | usage() |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 183 | |
dpranke@chromium.org | a992edb | 2013-04-03 21:22:20 +0000 | [diff] [blame] | 184 | dryrun = False |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 185 | if argv[1] in ('-n', '--dry-run'): |
| 186 | dryrun = True |
| 187 | argv.pop(1) |
| 188 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 189 | def looks_like_arg(arg): |
| 190 | return arg.startswith('--') and arg.count('=') == 1 |
| 191 | |
| 192 | bad_parms = [x for x in argv[2:] if not looks_like_arg(x)] |
| 193 | if bad_parms: |
| 194 | usage('Got bad arguments %s' % bad_parms) |
| 195 | |
| 196 | recipe = argv[1] |
| 197 | props = argv[2:] |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 198 | return dryrun, recipe, props |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | def run_recipe_fetch(recipe, props, aliased=False): |
| 202 | """Invoke a recipe's fetch method with the passed-through args |
| 203 | and return its json output as a python object.""" |
| 204 | recipe_path = os.path.abspath(os.path.join(SCRIPT_PATH, 'recipes', recipe)) |
dpranke@chromium.org | a992edb | 2013-04-03 21:22:20 +0000 | [diff] [blame] | 205 | if not os.path.exists(recipe_path + '.py'): |
dpranke@chromium.org | 2bf328a | 2013-04-03 21:14:41 +0000 | [diff] [blame] | 206 | print "Could not find a recipe for %s" % recipe |
| 207 | sys.exit(1) |
| 208 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 209 | cmd = [sys.executable, recipe_path + '.py', 'fetch'] + props |
| 210 | result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
dpranke@chromium.org | 2bf328a | 2013-04-03 21:14:41 +0000 | [diff] [blame] | 211 | |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 212 | spec = json.loads(result) |
| 213 | if 'alias' in spec: |
| 214 | assert not aliased |
| 215 | return run_recipe_fetch( |
| 216 | spec['alias']['recipe'], spec['alias']['props'] + props, aliased=True) |
| 217 | cmd = [sys.executable, recipe_path + '.py', 'root'] |
| 218 | result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 219 | root = json.loads(result) |
| 220 | return spec, root |
| 221 | |
| 222 | |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 223 | def run(dryrun, spec, root): |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 224 | """Perform a checkout with the given type and configuration. |
| 225 | |
| 226 | Args: |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 227 | dryrun: if True, don't actually execute the commands |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 228 | spec: Checkout configuration returned by the the recipe's fetch_spec |
| 229 | method (checkout type, repository url, etc.). |
| 230 | root: The directory into which the repo expects to be checkout out. |
| 231 | """ |
| 232 | assert 'type' in spec |
| 233 | checkout_type = spec['type'] |
| 234 | checkout_spec = spec['%s_spec' % checkout_type] |
| 235 | try: |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 236 | checkout = CheckoutFactory(checkout_type, dryrun, checkout_spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 237 | except KeyError: |
| 238 | return 1 |
| 239 | if checkout.exists(): |
| 240 | print 'You appear to already have this checkout.' |
| 241 | print 'Aborting to avoid clobbering your work.' |
| 242 | return 1 |
agable@chromium.org | 2560ea7 | 2013-04-04 01:22:38 +0000 | [diff] [blame] | 243 | return checkout.init() |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 244 | |
| 245 | |
| 246 | def main(): |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 247 | dryrun, recipe, props = handle_args(sys.argv) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 248 | spec, root = run_recipe_fetch(recipe, props) |
dpranke@chromium.org | d88d7f5 | 2013-04-03 21:09:07 +0000 | [diff] [blame] | 249 | return run(dryrun, spec, root) |
agable@chromium.org | cc02350 | 2013-04-03 20:24:21 +0000 | [diff] [blame] | 250 | |
| 251 | |
| 252 | if __name__ == '__main__': |
| 253 | sys.exit(main()) |