OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Bootstrap script to clone and forward to the recipe engine tool.""" |
| 8 |
| 9 import ast |
| 10 import logging |
| 11 import os |
| 12 import random |
| 13 import re |
| 14 import subprocess |
| 15 import sys |
| 16 import time |
| 17 import traceback |
| 18 |
| 19 BOOTSTRAP_VERSION = 1 |
| 20 RECIPES_CFG = os.path.join( |
| 21 os.pardir, os.pardir, 'infra', 'config', 'recipes.cfg') |
| 22 |
| 23 |
| 24 def parse_protobuf(fh): |
| 25 """Parse the protobuf text format just well enough to understand recipes.cfg. |
| 26 |
| 27 We don't use the protobuf library because we want to be as self-contained |
| 28 as possible in this bootstrap, so it can be simply vendored into a client |
| 29 repo. |
| 30 |
| 31 We assume all fields are repeated since we don't have a proto spec to work |
| 32 with. |
| 33 |
| 34 Args: |
| 35 fh: a filehandle containing the text format protobuf. |
| 36 Returns: |
| 37 A recursive dictionary of lists. |
| 38 """ |
| 39 def parse_atom(text): |
| 40 if text == 'true': return True |
| 41 if text == 'false': return False |
| 42 return ast.literal_eval(text) |
| 43 |
| 44 ret = {} |
| 45 for line in fh: |
| 46 line = line.strip() |
| 47 m = re.match(r'(\w+)\s*:\s*(.*)', line) |
| 48 if m: |
| 49 ret.setdefault(m.group(1), []).append(parse_atom(m.group(2))) |
| 50 continue |
| 51 |
| 52 m = re.match(r'(\w+)\s*{', line) |
| 53 if m: |
| 54 subparse = parse_protobuf(fh) |
| 55 ret.setdefault(m.group(1), []).append(subparse) |
| 56 continue |
| 57 |
| 58 if line == '}': return ret |
| 59 if line == '': continue |
| 60 |
| 61 raise Exception('Could not understand line: <%s>' % line) |
| 62 |
| 63 return ret |
| 64 |
| 65 |
| 66 def get_unique(things): |
| 67 if len(things) == 1: |
| 68 return things[0] |
| 69 elif len(things) == 0: |
| 70 raise ValueError("Expected to get one thing, but dinna get none.") |
| 71 else: |
| 72 logging.warn('Expected to get one thing, but got a bunch: %s\n%s' % |
| 73 (things, traceback.format_stack())) |
| 74 return things[0] |
| 75 |
| 76 |
| 77 def main(): |
| 78 if sys.platform.startswith(('win', 'cygwin')): |
| 79 git = 'git.bat' |
| 80 else: |
| 81 git = 'git' |
| 82 |
| 83 # Find the repository and config file to operate on. |
| 84 git_dir = os.path.dirname( |
| 85 subprocess.check_output([git, 'rev-parse', '--git-dir'], |
| 86 cwd=os.path.dirname(__file__)).strip()) |
| 87 recipes_cfg_path = os.path.join(os.path.dirname(__file__), RECIPES_CFG) |
| 88 |
| 89 with open(recipes_cfg_path, 'rU') as fh: |
| 90 protobuf = parse_protobuf(fh) |
| 91 |
| 92 engine_buf = get_unique([ |
| 93 b for b in protobuf['deps'] if b.get('project_id') == ['recipe_engine'] ]) |
| 94 engine_url = get_unique(engine_buf['url']) |
| 95 engine_revision = get_unique(engine_buf['revision']) |
| 96 engine_subpath = (get_unique(engine_buf.get('path_override', [''])) |
| 97 .replace('/', os.path.sep)) |
| 98 |
| 99 recipes_path = os.path.join(git_dir, |
| 100 get_unique(protobuf['recipes_path']).replace('/', os.path.sep)) |
| 101 deps_path = os.path.join(recipes_path, '.recipe_deps') |
| 102 engine_path = os.path.join(deps_path, 'recipe_engine') |
| 103 |
| 104 # Ensure that we have the recipe engine cloned. |
| 105 def ensure_engine(): |
| 106 if not os.path.exists(deps_path): |
| 107 os.makedirs(deps_path) |
| 108 if not os.path.exists(engine_path): |
| 109 subprocess.check_call([git, 'clone', engine_url, engine_path]) |
| 110 |
| 111 needs_fetch = subprocess.call( |
| 112 [git, 'rev-parse', '--verify', '%s^{commit}' % engine_revision], |
| 113 cwd=engine_path, stdout=open(os.devnull, 'w')) |
| 114 if needs_fetch: |
| 115 subprocess.check_call([git, 'fetch'], cwd=engine_path) |
| 116 subprocess.check_call( |
| 117 [git, 'checkout', '--quiet', engine_revision], cwd=engine_path) |
| 118 |
| 119 try: |
| 120 ensure_engine() |
| 121 except subprocess.CalledProcessError as e: |
| 122 if e.returncode == 128: # Thrown when git gets a lock error. |
| 123 time.sleep(random.uniform(2,5)) |
| 124 ensure_engine() |
| 125 else: |
| 126 raise |
| 127 |
| 128 args = ['--package', recipes_cfg_path, |
| 129 '--bootstrap-script', __file__] + sys.argv[1:] |
| 130 return subprocess.call([ |
| 131 sys.executable, '-u', |
| 132 os.path.join(engine_path, engine_subpath, 'recipes.py')] + args) |
| 133 |
| 134 if __name__ == '__main__': |
| 135 sys.exit(main()) |
OLD | NEW |