OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright (c) 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.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(field, text): | |
55 if text == 'true': | |
56 return True | |
57 if text == 'false': | |
58 return False | |
59 | |
60 # repo_type is an enum. Since it does not have quotes, | |
61 # invoking literal_eval would fail. | |
62 if field == 'repo_type': | |
63 return text | |
64 | |
65 return ast.literal_eval(text) | |
66 | |
67 ret = {} | |
68 for line in fh: | |
69 line = line.strip() | |
70 m = re.match(r'(\w+)\s*:\s*(.*)', line) | |
71 if m: | |
72 ret.setdefault(m.group(1), []).append(parse_atom(m.group(1), m.group(2))) | |
73 continue | |
74 | |
75 m = re.match(r'(\w+)\s*{', line) | |
76 if m: | |
77 subparse = parse_protobuf(fh) | |
78 ret.setdefault(m.group(1), []).append(subparse) | |
79 continue | |
80 | |
81 if line == '}': | |
82 return ret | |
83 if line == '': | |
84 continue | |
85 | |
86 raise ValueError('Could not understand line: <%s>' % line) | |
87 | |
88 return ret | |
89 | |
90 | |
91 def get_unique(things): | |
92 if len(things) == 1: | |
93 return things[0] | |
94 elif len(things) == 0: | |
95 raise ValueError("Expected to get one thing, but dinna get none.") | |
96 else: | |
97 logging.warn('Expected to get one thing, but got a bunch: %s\n%s' % | |
98 (things, traceback.format_stack())) | |
99 return things[0] | |
100 | |
101 | |
102 def _subprocess_call(argv, **kwargs): | |
103 logging.info('Running %r', argv) | |
104 return subprocess.call(argv, **kwargs) | |
105 | |
106 def _subprocess_check_call(argv, **kwargs): | |
107 logging.info('Running %r', argv) | |
108 subprocess.check_call(argv, **kwargs) | |
109 | |
110 | |
111 def main(): | |
112 if '--verbose' in sys.argv: | |
113 logging.getLogger().setLevel(logging.INFO) | |
114 | |
115 if sys.platform.startswith(('win', 'cygwin')): | |
116 git = 'git.bat' | |
117 else: | |
118 git = 'git' | |
119 | |
120 # Find the repository and config file to operate on. | |
121 repo_root = os.path.abspath( | |
122 os.path.join(os.path.dirname(__file__), REPO_ROOT)) | |
123 recipes_cfg_path = os.path.join(repo_root, RECIPES_CFG) | |
124 | |
125 with open(recipes_cfg_path, 'rU') as fh: | |
126 protobuf = parse_protobuf(fh) | |
127 | |
128 engine_buf = get_unique([ | |
129 b for b in protobuf['deps'] if b.get('project_id') == ['recipe_engine'] ]) | |
130 engine_url = get_unique(engine_buf['url']) | |
131 engine_revision = get_unique(engine_buf['revision']) | |
132 engine_subpath = (get_unique(engine_buf.get('path_override', [''])) | |
133 .replace('/', os.path.sep)) | |
134 | |
135 recipes_path = os.path.join(repo_root, | |
136 get_unique(protobuf['recipes_path']).replace('/', os.path.sep)) | |
137 deps_path = os.path.join(recipes_path, '.recipe_deps') | |
138 engine_path = os.path.join(deps_path, 'recipe_engine') | |
139 | |
140 # Ensure that we have the recipe engine cloned. | |
141 def ensure_engine(): | |
142 if not os.path.exists(deps_path): | |
143 os.makedirs(deps_path) | |
144 if not os.path.exists(engine_path): | |
145 _subprocess_check_call([git, 'clone', engine_url, engine_path]) | |
146 | |
147 needs_fetch = _subprocess_call( | |
148 [git, 'rev-parse', '--verify', '%s^{commit}' % engine_revision], | |
149 cwd=engine_path, stdout=open(os.devnull, 'w')) | |
150 if needs_fetch: | |
151 _subprocess_check_call([git, 'fetch'], cwd=engine_path) | |
152 _subprocess_check_call( | |
153 [git, 'checkout', '--quiet', engine_revision], cwd=engine_path) | |
154 | |
155 try: | |
156 ensure_engine() | |
157 except subprocess.CalledProcessError: | |
158 logging.exception('ensure_engine failed') | |
159 | |
160 # Retry errors. | |
161 time.sleep(random.uniform(2,5)) | |
162 ensure_engine() | |
163 | |
164 args = ['--package', recipes_cfg_path] + sys.argv[1:] | |
165 return _subprocess_call([ | |
166 sys.executable, '-u', | |
167 os.path.join(engine_path, engine_subpath, 'recipes.py')] + args) | |
168 | |
169 if __name__ == '__main__': | |
170 sys.exit(main()) | |
OLD | NEW |