Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 1 | #!/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-roller | a4579cb | 2021-03-18 13:14:58 -0700 | [diff] [blame] | 9 | # /usr/bin/env: 'python2 -u': No such file or directory |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -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. |
| 13 | # pylint: disable=pointless-string-statement |
recipe-roller | a4579cb | 2021-03-18 13:14:58 -0700 | [diff] [blame] | 14 | ''''exec python2 -u -- "$0" ${1+"$@"} # ''' |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 15 | # vi: syntax=python |
| 16 | """Bootstrap script to clone and forward to the recipe engine tool. |
| 17 | |
| 18 | ******************* |
| 19 | ** DO NOT MODIFY ** |
| 20 | ******************* |
| 21 | |
recipe-roller | 27c9c4f | 2021-05-11 10:23:41 -0700 | [diff] [blame] | 22 | This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/main/recipes.py. |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 23 | To fix bugs, fix in the googlesource repo then run the autoroller. |
| 24 | """ |
| 25 | |
| 26 | # pylint: disable=wrong-import-position |
| 27 | import argparse |
Prathmesh Prabhu | c81b4a4 | 2020-07-21 13:25:02 -0700 | [diff] [blame] | 28 | import errno |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 29 | import json |
| 30 | import logging |
| 31 | import os |
| 32 | import subprocess |
| 33 | import sys |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 34 | |
| 35 | from collections import namedtuple |
| 36 | |
recipe-roller | 4571aa1 | 2020-12-08 17:01:33 -0800 | [diff] [blame] | 37 | try: |
| 38 | import urllib.parse as urlparse |
| 39 | except ImportError: |
| 40 | import urlparse |
| 41 | |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 42 | # 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-roller | 27c9c4f | 2021-05-11 10:23:41 -0700 | [diff] [blame] | 47 | # refs/heads/main) |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 48 | EngineDep = namedtuple('EngineDep', 'url revision branch') |
| 49 | |
| 50 | |
| 51 | class 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 | |
| 58 | def 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-roller | 27c9c4f | 2021-05-11 10:23:41 -0700 | [diff] [blame] | 97 | engine.setdefault('branch', 'refs/heads/main') |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 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 Prabhu | 98066ba | 2020-09-11 10:52:16 -0700 | [diff] [blame] | 111 | IS_WIN = sys.platform.startswith(('win', 'cygwin')) |
| 112 | |
| 113 | _BAT = '.bat' if IS_WIN else '' |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 114 | GIT = 'git' + _BAT |
recipe-roller | 2c013a8 | 2021-06-15 14:08:42 -0700 | [diff] [blame] | 115 | VPYTHON = ('vpython' + |
| 116 | ('3' if os.getenv('RECIPES_USE_PY3') == 'true' else '') + |
| 117 | _BAT) |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 118 | CIPD = 'cipd' + _BAT |
| 119 | REQUIRED_BINARIES = {GIT, VPYTHON, CIPD} |
| 120 | |
| 121 | |
| 122 | def _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. |
| 127 | def _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 | |
| 135 | def _subprocess_call(argv, **kwargs): |
| 136 | logging.info('Running %r', argv) |
| 137 | return subprocess.call(argv, **kwargs) |
| 138 | |
| 139 | |
| 140 | def _git_check_call(argv, **kwargs): |
| 141 | argv = [GIT] + argv |
| 142 | logging.info('Running %r', argv) |
| 143 | subprocess.check_call(argv, **kwargs) |
| 144 | |
| 145 | |
| 146 | def _git_output(argv, **kwargs): |
| 147 | argv = [GIT] + argv |
| 148 | logging.info('Running %r', argv) |
| 149 | return subprocess.check_output(argv, **kwargs) |
| 150 | |
| 151 | |
| 152 | def 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 | |
| 170 | def 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 Prabhu | c81b4a4 | 2020-07-21 13:25:02 -0700 | [diff] [blame] | 207 | index_lock = os.path.join(engine_path, '.git', 'index.lock') |
| 208 | try: |
| 209 | os.remove(index_lock) |
| 210 | except OSError as exc: |
recipe-roller | 05f0164 | 2020-10-01 01:16:46 -0700 | [diff] [blame] | 211 | if exc.errno != errno.ENOENT: |
recipe-roller | 8396f73 | 2020-09-18 14:14:10 -0700 | [diff] [blame] | 212 | logging.warn('failed to remove %r, reset will fail: %s', index_lock, |
| 213 | exc) |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 214 | _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 | |
| 223 | def 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-roller | 4571aa1 | 2020-12-08 17:01:33 -0800 | [diff] [blame] | 243 | repo_root = os.path.abspath(repo_root).decode() |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 244 | recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg') |
| 245 | args = ['--package', recipes_cfg_path] + args |
Prathmesh Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 246 | engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path) |
| 247 | |
Prathmesh Prabhu | 98066ba | 2020-09-11 10:52:16 -0700 | [diff] [blame] | 248 | argv = ( |
recipe-roller | 8396f73 | 2020-09-18 14:14:10 -0700 | [diff] [blame] | 249 | [VPYTHON, '-u', |
| 250 | os.path.join(engine_path, 'recipe_engine', 'main.py')] + args) |
Prathmesh Prabhu | 98066ba | 2020-09-11 10:52:16 -0700 | [diff] [blame] | 251 | |
| 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-roller | d816478 | 2021-03-09 07:53:38 -0800 | [diff] [blame] | 255 | import signal |
Prathmesh Prabhu | 98066ba | 2020-09-11 10:52:16 -0700 | [diff] [blame] | 256 | 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 Prabhu | fd3d313 | 2020-03-20 12:09:50 -0700 | [diff] [blame] | 262 | |
| 263 | |
| 264 | if __name__ == '__main__': |
| 265 | sys.exit(main()) |