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