Chromium Code Reviews| 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 <config> [--property=value [--property2=value2 ...]] | 10 fetch <config> [--property=value [--property2=value2 ...]] |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 print textwrap.dedent("""\ | 224 print textwrap.dedent("""\ |
| 225 usage: %s [options] <config> [--property=value [--property2=value2 ...]] | 225 usage: %s [options] <config> [--property=value [--property2=value2 ...]] |
| 226 | 226 |
| 227 This script can be used to download the Chromium sources. See | 227 This script can be used to download the Chromium sources. See |
| 228 http://www.chromium.org/developers/how-tos/get-the-code | 228 http://www.chromium.org/developers/how-tos/get-the-code |
| 229 for full usage instructions. | 229 for full usage instructions. |
| 230 | 230 |
| 231 Valid options: | 231 Valid options: |
| 232 -h, --help, help Print this message. | 232 -h, --help, help Print this message. |
| 233 --nohooks Don't run hooks after checkout. | 233 --nohooks Don't run hooks after checkout. |
| 234 --force (dangerous) Don't look for existing .gclient file. | |
| 234 -n, --dry-run Don't run commands, only print them. | 235 -n, --dry-run Don't run commands, only print them. |
| 235 --no-history Perform shallow clones, don't fetch the full git histo ry. | 236 --no-history Perform shallow clones, don't fetch the full git histo ry. |
| 236 | 237 |
| 237 Valid fetch configs:""") % os.path.basename(sys.argv[0]) | 238 Valid fetch configs:""") % os.path.basename(sys.argv[0]) |
| 238 | 239 |
| 239 configs_dir = os.path.join(SCRIPT_PATH, 'fetch_configs') | 240 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 = [f[:-3] for f in os.listdir(configs_dir) if f.endswith('.py')] |
| 241 configs.sort() | 242 configs.sort() |
| 242 for fname in configs: | 243 for fname in configs: |
| 243 print ' ' + fname | 244 print ' ' + fname |
| 244 | 245 |
| 245 sys.exit(bool(msg)) | 246 sys.exit(bool(msg)) |
| 246 | 247 |
| 247 | 248 |
| 248 def handle_args(argv): | 249 def handle_args(argv): |
| 249 """Gets the config name from the command line arguments.""" | 250 """Gets the config name from the command line arguments.""" |
| 250 if len(argv) <= 1: | 251 if len(argv) <= 1: |
| 251 usage('Must specify a config.') | 252 usage('Must specify a config.') |
| 252 if argv[1] in ('-h', '--help', 'help'): | 253 if argv[1] in ('-h', '--help', 'help'): |
| 253 usage() | 254 usage() |
| 254 | 255 |
| 255 dry_run = False | 256 dry_run = False |
| 256 nohooks = False | 257 nohooks = False |
| 257 no_history = False | 258 no_history = False |
| 259 force = False | |
| 258 while len(argv) >= 2: | 260 while len(argv) >= 2: |
|
dsansome
2016/05/10 07:37:45
Parsing arguments manually in Python... what year
| |
| 259 arg = argv[1] | 261 arg = argv[1] |
| 260 if not arg.startswith('-'): | 262 if not arg.startswith('-'): |
| 261 break | 263 break |
| 262 argv.pop(1) | 264 argv.pop(1) |
| 263 if arg in ('-n', '--dry-run'): | 265 if arg in ('-n', '--dry-run'): |
| 264 dry_run = True | 266 dry_run = True |
| 265 elif arg == '--nohooks': | 267 elif arg == '--nohooks': |
| 266 nohooks = True | 268 nohooks = True |
| 267 elif arg == '--no-history': | 269 elif arg == '--no-history': |
| 268 no_history = True | 270 no_history = True |
| 271 elif arg == '--force': | |
| 272 force = True | |
| 269 else: | 273 else: |
| 270 usage('Invalid option %s.' % arg) | 274 usage('Invalid option %s.' % arg) |
| 271 | 275 |
| 272 def looks_like_arg(arg): | 276 def looks_like_arg(arg): |
| 273 return arg.startswith('--') and arg.count('=') == 1 | 277 return arg.startswith('--') and arg.count('=') == 1 |
| 274 | 278 |
| 275 bad_parms = [x for x in argv[2:] if not looks_like_arg(x)] | 279 bad_parms = [x for x in argv[2:] if not looks_like_arg(x)] |
| 276 if bad_parms: | 280 if bad_parms: |
| 277 usage('Got bad arguments %s' % bad_parms) | 281 usage('Got bad arguments %s' % bad_parms) |
| 278 | 282 |
| 279 config = argv[1] | 283 config = argv[1] |
| 280 props = argv[2:] | 284 props = argv[2:] |
| 281 return ( | 285 return ( |
| 282 optparse.Values( | 286 optparse.Values({ |
| 283 {'dry_run':dry_run, 'nohooks':nohooks, 'no_history': no_history }), | 287 'dry_run': dry_run, |
| 288 'nohooks': nohooks, | |
| 289 'no_history': no_history, | |
| 290 'force': force}), | |
| 284 config, | 291 config, |
| 285 props) | 292 props) |
| 286 | 293 |
| 287 | 294 |
| 288 def run_config_fetch(config, props, aliased=False): | 295 def run_config_fetch(config, props, aliased=False): |
| 289 """Invoke a config's fetch method with the passed-through args | 296 """Invoke a config's fetch method with the passed-through args |
| 290 and return its json output as a python object.""" | 297 and return its json output as a python object.""" |
| 291 config_path = os.path.abspath( | 298 config_path = os.path.abspath( |
| 292 os.path.join(SCRIPT_PATH, 'fetch_configs', config)) | 299 os.path.join(SCRIPT_PATH, 'fetch_configs', config)) |
| 293 if not os.path.exists(config_path + '.py'): | 300 if not os.path.exists(config_path + '.py'): |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 317 method (checkout type, repository url, etc.). | 324 method (checkout type, repository url, etc.). |
| 318 root: The directory into which the repo expects to be checkout out. | 325 root: The directory into which the repo expects to be checkout out. |
| 319 """ | 326 """ |
| 320 assert 'type' in spec | 327 assert 'type' in spec |
| 321 checkout_type = spec['type'] | 328 checkout_type = spec['type'] |
| 322 checkout_spec = spec['%s_spec' % checkout_type] | 329 checkout_spec = spec['%s_spec' % checkout_type] |
| 323 try: | 330 try: |
| 324 checkout = CheckoutFactory(checkout_type, options, checkout_spec, root) | 331 checkout = CheckoutFactory(checkout_type, options, checkout_spec, root) |
| 325 except KeyError: | 332 except KeyError: |
| 326 return 1 | 333 return 1 |
| 327 if checkout.exists(): | 334 if not options.force and checkout.exists(): |
| 328 print 'Your current directory appears to already contain, or be part of, ' | 335 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 ' | 336 print 'a checkout. "fetch" is used only to get new checkouts. Use ' |
| 330 print '"gclient sync" to update existing checkouts.' | 337 print '"gclient sync" to update existing checkouts.' |
| 331 print | 338 print |
| 332 print 'Fetch also does not yet deal with partial checkouts, so if fetch' | 339 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).' | 340 print 'failed, delete the checkout and start over (crbug.com/230691).' |
| 334 return 1 | 341 return 1 |
| 335 return checkout.init() | 342 return checkout.init() |
| 336 | 343 |
| 337 | 344 |
| 338 def main(): | 345 def main(): |
| 339 options, config, props = handle_args(sys.argv) | 346 options, config, props = handle_args(sys.argv) |
| 340 spec, root = run_config_fetch(config, props) | 347 spec, root = run_config_fetch(config, props) |
| 341 return run(options, spec, root) | 348 return run(options, spec, root) |
| 342 | 349 |
| 343 | 350 |
| 344 if __name__ == '__main__': | 351 if __name__ == '__main__': |
| 345 try: | 352 try: |
| 346 sys.exit(main()) | 353 sys.exit(main()) |
| 347 except KeyboardInterrupt: | 354 except KeyboardInterrupt: |
| 348 sys.stderr.write('interrupted\n') | 355 sys.stderr.write('interrupted\n') |
| 349 sys.exit(1) | 356 sys.exit(1) |
| OLD | NEW |