recipe-roller | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 1 | #!/bin/sh |
recipe-roller | 18f0e71 | 2019-05-17 15:36:20 -0700 | [diff] [blame] | 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 | |
recipe-roller | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 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-roller | 8b80894 | 2021-09-17 12:58:02 -0700 | [diff] [blame] | 9 | # /usr/bin/env: 'python3 -u': No such file or directory |
recipe-roller | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 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. |
recipe-roller | 18f0e71 | 2019-05-17 15:36:20 -0700 | [diff] [blame] | 13 | # pylint: disable=pointless-string-statement |
recipe-roller | 8b80894 | 2021-09-17 12:58:02 -0700 | [diff] [blame] | 14 | ''''exec python3 -u -- "$0" ${1+"$@"} # ''' |
recipe-roller | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 15 | # vi: syntax=python |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 16 | """Bootstrap script to clone and forward to the recipe engine tool. |
| 17 | |
| 18 | ******************* |
| 19 | ** DO NOT MODIFY ** |
| 20 | ******************* |
| 21 | |
recipe-roller | 4e218f2 | 2021-05-12 03:38:23 -0700 | [diff] [blame] | 22 | This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/main/recipes.py. |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 23 | To fix bugs, fix in the googlesource repo then run the autoroller. |
| 24 | """ |
| 25 | |
recipe-roller | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 26 | # pylint: disable=wrong-import-position |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 27 | import argparse |
Prathmesh Prabhu | 9bc353d | 2020-07-21 15:35:55 -0700 | [diff] [blame] | 28 | import errno |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 29 | import json |
| 30 | import logging |
| 31 | import os |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 32 | import subprocess |
| 33 | import sys |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 34 | |
| 35 | from collections import namedtuple |
recipe-roller | 340874e | 2021-08-25 12:05:15 -0700 | [diff] [blame] | 36 | from io import open # pylint: disable=redefined-builtin |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 37 | |
recipe-roller | 7b1a018 | 2020-12-08 13:02:09 -0800 | [diff] [blame] | 38 | try: |
| 39 | import urllib.parse as urlparse |
| 40 | except ImportError: |
| 41 | import urlparse |
| 42 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 43 | # 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 Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 47 | # branch (str) - the branch to fetch for the engine as an absolute ref (e.g. |
recipe-roller | 4e218f2 | 2021-05-12 03:38:23 -0700 | [diff] [blame] | 48 | # refs/heads/main) |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 49 | EngineDep = namedtuple('EngineDep', 'url revision branch') |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 50 | |
| 51 | |
| 52 | class MalformedRecipesCfg(Exception): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 53 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 54 | def __init__(self, msg, path): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 55 | full_message = 'malformed recipes.cfg: %s: %r' % (msg, path) |
| 56 | super(MalformedRecipesCfg, self).__init__(full_message) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def 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-roller | 340874e | 2021-08-25 12:05:15 -0700 | [diff] [blame] | 74 | with open(recipes_cfg_path, 'r') as fh: |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 75 | 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 Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 82 | # If we're running ./recipes.py from the recipe_engine repo itself, then |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 83 | # return None to signal that there's no EngineDep. |
Lann Martin | ee0b13d | 2019-03-05 12:52:28 -0700 | [diff] [blame] | 84 | repo_name = pb.get('repo_name') |
| 85 | if not repo_name: |
| 86 | repo_name = pb['project_id'] |
| 87 | if repo_name == 'recipe_engine': |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 88 | return None, pb.get('recipes_path', '') |
| 89 | |
| 90 | engine = pb['deps']['recipe_engine'] |
| 91 | |
| 92 | if 'url' not in engine: |
| 93 | raise MalformedRecipesCfg( |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 94 | 'Required field "url" in dependency "recipe_engine" not found', |
| 95 | recipes_cfg_path) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 96 | |
| 97 | engine.setdefault('revision', '') |
recipe-roller | 4e218f2 | 2021-05-12 03:38:23 -0700 | [diff] [blame] | 98 | engine.setdefault('branch', 'refs/heads/main') |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 99 | 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-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 105 | recipes_path = os.path.join(repo_root, |
| 106 | recipes_path.replace('/', os.path.sep)) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 107 | return EngineDep(**engine), recipes_path |
| 108 | except KeyError as ex: |
recipe-roller | cc26f2d | 2021-10-04 06:17:08 -0700 | [diff] [blame^] | 109 | raise MalformedRecipesCfg(str(ex), recipes_cfg_path) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 110 | |
| 111 | |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 112 | IS_WIN = sys.platform.startswith(('win', 'cygwin')) |
| 113 | |
| 114 | _BAT = '.bat' if IS_WIN else '' |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 115 | GIT = 'git' + _BAT |
recipe-roller | a5c3863 | 2021-06-17 10:50:50 -0700 | [diff] [blame] | 116 | VPYTHON = ('vpython' + |
| 117 | ('3' if os.getenv('RECIPES_USE_PY3') == 'true' else '') + |
| 118 | _BAT) |
recipe-roller | a2fcee5 | 2019-04-09 08:22:38 -0700 | [diff] [blame] | 119 | CIPD = 'cipd' + _BAT |
| 120 | REQUIRED_BINARIES = {GIT, VPYTHON, CIPD} |
| 121 | |
| 122 | |
| 123 | def _is_executable(path): |
| 124 | return os.path.isfile(path) and os.access(path, os.X_OK) |
| 125 | |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 126 | |
recipe-roller | a2fcee5 | 2019-04-09 08:22:38 -0700 | [diff] [blame] | 127 | # TODO: Use shutil.which once we switch to Python3. |
| 128 | def _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 Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def _subprocess_call(argv, **kwargs): |
| 137 | logging.info('Running %r', argv) |
| 138 | return subprocess.call(argv, **kwargs) |
| 139 | |
| 140 | |
| 141 | def _git_check_call(argv, **kwargs): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 142 | argv = [GIT] + argv |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 143 | logging.info('Running %r', argv) |
| 144 | subprocess.check_call(argv, **kwargs) |
| 145 | |
| 146 | |
| 147 | def _git_output(argv, **kwargs): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 148 | argv = [GIT] + argv |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 149 | logging.info('Running %r', argv) |
| 150 | return subprocess.check_output(argv, **kwargs) |
| 151 | |
| 152 | |
| 153 | def parse_args(argv): |
| 154 | """This extracts a subset of the arguments that this bootstrap script cares |
| 155 | about. Currently this consists of: |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 156 | * an override for the recipe engine in the form of `-O recipe_engine=/path` |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 157 | * 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 | |
| 171 | def 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 Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 184 | branch = dep.branch |
| 185 | |
| 186 | # Ensure that we have the recipe engine cloned. |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 187 | engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine') |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 188 | |
| 189 | with open(os.devnull, 'w') as NUL: |
| 190 | # Note: this logic mirrors the logic in recipe_engine/fetch.py |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 191 | _git_check_call(['init', engine_path], stdout=NUL) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 192 | |
| 193 | try: |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 194 | _git_check_call(['rev-parse', '--verify', |
| 195 | '%s^{commit}' % revision], |
| 196 | cwd=engine_path, |
| 197 | stdout=NUL, |
| 198 | stderr=NUL) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 199 | except subprocess.CalledProcessError: |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 200 | _git_check_call(['fetch', url, branch], |
| 201 | cwd=engine_path, |
| 202 | stdout=NUL, |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 203 | stderr=NUL) |
| 204 | |
| 205 | try: |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 206 | _git_check_call(['diff', '--quiet', revision], cwd=engine_path) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 207 | except subprocess.CalledProcessError: |
Prathmesh Prabhu | 9bc353d | 2020-07-21 15:35:55 -0700 | [diff] [blame] | 208 | index_lock = os.path.join(engine_path, '.git', 'index.lock') |
| 209 | try: |
| 210 | os.remove(index_lock) |
| 211 | except OSError as exc: |
recipe-roller | 21d6913 | 2020-10-01 01:58:08 -0700 | [diff] [blame] | 212 | if exc.errno != errno.ENOENT: |
recipe-roller | 3663a43 | 2020-09-20 12:05:19 -0700 | [diff] [blame] | 213 | logging.warn('failed to remove %r, reset will fail: %s', index_lock, |
| 214 | exc) |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 215 | _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 216 | |
recipe-roller | 149c157 | 2019-03-28 15:53:36 -0700 | [diff] [blame] | 217 | # 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 Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 221 | return engine_path |
| 222 | |
| 223 | |
| 224 | def main(): |
recipe-roller | a2fcee5 | 2019-04-09 08:22:38 -0700 | [diff] [blame] | 225 | 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 Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 229 | 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-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 238 | os.path.dirname(os.path.dirname(recipes_cfg_path))) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 239 | else: |
| 240 | # find repo_root with git and calculate recipes_cfg_path |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 241 | repo_root = ( |
| 242 | _git_output(['rev-parse', '--show-toplevel'], |
| 243 | cwd=os.path.abspath(os.path.dirname(__file__))).strip()) |
recipe-roller | 7b1a018 | 2020-12-08 13:02:09 -0800 | [diff] [blame] | 244 | repo_root = os.path.abspath(repo_root).decode() |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 245 | recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg') |
| 246 | args = ['--package', recipes_cfg_path] + args |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 247 | engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path) |
| 248 | |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 249 | argv = ( |
recipe-roller | 3663a43 | 2020-09-20 12:05:19 -0700 | [diff] [blame] | 250 | [VPYTHON, '-u', |
| 251 | os.path.join(engine_path, 'recipe_engine', 'main.py')] + args) |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 252 | |
| 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-roller | 5185071 | 2021-03-09 08:25:47 -0800 | [diff] [blame] | 256 | import signal |
recipe-roller | 4202bc6 | 2020-09-17 17:38:22 -0700 | [diff] [blame] | 257 | 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 Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 263 | |
recipe-roller | 978cd71 | 2020-03-30 12:16:42 -0700 | [diff] [blame] | 264 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 265 | if __name__ == '__main__': |
| 266 | sys.exit(main()) |