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 | # |
| 9 | # /usr/bin/env: 'python -u': No such file or directory |
| 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 | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 14 | ''''exec python -u -- "$0" ${1+"$@"} # ''' |
| 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 | |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 22 | This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/master/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 |
| 28 | import json |
| 29 | import logging |
| 30 | import os |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 31 | import subprocess |
| 32 | import sys |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 33 | import urlparse |
| 34 | |
| 35 | from collections import namedtuple |
| 36 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 37 | # The dependency entry for the recipe_engine in the client repo's recipes.cfg |
| 38 | # |
| 39 | # url (str) - the url to the engine repo we want to use. |
| 40 | # revision (str) - the git revision for the engine to get. |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 41 | # branch (str) - the branch to fetch for the engine as an absolute ref (e.g. |
| 42 | # refs/heads/master) |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 43 | EngineDep = namedtuple('EngineDep', 'url revision branch') |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 44 | |
| 45 | |
| 46 | class MalformedRecipesCfg(Exception): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 47 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 48 | def __init__(self, msg, path): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 49 | full_message = 'malformed recipes.cfg: %s: %r' % (msg, path) |
| 50 | super(MalformedRecipesCfg, self).__init__(full_message) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def parse(repo_root, recipes_cfg_path): |
| 54 | """Parse is a lightweight a recipes.cfg file parser. |
| 55 | |
| 56 | Args: |
| 57 | repo_root (str) - native path to the root of the repo we're trying to run |
| 58 | recipes for. |
| 59 | recipes_cfg_path (str) - native path to the recipes.cfg file to process. |
| 60 | |
| 61 | Returns (as tuple): |
| 62 | engine_dep (EngineDep|None): The recipe_engine dependency, or None, if the |
| 63 | current repo IS the recipe_engine. |
| 64 | recipes_path (str) - native path to where the recipes live inside of the |
| 65 | current repo (i.e. the folder containing `recipes/` and/or |
| 66 | `recipe_modules`) |
| 67 | """ |
| 68 | with open(recipes_cfg_path, 'rU') as fh: |
| 69 | pb = json.load(fh) |
| 70 | |
| 71 | try: |
| 72 | if pb['api_version'] != 2: |
| 73 | raise MalformedRecipesCfg('unknown version %d' % pb['api_version'], |
| 74 | recipes_cfg_path) |
| 75 | |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 76 | # 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] | 77 | # return None to signal that there's no EngineDep. |
Lann Martin | ee0b13d | 2019-03-05 12:52:28 -0700 | [diff] [blame] | 78 | repo_name = pb.get('repo_name') |
| 79 | if not repo_name: |
| 80 | repo_name = pb['project_id'] |
| 81 | if repo_name == 'recipe_engine': |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 82 | return None, pb.get('recipes_path', '') |
| 83 | |
| 84 | engine = pb['deps']['recipe_engine'] |
| 85 | |
| 86 | if 'url' not in engine: |
| 87 | raise MalformedRecipesCfg( |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 88 | 'Required field "url" in dependency "recipe_engine" not found', |
| 89 | recipes_cfg_path) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 90 | |
| 91 | engine.setdefault('revision', '') |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 92 | engine.setdefault('branch', 'refs/heads/master') |
| 93 | recipes_path = pb.get('recipes_path', '') |
| 94 | |
| 95 | # TODO(iannucci): only support absolute refs |
| 96 | if not engine['branch'].startswith('refs/'): |
| 97 | engine['branch'] = 'refs/heads/' + engine['branch'] |
| 98 | |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 99 | recipes_path = os.path.join(repo_root, |
| 100 | recipes_path.replace('/', os.path.sep)) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 101 | return EngineDep(**engine), recipes_path |
| 102 | except KeyError as ex: |
| 103 | raise MalformedRecipesCfg(ex.message, recipes_cfg_path) |
| 104 | |
| 105 | |
| 106 | _BAT = '.bat' if sys.platform.startswith(('win', 'cygwin')) else '' |
| 107 | GIT = 'git' + _BAT |
| 108 | VPYTHON = 'vpython' + _BAT |
recipe-roller | a2fcee5 | 2019-04-09 08:22:38 -0700 | [diff] [blame] | 109 | CIPD = 'cipd' + _BAT |
| 110 | REQUIRED_BINARIES = {GIT, VPYTHON, CIPD} |
| 111 | |
| 112 | |
| 113 | def _is_executable(path): |
| 114 | return os.path.isfile(path) and os.access(path, os.X_OK) |
| 115 | |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 116 | |
recipe-roller | a2fcee5 | 2019-04-09 08:22:38 -0700 | [diff] [blame] | 117 | # TODO: Use shutil.which once we switch to Python3. |
| 118 | def _is_on_path(basename): |
| 119 | for path in os.environ['PATH'].split(os.pathsep): |
| 120 | full_path = os.path.join(path, basename) |
| 121 | if _is_executable(full_path): |
| 122 | return True |
| 123 | return False |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 124 | |
| 125 | |
| 126 | def _subprocess_call(argv, **kwargs): |
| 127 | logging.info('Running %r', argv) |
| 128 | return subprocess.call(argv, **kwargs) |
| 129 | |
| 130 | |
| 131 | def _git_check_call(argv, **kwargs): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 132 | argv = [GIT] + argv |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 133 | logging.info('Running %r', argv) |
| 134 | subprocess.check_call(argv, **kwargs) |
| 135 | |
| 136 | |
| 137 | def _git_output(argv, **kwargs): |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 138 | argv = [GIT] + argv |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 139 | logging.info('Running %r', argv) |
| 140 | return subprocess.check_output(argv, **kwargs) |
| 141 | |
| 142 | |
| 143 | def parse_args(argv): |
| 144 | """This extracts a subset of the arguments that this bootstrap script cares |
| 145 | about. Currently this consists of: |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 146 | * 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] | 147 | * the --package option. |
| 148 | """ |
| 149 | PREFIX = 'recipe_engine=' |
| 150 | |
| 151 | p = argparse.ArgumentParser(add_help=False) |
| 152 | p.add_argument('-O', '--project-override', action='append') |
| 153 | p.add_argument('--package', type=os.path.abspath) |
| 154 | args, _ = p.parse_known_args(argv) |
| 155 | for override in args.project_override or (): |
| 156 | if override.startswith(PREFIX): |
| 157 | return override[len(PREFIX):], args.package |
| 158 | return None, args.package |
| 159 | |
| 160 | |
| 161 | def checkout_engine(engine_path, repo_root, recipes_cfg_path): |
| 162 | dep, recipes_path = parse(repo_root, recipes_cfg_path) |
| 163 | if dep is None: |
| 164 | # we're running from the engine repo already! |
| 165 | return os.path.join(repo_root, recipes_path) |
| 166 | |
| 167 | url = dep.url |
| 168 | |
| 169 | if not engine_path and url.startswith('file://'): |
| 170 | engine_path = urlparse.urlparse(url).path |
| 171 | |
| 172 | if not engine_path: |
| 173 | revision = dep.revision |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 174 | branch = dep.branch |
| 175 | |
| 176 | # Ensure that we have the recipe engine cloned. |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 177 | engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine') |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 178 | |
| 179 | with open(os.devnull, 'w') as NUL: |
| 180 | # Note: this logic mirrors the logic in recipe_engine/fetch.py |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 181 | _git_check_call(['init', engine_path], stdout=NUL) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 182 | |
| 183 | try: |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 184 | _git_check_call(['rev-parse', '--verify', |
| 185 | '%s^{commit}' % revision], |
| 186 | cwd=engine_path, |
| 187 | stdout=NUL, |
| 188 | stderr=NUL) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 189 | except subprocess.CalledProcessError: |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 190 | _git_check_call(['fetch', url, branch], |
| 191 | cwd=engine_path, |
| 192 | stdout=NUL, |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 193 | stderr=NUL) |
| 194 | |
| 195 | try: |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 196 | _git_check_call(['diff', '--quiet', revision], cwd=engine_path) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 197 | except subprocess.CalledProcessError: |
Yaakov Shaul | 3114e17 | 2019-02-05 21:45:16 -0700 | [diff] [blame] | 198 | _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 199 | |
recipe-roller | 149c157 | 2019-03-28 15:53:36 -0700 | [diff] [blame] | 200 | # If the engine has refactored/moved modules we need to clean all .pyc files |
| 201 | # or things will get squirrely. |
| 202 | _git_check_call(['clean', '-qxf'], cwd=engine_path) |
| 203 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 204 | return engine_path |
| 205 | |
| 206 | |
| 207 | def main(): |
recipe-roller | a2fcee5 | 2019-04-09 08:22:38 -0700 | [diff] [blame] | 208 | for required_binary in REQUIRED_BINARIES: |
| 209 | if not _is_on_path(required_binary): |
| 210 | return 'Required binary is not found on PATH: %s' % required_binary |
| 211 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 212 | if '--verbose' in sys.argv: |
| 213 | logging.getLogger().setLevel(logging.INFO) |
| 214 | |
| 215 | args = sys.argv[1:] |
| 216 | engine_override, recipes_cfg_path = parse_args(args) |
| 217 | |
| 218 | if recipes_cfg_path: |
| 219 | # calculate repo_root from recipes_cfg_path |
| 220 | repo_root = os.path.dirname( |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 221 | os.path.dirname(os.path.dirname(recipes_cfg_path))) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 222 | else: |
| 223 | # find repo_root with git and calculate recipes_cfg_path |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 224 | repo_root = ( |
| 225 | _git_output(['rev-parse', '--show-toplevel'], |
| 226 | cwd=os.path.abspath(os.path.dirname(__file__))).strip()) |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 227 | repo_root = os.path.abspath(repo_root) |
| 228 | recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg') |
| 229 | args = ['--package', recipes_cfg_path] + args |
| 230 | |
| 231 | engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path) |
| 232 | |
recipe-roller | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 233 | try: |
recipe-roller | 7107df2 | 2019-05-22 16:47:18 -0700 | [diff] [blame] | 234 | return _subprocess_call( |
| 235 | [VPYTHON, '-u', |
| 236 | os.path.join(engine_path, 'recipe_engine', 'main.py')] + args) |
recipe-roller | f8a0329 | 2019-05-17 13:55:55 -0700 | [diff] [blame] | 237 | except KeyboardInterrupt: |
| 238 | return 1 |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 239 | |
recipe-roller | 978cd71 | 2020-03-30 12:16:42 -0700 | [diff] [blame^] | 240 | |
Lann Martin | 079e5aa | 2018-10-29 12:24:54 -0600 | [diff] [blame] | 241 | if __name__ == '__main__': |
| 242 | sys.exit(main()) |