OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2016 The LUCI Authors. All rights reserved. | |
3 # Use of this source code is governed under the Apache License, Version 2.0 | |
4 # that can be found in the LICENSE file. | |
5 """Bootstrap script to clone and forward to the recipe engine tool. | |
6 *********************************************************************** | |
7 ** DO NOT MODIFY EXCEPT IN THE PER-REPO CONFIGURATION SECTION BELOW. ** | |
8 *********************************************************************** | |
9 This is a copy of https://github.com/luci/recipes-py/blob/master/doc/recipes.py. | |
10 To fix bugs, fix in the github repo then copy it back to here and fix the | |
11 PER-REPO CONFIGURATION section to look like this one. | |
12 """ | |
13 import os | |
14 #### PER-REPO CONFIGURATION (editable) #### | |
rmistry
2016/07/26 18:00:38
This file is very compact which is fine because it
borenet
2016/07/26 18:08:45
Sure, done.
| |
15 # The root of the repository relative to the directory of this file. | |
16 REPO_ROOT = os.path.join(os.pardir, os.pardir) | |
17 # The path of the recipes.cfg file relative to the root of the repository. | |
18 RECIPES_CFG = os.path.join('infra', 'config', 'recipes.cfg') | |
19 #### END PER-REPO CONFIGURATION #### | |
20 BOOTSTRAP_VERSION = 1 | |
21 import ast | |
22 import logging | |
23 import random | |
24 import re | |
25 import subprocess | |
26 import sys | |
27 import time | |
28 import traceback | |
29 def parse_protobuf(fh): | |
30 """Parse the protobuf text format just well enough to understand recipes.cfg. | |
31 We don't use the protobuf library because we want to be as self-contained | |
32 as possible in this bootstrap, so it can be simply vendored into a client | |
33 repo. | |
34 We assume all fields are repeated since we don't have a proto spec to work | |
35 with. | |
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': | |
43 return True | |
44 if text == 'false': | |
45 return False | |
46 return ast.literal_eval(text) | |
47 ret = {} | |
48 for line in fh: | |
49 line = line.strip() | |
50 m = re.match(r'(\w+)\s*:\s*(.*)', line) | |
51 if m: | |
52 ret.setdefault(m.group(1), []).append(parse_atom(m.group(2))) | |
53 continue | |
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 if line == '}': | |
60 return ret | |
61 if line == '': | |
62 continue | |
63 raise ValueError('Could not understand line: <%s>' % line) | |
64 return ret | |
65 def get_unique(things): | |
66 if len(things) == 1: | |
67 return things[0] | |
68 elif len(things) == 0: | |
69 raise ValueError("Expected to get one thing, but dinna get none.") | |
70 else: | |
71 logging.warn('Expected to get one thing, but got a bunch: %s\n%s' % | |
72 (things, traceback.format_stack())) | |
73 return things[0] | |
74 def _subprocess_call(argv, **kwargs): | |
75 logging.info('Running %r', argv) | |
76 return subprocess.call(argv, **kwargs) | |
77 def _subprocess_check_call(argv, **kwargs): | |
78 logging.info('Running %r', argv) | |
79 subprocess.check_call(argv, **kwargs) | |
80 def main(): | |
81 if '--verbose' in sys.argv: | |
82 logging.getLogger().setLevel(logging.INFO) | |
83 if sys.platform.startswith(('win', 'cygwin')): | |
84 git = 'git.bat' | |
85 else: | |
86 git = 'git' | |
87 # Find the repository and config file to operate on. | |
88 repo_root = os.path.abspath( | |
89 os.path.join(os.path.dirname(__file__), REPO_ROOT)) | |
90 recipes_cfg_path = os.path.join(repo_root, RECIPES_CFG) | |
91 with open(recipes_cfg_path, 'rU') as fh: | |
92 protobuf = parse_protobuf(fh) | |
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 recipes_path = os.path.join(repo_root, | |
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 # Ensure that we have the recipe engine cloned. | |
104 def ensure_engine(): | |
105 if not os.path.exists(deps_path): | |
106 os.makedirs(deps_path) | |
107 if not os.path.exists(engine_path): | |
108 _subprocess_check_call([git, 'clone', engine_url, engine_path]) | |
109 needs_fetch = _subprocess_call( | |
110 [git, 'rev-parse', '--verify', '%s^{commit}' % engine_revision], | |
111 cwd=engine_path, stdout=open(os.devnull, 'w')) | |
112 if needs_fetch: | |
113 _subprocess_check_call([git, 'fetch'], cwd=engine_path) | |
114 _subprocess_check_call( | |
115 [git, 'checkout', '--quiet', engine_revision], cwd=engine_path) | |
116 try: | |
117 ensure_engine() | |
118 except subprocess.CalledProcessError: | |
119 logging.exception('ensure_engine failed') | |
120 # Retry errors. | |
121 time.sleep(random.uniform(2,5)) | |
122 ensure_engine() | |
123 args = ['--package', recipes_cfg_path, | |
124 '--bootstrap-script', __file__] + sys.argv[1:] | |
125 return _subprocess_call([ | |
126 sys.executable, '-u', | |
127 os.path.join(engine_path, engine_subpath, 'recipes.py')] + args) | |
128 if __name__ == '__main__': | |
129 sys.exit(main()) | |
OLD | NEW |