blob: 8250010a6ae845eb548d12aede1a7c46983a8793 [file] [log] [blame]
recipe-rollerf8a03292019-05-17 13:55:55 -07001#!/bin/sh
recipe-roller18f0e712019-05-17 15:36:20 -07002# 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
recipe-rollerf8a03292019-05-17 13:55:55 -07006# 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-roller8b808942021-09-17 12:58:02 -07009# /usr/bin/env: 'python3 -u': No such file or directory
recipe-rollerf8a03292019-05-17 13:55:55 -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.
recipe-roller18f0e712019-05-17 15:36:20 -070013# pylint: disable=pointless-string-statement
recipe-roller8b808942021-09-17 12:58:02 -070014''''exec python3 -u -- "$0" ${1+"$@"} # '''
recipe-rollerf8a03292019-05-17 13:55:55 -070015# vi: syntax=python
Lann Martin079e5aa2018-10-29 12:24:54 -060016"""Bootstrap script to clone and forward to the recipe engine tool.
17
18*******************
19** DO NOT MODIFY **
20*******************
21
recipe-roller4e218f22021-05-12 03:38:23 -070022This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/main/recipes.py.
Lann Martin079e5aa2018-10-29 12:24:54 -060023To fix bugs, fix in the googlesource repo then run the autoroller.
24"""
25
recipe-rollerf8a03292019-05-17 13:55:55 -070026# pylint: disable=wrong-import-position
Lann Martin079e5aa2018-10-29 12:24:54 -060027import argparse
Prathmesh Prabhu9bc353d2020-07-21 15:35:55 -070028import errno
Lann Martin079e5aa2018-10-29 12:24:54 -060029import json
30import logging
31import os
Lann Martin079e5aa2018-10-29 12:24:54 -060032import subprocess
33import sys
Lann Martin079e5aa2018-10-29 12:24:54 -060034
35from collections import namedtuple
recipe-roller340874e2021-08-25 12:05:15 -070036from io import open # pylint: disable=redefined-builtin
Lann Martin079e5aa2018-10-29 12:24:54 -060037
recipe-roller7b1a0182020-12-08 13:02:09 -080038try:
39 import urllib.parse as urlparse
40except ImportError:
41 import urlparse
42
Lann Martin079e5aa2018-10-29 12:24:54 -060043# The dependency entry for the recipe_engine in the client repo's recipes.cfg
44#
45# url (str) - the url to the engine repo we want to use.
46# revision (str) - the git revision for the engine to get.
Lann Martin079e5aa2018-10-29 12:24:54 -060047# branch (str) - the branch to fetch for the engine as an absolute ref (e.g.
recipe-roller4e218f22021-05-12 03:38:23 -070048# refs/heads/main)
recipe-roller7107df22019-05-22 16:47:18 -070049EngineDep = namedtuple('EngineDep', 'url revision branch')
Lann Martin079e5aa2018-10-29 12:24:54 -060050
51
52class MalformedRecipesCfg(Exception):
recipe-roller7107df22019-05-22 16:47:18 -070053
Lann Martin079e5aa2018-10-29 12:24:54 -060054 def __init__(self, msg, path):
recipe-roller7107df22019-05-22 16:47:18 -070055 full_message = 'malformed recipes.cfg: %s: %r' % (msg, path)
56 super(MalformedRecipesCfg, self).__init__(full_message)
Lann Martin079e5aa2018-10-29 12:24:54 -060057
58
59def parse(repo_root, recipes_cfg_path):
60 """Parse is a lightweight a recipes.cfg file parser.
61
62 Args:
63 repo_root (str) - native path to the root of the repo we're trying to run
64 recipes for.
65 recipes_cfg_path (str) - native path to the recipes.cfg file to process.
66
67 Returns (as tuple):
68 engine_dep (EngineDep|None): The recipe_engine dependency, or None, if the
69 current repo IS the recipe_engine.
70 recipes_path (str) - native path to where the recipes live inside of the
71 current repo (i.e. the folder containing `recipes/` and/or
72 `recipe_modules`)
73 """
recipe-roller340874e2021-08-25 12:05:15 -070074 with open(recipes_cfg_path, 'r') as fh:
Lann Martin079e5aa2018-10-29 12:24:54 -060075 pb = json.load(fh)
76
77 try:
78 if pb['api_version'] != 2:
79 raise MalformedRecipesCfg('unknown version %d' % pb['api_version'],
80 recipes_cfg_path)
81
Yaakov Shaul3114e172019-02-05 21:45:16 -070082 # If we're running ./recipes.py from the recipe_engine repo itself, then
Lann Martin079e5aa2018-10-29 12:24:54 -060083 # return None to signal that there's no EngineDep.
Lann Martinee0b13d2019-03-05 12:52:28 -070084 repo_name = pb.get('repo_name')
85 if not repo_name:
86 repo_name = pb['project_id']
87 if repo_name == 'recipe_engine':
Lann Martin079e5aa2018-10-29 12:24:54 -060088 return None, pb.get('recipes_path', '')
89
90 engine = pb['deps']['recipe_engine']
91
92 if 'url' not in engine:
93 raise MalformedRecipesCfg(
recipe-roller7107df22019-05-22 16:47:18 -070094 'Required field "url" in dependency "recipe_engine" not found',
95 recipes_cfg_path)
Lann Martin079e5aa2018-10-29 12:24:54 -060096
97 engine.setdefault('revision', '')
recipe-roller4e218f22021-05-12 03:38:23 -070098 engine.setdefault('branch', 'refs/heads/main')
Lann Martin079e5aa2018-10-29 12:24:54 -060099 recipes_path = pb.get('recipes_path', '')
100
101 # TODO(iannucci): only support absolute refs
102 if not engine['branch'].startswith('refs/'):
103 engine['branch'] = 'refs/heads/' + engine['branch']
104
recipe-roller7107df22019-05-22 16:47:18 -0700105 recipes_path = os.path.join(repo_root,
106 recipes_path.replace('/', os.path.sep))
Lann Martin079e5aa2018-10-29 12:24:54 -0600107 return EngineDep(**engine), recipes_path
108 except KeyError as ex:
recipe-rollercc26f2d2021-10-04 06:17:08 -0700109 raise MalformedRecipesCfg(str(ex), recipes_cfg_path)
Lann Martin079e5aa2018-10-29 12:24:54 -0600110
111
recipe-roller4202bc62020-09-17 17:38:22 -0700112IS_WIN = sys.platform.startswith(('win', 'cygwin'))
113
114_BAT = '.bat' if IS_WIN else ''
Lann Martin079e5aa2018-10-29 12:24:54 -0600115GIT = 'git' + _BAT
recipe-rollera5c38632021-06-17 10:50:50 -0700116VPYTHON = ('vpython' +
117 ('3' if os.getenv('RECIPES_USE_PY3') == 'true' else '') +
118 _BAT)
recipe-rollera2fcee52019-04-09 08:22:38 -0700119CIPD = 'cipd' + _BAT
120REQUIRED_BINARIES = {GIT, VPYTHON, CIPD}
121
122
123def _is_executable(path):
124 return os.path.isfile(path) and os.access(path, os.X_OK)
125
recipe-roller7107df22019-05-22 16:47:18 -0700126
recipe-rollera2fcee52019-04-09 08:22:38 -0700127# TODO: Use shutil.which once we switch to Python3.
128def _is_on_path(basename):
129 for path in os.environ['PATH'].split(os.pathsep):
130 full_path = os.path.join(path, basename)
131 if _is_executable(full_path):
132 return True
133 return False
Lann Martin079e5aa2018-10-29 12:24:54 -0600134
135
136def _subprocess_call(argv, **kwargs):
137 logging.info('Running %r', argv)
138 return subprocess.call(argv, **kwargs)
139
140
141def _git_check_call(argv, **kwargs):
recipe-roller7107df22019-05-22 16:47:18 -0700142 argv = [GIT] + argv
Lann Martin079e5aa2018-10-29 12:24:54 -0600143 logging.info('Running %r', argv)
144 subprocess.check_call(argv, **kwargs)
145
146
147def _git_output(argv, **kwargs):
recipe-roller7107df22019-05-22 16:47:18 -0700148 argv = [GIT] + argv
Lann Martin079e5aa2018-10-29 12:24:54 -0600149 logging.info('Running %r', argv)
150 return subprocess.check_output(argv, **kwargs)
151
152
153def parse_args(argv):
154 """This extracts a subset of the arguments that this bootstrap script cares
155 about. Currently this consists of:
Yaakov Shaul3114e172019-02-05 21:45:16 -0700156 * an override for the recipe engine in the form of `-O recipe_engine=/path`
Lann Martin079e5aa2018-10-29 12:24:54 -0600157 * the --package option.
158 """
159 PREFIX = 'recipe_engine='
160
161 p = argparse.ArgumentParser(add_help=False)
162 p.add_argument('-O', '--project-override', action='append')
163 p.add_argument('--package', type=os.path.abspath)
164 args, _ = p.parse_known_args(argv)
165 for override in args.project_override or ():
166 if override.startswith(PREFIX):
167 return override[len(PREFIX):], args.package
168 return None, args.package
169
170
171def checkout_engine(engine_path, repo_root, recipes_cfg_path):
172 dep, recipes_path = parse(repo_root, recipes_cfg_path)
173 if dep is None:
174 # we're running from the engine repo already!
175 return os.path.join(repo_root, recipes_path)
176
177 url = dep.url
178
179 if not engine_path and url.startswith('file://'):
180 engine_path = urlparse.urlparse(url).path
181
182 if not engine_path:
183 revision = dep.revision
Lann Martin079e5aa2018-10-29 12:24:54 -0600184 branch = dep.branch
185
186 # Ensure that we have the recipe engine cloned.
Yaakov Shaul3114e172019-02-05 21:45:16 -0700187 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
Lann Martin079e5aa2018-10-29 12:24:54 -0600188
189 with open(os.devnull, 'w') as NUL:
190 # Note: this logic mirrors the logic in recipe_engine/fetch.py
Yaakov Shaul3114e172019-02-05 21:45:16 -0700191 _git_check_call(['init', engine_path], stdout=NUL)
Lann Martin079e5aa2018-10-29 12:24:54 -0600192
193 try:
recipe-roller7107df22019-05-22 16:47:18 -0700194 _git_check_call(['rev-parse', '--verify',
195 '%s^{commit}' % revision],
196 cwd=engine_path,
197 stdout=NUL,
198 stderr=NUL)
Lann Martin079e5aa2018-10-29 12:24:54 -0600199 except subprocess.CalledProcessError:
recipe-roller7107df22019-05-22 16:47:18 -0700200 _git_check_call(['fetch', url, branch],
201 cwd=engine_path,
202 stdout=NUL,
Lann Martin079e5aa2018-10-29 12:24:54 -0600203 stderr=NUL)
204
205 try:
Yaakov Shaul3114e172019-02-05 21:45:16 -0700206 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
Lann Martin079e5aa2018-10-29 12:24:54 -0600207 except subprocess.CalledProcessError:
Prathmesh Prabhu9bc353d2020-07-21 15:35:55 -0700208 index_lock = os.path.join(engine_path, '.git', 'index.lock')
209 try:
210 os.remove(index_lock)
211 except OSError as exc:
recipe-roller21d69132020-10-01 01:58:08 -0700212 if exc.errno != errno.ENOENT:
recipe-roller3663a432020-09-20 12:05:19 -0700213 logging.warn('failed to remove %r, reset will fail: %s', index_lock,
214 exc)
Yaakov Shaul3114e172019-02-05 21:45:16 -0700215 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
Lann Martin079e5aa2018-10-29 12:24:54 -0600216
recipe-roller149c1572019-03-28 15:53:36 -0700217 # If the engine has refactored/moved modules we need to clean all .pyc files
218 # or things will get squirrely.
219 _git_check_call(['clean', '-qxf'], cwd=engine_path)
220
Lann Martin079e5aa2018-10-29 12:24:54 -0600221 return engine_path
222
223
224def main():
recipe-rollera2fcee52019-04-09 08:22:38 -0700225 for required_binary in REQUIRED_BINARIES:
226 if not _is_on_path(required_binary):
227 return 'Required binary is not found on PATH: %s' % required_binary
228
Lann Martin079e5aa2018-10-29 12:24:54 -0600229 if '--verbose' in sys.argv:
230 logging.getLogger().setLevel(logging.INFO)
231
232 args = sys.argv[1:]
233 engine_override, recipes_cfg_path = parse_args(args)
234
235 if recipes_cfg_path:
236 # calculate repo_root from recipes_cfg_path
237 repo_root = os.path.dirname(
recipe-roller7107df22019-05-22 16:47:18 -0700238 os.path.dirname(os.path.dirname(recipes_cfg_path)))
Lann Martin079e5aa2018-10-29 12:24:54 -0600239 else:
240 # find repo_root with git and calculate recipes_cfg_path
recipe-roller7107df22019-05-22 16:47:18 -0700241 repo_root = (
242 _git_output(['rev-parse', '--show-toplevel'],
243 cwd=os.path.abspath(os.path.dirname(__file__))).strip())
recipe-roller7b1a0182020-12-08 13:02:09 -0800244 repo_root = os.path.abspath(repo_root).decode()
Lann Martin079e5aa2018-10-29 12:24:54 -0600245 recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg')
246 args = ['--package', recipes_cfg_path] + args
Lann Martin079e5aa2018-10-29 12:24:54 -0600247 engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path)
248
recipe-roller4202bc62020-09-17 17:38:22 -0700249 argv = (
recipe-roller3663a432020-09-20 12:05:19 -0700250 [VPYTHON, '-u',
251 os.path.join(engine_path, 'recipe_engine', 'main.py')] + args)
recipe-roller4202bc62020-09-17 17:38:22 -0700252
253 if IS_WIN:
254 # No real 'exec' on windows; set these signals to ignore so that they
255 # propagate to our children but we still wait for the child process to quit.
recipe-roller51850712021-03-09 08:25:47 -0800256 import signal
recipe-roller4202bc62020-09-17 17:38:22 -0700257 signal.signal(signal.SIGBREAK, signal.SIG_IGN)
258 signal.signal(signal.SIGINT, signal.SIG_IGN)
259 signal.signal(signal.SIGTERM, signal.SIG_IGN)
260 return _subprocess_call(argv)
261 else:
262 os.execvp(argv[0], argv)
Lann Martin079e5aa2018-10-29 12:24:54 -0600263
recipe-roller978cd712020-03-30 12:16:42 -0700264
Lann Martin079e5aa2018-10-29 12:24:54 -0600265if __name__ == '__main__':
266 sys.exit(main())