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