blob: 9804a4c320624fcbac2eaacbc92f7987b5955cdf [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
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
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.
47# refs/heads/master)
48EngineDep = 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', '')
97 engine.setdefault('branch', 'refs/heads/master')
98 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
115VPYTHON = 'vpython' + _BAT
116CIPD = 'cipd' + _BAT
117REQUIRED_BINARIES = {GIT, VPYTHON, CIPD}
118
119
120def _is_executable(path):
121 return os.path.isfile(path) and os.access(path, os.X_OK)
122
123
124# TODO: Use shutil.which once we switch to Python3.
125def _is_on_path(basename):
126 for path in os.environ['PATH'].split(os.pathsep):
127 full_path = os.path.join(path, basename)
128 if _is_executable(full_path):
129 return True
130 return False
131
132
133def _subprocess_call(argv, **kwargs):
134 logging.info('Running %r', argv)
135 return subprocess.call(argv, **kwargs)
136
137
138def _git_check_call(argv, **kwargs):
139 argv = [GIT] + argv
140 logging.info('Running %r', argv)
141 subprocess.check_call(argv, **kwargs)
142
143
144def _git_output(argv, **kwargs):
145 argv = [GIT] + argv
146 logging.info('Running %r', argv)
147 return subprocess.check_output(argv, **kwargs)
148
149
150def parse_args(argv):
151 """This extracts a subset of the arguments that this bootstrap script cares
152 about. Currently this consists of:
153 * an override for the recipe engine in the form of `-O recipe_engine=/path`
154 * the --package option.
155 """
156 PREFIX = 'recipe_engine='
157
158 p = argparse.ArgumentParser(add_help=False)
159 p.add_argument('-O', '--project-override', action='append')
160 p.add_argument('--package', type=os.path.abspath)
161 args, _ = p.parse_known_args(argv)
162 for override in args.project_override or ():
163 if override.startswith(PREFIX):
164 return override[len(PREFIX):], args.package
165 return None, args.package
166
167
168def checkout_engine(engine_path, repo_root, recipes_cfg_path):
169 dep, recipes_path = parse(repo_root, recipes_cfg_path)
170 if dep is None:
171 # we're running from the engine repo already!
172 return os.path.join(repo_root, recipes_path)
173
174 url = dep.url
175
176 if not engine_path and url.startswith('file://'):
177 engine_path = urlparse.urlparse(url).path
178
179 if not engine_path:
180 revision = dep.revision
181 branch = dep.branch
182
183 # Ensure that we have the recipe engine cloned.
184 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
185
186 with open(os.devnull, 'w') as NUL:
187 # Note: this logic mirrors the logic in recipe_engine/fetch.py
188 _git_check_call(['init', engine_path], stdout=NUL)
189
190 try:
191 _git_check_call(['rev-parse', '--verify',
192 '%s^{commit}' % revision],
193 cwd=engine_path,
194 stdout=NUL,
195 stderr=NUL)
196 except subprocess.CalledProcessError:
197 _git_check_call(['fetch', url, branch],
198 cwd=engine_path,
199 stdout=NUL,
200 stderr=NUL)
201
202 try:
203 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
204 except subprocess.CalledProcessError:
Prathmesh Prabhuc81b4a42020-07-21 13:25:02 -0700205 index_lock = os.path.join(engine_path, '.git', 'index.lock')
206 try:
207 os.remove(index_lock)
208 except OSError as exc:
recipe-roller05f01642020-10-01 01:16:46 -0700209 if exc.errno != errno.ENOENT:
recipe-roller8396f732020-09-18 14:14:10 -0700210 logging.warn('failed to remove %r, reset will fail: %s', index_lock,
211 exc)
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700212 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
213
214 # If the engine has refactored/moved modules we need to clean all .pyc files
215 # or things will get squirrely.
216 _git_check_call(['clean', '-qxf'], cwd=engine_path)
217
218 return engine_path
219
220
221def main():
222 for required_binary in REQUIRED_BINARIES:
223 if not _is_on_path(required_binary):
224 return 'Required binary is not found on PATH: %s' % required_binary
225
226 if '--verbose' in sys.argv:
227 logging.getLogger().setLevel(logging.INFO)
228
229 args = sys.argv[1:]
230 engine_override, recipes_cfg_path = parse_args(args)
231
232 if recipes_cfg_path:
233 # calculate repo_root from recipes_cfg_path
234 repo_root = os.path.dirname(
235 os.path.dirname(os.path.dirname(recipes_cfg_path)))
236 else:
237 # find repo_root with git and calculate recipes_cfg_path
238 repo_root = (
239 _git_output(['rev-parse', '--show-toplevel'],
240 cwd=os.path.abspath(os.path.dirname(__file__))).strip())
recipe-roller4571aa12020-12-08 17:01:33 -0800241 repo_root = os.path.abspath(repo_root).decode()
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700242 recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg')
243 args = ['--package', recipes_cfg_path] + args
244
245 engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path)
246
Prathmesh Prabhu98066ba2020-09-11 10:52:16 -0700247 argv = (
recipe-roller8396f732020-09-18 14:14:10 -0700248 [VPYTHON, '-u',
249 os.path.join(engine_path, 'recipe_engine', 'main.py')] + args)
Prathmesh Prabhu98066ba2020-09-11 10:52:16 -0700250
251 if IS_WIN:
252 # No real 'exec' on windows; set these signals to ignore so that they
253 # propagate to our children but we still wait for the child process to quit.
recipe-rollerd8164782021-03-09 07:53:38 -0800254 import signal
Prathmesh Prabhu98066ba2020-09-11 10:52:16 -0700255 signal.signal(signal.SIGBREAK, signal.SIG_IGN)
256 signal.signal(signal.SIGINT, signal.SIG_IGN)
257 signal.signal(signal.SIGTERM, signal.SIG_IGN)
258 return _subprocess_call(argv)
259 else:
260 os.execvp(argv[0], argv)
Prathmesh Prabhufd3d3132020-03-20 12:09:50 -0700261
262
263if __name__ == '__main__':
264 sys.exit(main())