blob: c87581f0f1db639f6dfb0dd4c9acaed5de1b7295 [file] [log] [blame]
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -07001#!/bin/sh
2# Copyright 2019 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
6# We want to run python in unbuffered mode; however shebangs on linux grab the
7# entire rest of the shebang line as a single argument, leading to errors like:
8#
9# /usr/bin/env: 'python -u': No such file or directory
10#
11# This little shell hack is a triple-quoted noop in python, but in sh it
12# evaluates to re-exec'ing this script in unbuffered mode.
13# pylint: disable=pointless-string-statement
14''''exec python -u -- "$0" ${1+"$@"} # '''
15# vi: syntax=python
16"""Bootstrap script to clone and forward to the recipe engine tool.
17
18*******************
19** DO NOT MODIFY **
20*******************
21
22This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/master/recipes.py.
23To fix bugs, fix in the googlesource repo then run the autoroller.
24"""
25
26# pylint: disable=wrong-import-position
27import argparse
Prathmesh Prabhuc81b4a42020-07-21 13:25:02 -070028import errno
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070029import json
30import logging
31import os
32import subprocess
33import sys
34import urlparse
35
36from collections import namedtuple
37
38# The dependency entry for the recipe_engine in the client repo's recipes.cfg
39#
40# url (str) - the url to the engine repo we want to use.
41# revision (str) - the git revision for the engine to get.
42# branch (str) - the branch to fetch for the engine as an absolute ref (e.g.
43# refs/heads/master)
44EngineDep = namedtuple('EngineDep', 'url revision branch')
45
46
47class MalformedRecipesCfg(Exception):
48
49 def __init__(self, msg, path):
50 full_message = 'malformed recipes.cfg: %s: %r' % (msg, path)
51 super(MalformedRecipesCfg, self).__init__(full_message)
52
53
54def parse(repo_root, recipes_cfg_path):
55 """Parse is a lightweight a recipes.cfg file parser.
56
57 Args:
58 repo_root (str) - native path to the root of the repo we're trying to run
59 recipes for.
60 recipes_cfg_path (str) - native path to the recipes.cfg file to process.
61
62 Returns (as tuple):
63 engine_dep (EngineDep|None): The recipe_engine dependency, or None, if the
64 current repo IS the recipe_engine.
65 recipes_path (str) - native path to where the recipes live inside of the
66 current repo (i.e. the folder containing `recipes/` and/or
67 `recipe_modules`)
68 """
69 with open(recipes_cfg_path, 'rU') as fh:
70 pb = json.load(fh)
71
72 try:
73 if pb['api_version'] != 2:
74 raise MalformedRecipesCfg('unknown version %d' % pb['api_version'],
75 recipes_cfg_path)
76
77 # If we're running ./recipes.py from the recipe_engine repo itself, then
78 # return None to signal that there's no EngineDep.
79 repo_name = pb.get('repo_name')
80 if not repo_name:
81 repo_name = pb['project_id']
82 if repo_name == 'recipe_engine':
83 return None, pb.get('recipes_path', '')
84
85 engine = pb['deps']['recipe_engine']
86
87 if 'url' not in engine:
88 raise MalformedRecipesCfg(
89 'Required field "url" in dependency "recipe_engine" not found',
90 recipes_cfg_path)
91
92 engine.setdefault('revision', '')
93 engine.setdefault('branch', 'refs/heads/master')
94 recipes_path = pb.get('recipes_path', '')
95
96 # TODO(iannucci): only support absolute refs
97 if not engine['branch'].startswith('refs/'):
98 engine['branch'] = 'refs/heads/' + engine['branch']
99
100 recipes_path = os.path.join(repo_root,
101 recipes_path.replace('/', os.path.sep))
102 return EngineDep(**engine), recipes_path
103 except KeyError as ex:
104 raise MalformedRecipesCfg(ex.message, recipes_cfg_path)
105
106
107_BAT = '.bat' if sys.platform.startswith(('win', 'cygwin')) else ''
108GIT = 'git' + _BAT
109VPYTHON = 'vpython' + _BAT
110CIPD = 'cipd' + _BAT
111REQUIRED_BINARIES = {GIT, VPYTHON, CIPD}
112
113
114def _is_executable(path):
115 return os.path.isfile(path) and os.access(path, os.X_OK)
116
117
118# TODO: Use shutil.which once we switch to Python3.
119def _is_on_path(basename):
120 for path in os.environ['PATH'].split(os.pathsep):
121 full_path = os.path.join(path, basename)
122 if _is_executable(full_path):
123 return True
124 return False
125
126
127def _subprocess_call(argv, **kwargs):
128 logging.info('Running %r', argv)
129 return subprocess.call(argv, **kwargs)
130
131
132def _git_check_call(argv, **kwargs):
133 argv = [GIT] + argv
134 logging.info('Running %r', argv)
135 subprocess.check_call(argv, **kwargs)
136
137
138def _git_output(argv, **kwargs):
139 argv = [GIT] + argv
140 logging.info('Running %r', argv)
141 return subprocess.check_output(argv, **kwargs)
142
143
144def parse_args(argv):
145 """This extracts a subset of the arguments that this bootstrap script cares
146 about. Currently this consists of:
147 * an override for the recipe engine in the form of `-O recipe_engine=/path`
148 * the --package option.
149 """
150 PREFIX = 'recipe_engine='
151
152 p = argparse.ArgumentParser(add_help=False)
153 p.add_argument('-O', '--project-override', action='append')
154 p.add_argument('--package', type=os.path.abspath)
155 args, _ = p.parse_known_args(argv)
156 for override in args.project_override or ():
157 if override.startswith(PREFIX):
158 return override[len(PREFIX):], args.package
159 return None, args.package
160
161
162def checkout_engine(engine_path, repo_root, recipes_cfg_path):
163 dep, recipes_path = parse(repo_root, recipes_cfg_path)
164 if dep is None:
165 # we're running from the engine repo already!
166 return os.path.join(repo_root, recipes_path)
167
168 url = dep.url
169
170 if not engine_path and url.startswith('file://'):
171 engine_path = urlparse.urlparse(url).path
172
173 if not engine_path:
174 revision = dep.revision
175 branch = dep.branch
176
177 # Ensure that we have the recipe engine cloned.
178 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
179
180 with open(os.devnull, 'w') as NUL:
181 # Note: this logic mirrors the logic in recipe_engine/fetch.py
182 _git_check_call(['init', engine_path], stdout=NUL)
183
184 try:
185 _git_check_call(['rev-parse', '--verify',
186 '%s^{commit}' % revision],
187 cwd=engine_path,
188 stdout=NUL,
189 stderr=NUL)
190 except subprocess.CalledProcessError:
191 _git_check_call(['fetch', url, branch],
192 cwd=engine_path,
193 stdout=NUL,
194 stderr=NUL)
195
196 try:
197 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
198 except subprocess.CalledProcessError:
Prathmesh Prabhuc81b4a42020-07-21 13:25:02 -0700199 index_lock = os.path.join(engine_path, '.git', 'index.lock')
200 try:
201 os.remove(index_lock)
202 except OSError as exc:
203 if exc.errno != errno.EEXIST:
204 logging.warn('failed to remove %r, reset will fail: %s', index_lock, exc)
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700205 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
206
207 # If the engine has refactored/moved modules we need to clean all .pyc files
208 # or things will get squirrely.
209 _git_check_call(['clean', '-qxf'], cwd=engine_path)
210
211 return engine_path
212
213
214def main():
215 for required_binary in REQUIRED_BINARIES:
216 if not _is_on_path(required_binary):
217 return 'Required binary is not found on PATH: %s' % required_binary
218
219 if '--verbose' in sys.argv:
220 logging.getLogger().setLevel(logging.INFO)
221
222 args = sys.argv[1:]
223 engine_override, recipes_cfg_path = parse_args(args)
224
225 if recipes_cfg_path:
226 # calculate repo_root from recipes_cfg_path
227 repo_root = os.path.dirname(
228 os.path.dirname(os.path.dirname(recipes_cfg_path)))
229 else:
230 # find repo_root with git and calculate recipes_cfg_path
231 repo_root = (
232 _git_output(['rev-parse', '--show-toplevel'],
233 cwd=os.path.abspath(os.path.dirname(__file__))).strip())
234 repo_root = os.path.abspath(repo_root)
235 recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg')
236 args = ['--package', recipes_cfg_path] + args
237
238 engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path)
239
240 try:
241 return _subprocess_call(
242 [VPYTHON, '-u',
243 os.path.join(engine_path, 'recipe_engine', 'main.py')] + args)
244 except KeyboardInterrupt:
245 return 1
246
247
248if __name__ == '__main__':
249 sys.exit(main())