blob: 8d550e8b85cc271c7c9cbd66a8c39b849d8b0973 [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#
recipe-rollera4579cb2021-03-18 13:14:58 -07009# /usr/bin/env: 'python2 -u': No such file or directory
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070010#
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
recipe-rollera4579cb2021-03-18 13:14:58 -070014''''exec python2 -u -- "$0" ${1+"$@"} # '''
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070015# vi: syntax=python
16"""Bootstrap script to clone and forward to the recipe engine tool.
17
18*******************
19** DO NOT MODIFY **
20*******************
21
recipe-roller27c9c4f2021-05-11 10:23:41 -070022This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/main/recipes.py.
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070023To 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
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070034
35from collections import namedtuple
36
recipe-roller4571aa12020-12-08 17:01:33 -080037try:
38 import urllib.parse as urlparse
39except ImportError:
40 import urlparse
41
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070042# The dependency entry for the recipe_engine in the client repo's recipes.cfg
43#
44# url (str) - the url to the engine repo we want to use.
45# revision (str) - the git revision for the engine to get.
46# branch (str) - the branch to fetch for the engine as an absolute ref (e.g.
recipe-roller27c9c4f2021-05-11 10:23:41 -070047# refs/heads/main)
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070048EngineDep = namedtuple('EngineDep', 'url revision branch')
49
50
51class MalformedRecipesCfg(Exception):
52
53 def __init__(self, msg, path):
54 full_message = 'malformed recipes.cfg: %s: %r' % (msg, path)
55 super(MalformedRecipesCfg, self).__init__(full_message)
56
57
58def parse(repo_root, recipes_cfg_path):
59 """Parse is a lightweight a recipes.cfg file parser.
60
61 Args:
62 repo_root (str) - native path to the root of the repo we're trying to run
63 recipes for.
64 recipes_cfg_path (str) - native path to the recipes.cfg file to process.
65
66 Returns (as tuple):
67 engine_dep (EngineDep|None): The recipe_engine dependency, or None, if the
68 current repo IS the recipe_engine.
69 recipes_path (str) - native path to where the recipes live inside of the
70 current repo (i.e. the folder containing `recipes/` and/or
71 `recipe_modules`)
72 """
73 with open(recipes_cfg_path, 'rU') as fh:
74 pb = json.load(fh)
75
76 try:
77 if pb['api_version'] != 2:
78 raise MalformedRecipesCfg('unknown version %d' % pb['api_version'],
79 recipes_cfg_path)
80
81 # If we're running ./recipes.py from the recipe_engine repo itself, then
82 # return None to signal that there's no EngineDep.
83 repo_name = pb.get('repo_name')
84 if not repo_name:
85 repo_name = pb['project_id']
86 if repo_name == 'recipe_engine':
87 return None, pb.get('recipes_path', '')
88
89 engine = pb['deps']['recipe_engine']
90
91 if 'url' not in engine:
92 raise MalformedRecipesCfg(
93 'Required field "url" in dependency "recipe_engine" not found',
94 recipes_cfg_path)
95
96 engine.setdefault('revision', '')
recipe-roller27c9c4f2021-05-11 10:23:41 -070097 engine.setdefault('branch', 'refs/heads/main')
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -070098 recipes_path = pb.get('recipes_path', '')
99
100 # TODO(iannucci): only support absolute refs
101 if not engine['branch'].startswith('refs/'):
102 engine['branch'] = 'refs/heads/' + engine['branch']
103
104 recipes_path = os.path.join(repo_root,
105 recipes_path.replace('/', os.path.sep))
106 return EngineDep(**engine), recipes_path
107 except KeyError as ex:
108 raise MalformedRecipesCfg(ex.message, recipes_cfg_path)
109
110
Prathmesh Prabhu98066ba2020-09-11 10:52:16 -0700111IS_WIN = sys.platform.startswith(('win', 'cygwin'))
112
113_BAT = '.bat' if IS_WIN else ''
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700114GIT = 'git' + _BAT
recipe-roller2c013a82021-06-15 14:08:42 -0700115VPYTHON = ('vpython' +
116 ('3' if os.getenv('RECIPES_USE_PY3') == 'true' else '') +
117 _BAT)
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700118CIPD = 'cipd' + _BAT
119REQUIRED_BINARIES = {GIT, VPYTHON, CIPD}
120
121
122def _is_executable(path):
123 return os.path.isfile(path) and os.access(path, os.X_OK)
124
125
126# TODO: Use shutil.which once we switch to Python3.
127def _is_on_path(basename):
128 for path in os.environ['PATH'].split(os.pathsep):
129 full_path = os.path.join(path, basename)
130 if _is_executable(full_path):
131 return True
132 return False
133
134
135def _subprocess_call(argv, **kwargs):
136 logging.info('Running %r', argv)
137 return subprocess.call(argv, **kwargs)
138
139
140def _git_check_call(argv, **kwargs):
141 argv = [GIT] + argv
142 logging.info('Running %r', argv)
143 subprocess.check_call(argv, **kwargs)
144
145
146def _git_output(argv, **kwargs):
147 argv = [GIT] + argv
148 logging.info('Running %r', argv)
149 return subprocess.check_output(argv, **kwargs)
150
151
152def parse_args(argv):
153 """This extracts a subset of the arguments that this bootstrap script cares
154 about. Currently this consists of:
155 * an override for the recipe engine in the form of `-O recipe_engine=/path`
156 * the --package option.
157 """
158 PREFIX = 'recipe_engine='
159
160 p = argparse.ArgumentParser(add_help=False)
161 p.add_argument('-O', '--project-override', action='append')
162 p.add_argument('--package', type=os.path.abspath)
163 args, _ = p.parse_known_args(argv)
164 for override in args.project_override or ():
165 if override.startswith(PREFIX):
166 return override[len(PREFIX):], args.package
167 return None, args.package
168
169
170def checkout_engine(engine_path, repo_root, recipes_cfg_path):
171 dep, recipes_path = parse(repo_root, recipes_cfg_path)
172 if dep is None:
173 # we're running from the engine repo already!
174 return os.path.join(repo_root, recipes_path)
175
176 url = dep.url
177
178 if not engine_path and url.startswith('file://'):
179 engine_path = urlparse.urlparse(url).path
180
181 if not engine_path:
182 revision = dep.revision
183 branch = dep.branch
184
185 # Ensure that we have the recipe engine cloned.
186 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
187
188 with open(os.devnull, 'w') as NUL:
189 # Note: this logic mirrors the logic in recipe_engine/fetch.py
190 _git_check_call(['init', engine_path], stdout=NUL)
191
192 try:
193 _git_check_call(['rev-parse', '--verify',
194 '%s^{commit}' % revision],
195 cwd=engine_path,
196 stdout=NUL,
197 stderr=NUL)
198 except subprocess.CalledProcessError:
199 _git_check_call(['fetch', url, branch],
200 cwd=engine_path,
201 stdout=NUL,
202 stderr=NUL)
203
204 try:
205 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
206 except subprocess.CalledProcessError:
Prathmesh Prabhuc81b4a42020-07-21 13:25:02 -0700207 index_lock = os.path.join(engine_path, '.git', 'index.lock')
208 try:
209 os.remove(index_lock)
210 except OSError as exc:
recipe-roller05f01642020-10-01 01:16:46 -0700211 if exc.errno != errno.ENOENT:
recipe-roller8396f732020-09-18 14:14:10 -0700212 logging.warn('failed to remove %r, reset will fail: %s', index_lock,
213 exc)
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700214 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
215
216 # If the engine has refactored/moved modules we need to clean all .pyc files
217 # or things will get squirrely.
218 _git_check_call(['clean', '-qxf'], cwd=engine_path)
219
220 return engine_path
221
222
223def main():
224 for required_binary in REQUIRED_BINARIES:
225 if not _is_on_path(required_binary):
226 return 'Required binary is not found on PATH: %s' % required_binary
227
228 if '--verbose' in sys.argv:
229 logging.getLogger().setLevel(logging.INFO)
230
231 args = sys.argv[1:]
232 engine_override, recipes_cfg_path = parse_args(args)
233
234 if recipes_cfg_path:
235 # calculate repo_root from recipes_cfg_path
236 repo_root = os.path.dirname(
237 os.path.dirname(os.path.dirname(recipes_cfg_path)))
238 else:
239 # find repo_root with git and calculate recipes_cfg_path
240 repo_root = (
241 _git_output(['rev-parse', '--show-toplevel'],
242 cwd=os.path.abspath(os.path.dirname(__file__))).strip())
recipe-roller4571aa12020-12-08 17:01:33 -0800243 repo_root = os.path.abspath(repo_root).decode()
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700244 recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg')
245 args = ['--package', recipes_cfg_path] + args
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700246 engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path)
247
Prathmesh Prabhu98066ba2020-09-11 10:52:16 -0700248 argv = (
recipe-roller8396f732020-09-18 14:14:10 -0700249 [VPYTHON, '-u',
250 os.path.join(engine_path, 'recipe_engine', 'main.py')] + args)
Prathmesh Prabhu98066ba2020-09-11 10:52:16 -0700251
252 if IS_WIN:
253 # No real 'exec' on windows; set these signals to ignore so that they
254 # propagate to our children but we still wait for the child process to quit.
recipe-rollerd8164782021-03-09 07:53:38 -0800255 import signal
Prathmesh Prabhu98066ba2020-09-11 10:52:16 -0700256 signal.signal(signal.SIGBREAK, signal.SIG_IGN)
257 signal.signal(signal.SIGINT, signal.SIG_IGN)
258 signal.signal(signal.SIGTERM, signal.SIG_IGN)
259 return _subprocess_call(argv)
260 else:
261 os.execvp(argv[0], argv)
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700262
263
264if __name__ == '__main__':
265 sys.exit(main())