blob: 6cdc365cadb06d02f1063528fb27a294529cb6c9 [file] [log] [blame]
recipe-roller955ddaf2019-05-20 13:03:55 -07001#!/bin/sh
2# Copyright 2019 The LUCI Authors. All rights reserved.
borenet1ed2ae42016-07-26 11:52:17 -07003# Use of this source code is governed under the Apache License, Version 2.0
4# that can be found in the LICENSE file.
Eric Borenf171e162016-11-14 12:18:34 -05005
recipe-roller955ddaf2019-05-20 13:03: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#
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.
13# pylint: disable=pointless-string-statement
14''''exec python -u -- "$0" ${1+"$@"} # '''
15# vi: syntax=python
16
borenet1ed2ae42016-07-26 11:52:17 -070017"""Bootstrap script to clone and forward to the recipe engine tool.
Eric Borenf171e162016-11-14 12:18:34 -050018
Robert Iannucci3734c7d2017-05-09 11:06:48 -070019*******************
20** DO NOT MODIFY **
21*******************
Eric Borenf171e162016-11-14 12:18:34 -050022
Robert Iannuccie1c23542018-12-07 17:42:12 -080023This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/master/recipes.py.
Eric Boren8e0c2c92017-09-27 13:03:35 -040024To fix bugs, fix in the googlesource repo then run the autoroller.
borenet1ed2ae42016-07-26 11:52:17 -070025"""
borenet1ed2ae42016-07-26 11:52:17 -070026
recipe-roller955ddaf2019-05-20 13:03:55 -070027# pylint: disable=wrong-import-position
Robert Iannuccie8b50852017-03-15 01:24:56 -070028import argparse
Robert Iannucci2ba659e2017-03-15 18:04:05 -070029import json
borenet1ed2ae42016-07-26 11:52:17 -070030import logging
recipe-roller42e16b02017-05-11 04:27:01 -070031import os
borenet1ed2ae42016-07-26 11:52:17 -070032import subprocess
33import sys
Robert Iannucci342977c2017-03-24 17:45:40 -070034import urlparse
Eric Borenf171e162016-11-14 12:18:34 -050035
Robert Iannucci3734c7d2017-05-09 11:06:48 -070036from collections import namedtuple
37
Robert Iannucci3734c7d2017-05-09 11:06:48 -070038# The dependency entry for the recipe_engine in the client repo's recipes.cfg
39#
40# url (str) - the url to the engine repo we want to use.
41# revision (str) - the git revision for the engine to get.
Robert Iannucci3734c7d2017-05-09 11:06:48 -070042# branch (str) - the branch to fetch for the engine as an absolute ref (e.g.
43# refs/heads/master)
Robert Iannucci3734c7d2017-05-09 11:06:48 -070044EngineDep = namedtuple('EngineDep',
recipe-roller1ce80fb2019-01-16 15:11:31 -080045 'url revision branch')
Robert Iannucci3734c7d2017-05-09 11:06:48 -070046
47
48class MalformedRecipesCfg(Exception):
49 def __init__(self, msg, path):
50 super(MalformedRecipesCfg, self).__init__('malformed recipes.cfg: %s: %r'
51 % (msg, path))
52
Robert Iannucci2ba659e2017-03-15 18:04:05 -070053
54def parse(repo_root, recipes_cfg_path):
Robert Iannucci3734c7d2017-05-09 11:06:48 -070055 """Parse is a lightweight a recipes.cfg file parser.
Robert Iannucci2ba659e2017-03-15 18:04:05 -070056
57 Args:
58 repo_root (str) - native path to the root of the repo we're trying to run
59 recipes for.
60 recipes_cfg_path (str) - native path to the recipes.cfg file to process.
61
62 Returns (as tuple):
Eric Boren8e0c2c92017-09-27 13:03:35 -040063 engine_dep (EngineDep|None): The recipe_engine dependency, or None, if the
64 current repo IS the recipe_engine.
Robert Iannucci2ba659e2017-03-15 18:04:05 -070065 recipes_path (str) - native path to where the recipes live inside of the
66 current repo (i.e. the folder containing `recipes/` and/or
67 `recipe_modules`)
68 """
69 with open(recipes_cfg_path, 'rU') as fh:
Robert Iannucci654dfee2017-03-27 20:52:15 -070070 pb = json.load(fh)
Robert Iannucci2ba659e2017-03-15 18:04:05 -070071
Robert Iannucci3734c7d2017-05-09 11:06:48 -070072 try:
73 if pb['api_version'] != 2:
74 raise MalformedRecipesCfg('unknown version %d' % pb['api_version'],
75 recipes_cfg_path)
Robert Iannucci2ba659e2017-03-15 18:04:05 -070076
Robert Iannuccie1c23542018-12-07 17:42:12 -080077 # If we're running ./recipes.py from the recipe_engine repo itself, then
Eric Boren8e0c2c92017-09-27 13:03:35 -040078 # return None to signal that there's no EngineDep.
Robert Iannuccif4d4b872019-02-16 14:10:41 -080079 repo_name = pb.get('repo_name')
80 if not repo_name:
81 repo_name = pb['project_id']
82 if repo_name == 'recipe_engine':
Eric Boren8e0c2c92017-09-27 13:03:35 -040083 return None, pb.get('recipes_path', '')
84
Robert Iannucci3734c7d2017-05-09 11:06:48 -070085 engine = pb['deps']['recipe_engine']
86
87 if 'url' not in engine:
88 raise MalformedRecipesCfg(
89 'Required field "url" in dependency "recipe_engine" not found',
90 recipes_cfg_path)
91
92 engine.setdefault('revision', '')
Robert Iannucci3734c7d2017-05-09 11:06:48 -070093 engine.setdefault('branch', 'refs/heads/master')
94 recipes_path = pb.get('recipes_path', '')
95
96 # TODO(iannucci): only support absolute refs
97 if not engine['branch'].startswith('refs/'):
98 engine['branch'] = 'refs/heads/' + engine['branch']
99
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700100 recipes_path = os.path.join(
101 repo_root, recipes_path.replace('/', os.path.sep))
102 return EngineDep(**engine), recipes_path
103 except KeyError as ex:
104 raise MalformedRecipesCfg(ex.message, recipes_cfg_path)
105
106
recipe-roller198498b2018-06-22 07:38:45 -0700107_BAT = '.bat' if sys.platform.startswith(('win', 'cygwin')) else ''
108GIT = 'git' + _BAT
109VPYTHON = 'vpython' + _BAT
recipe-roller11078c652019-04-05 11:34:44 -0700110CIPD = 'cipd' + _BAT
111REQUIRED_BINARIES = {GIT, VPYTHON, CIPD}
112
113
114def _is_executable(path):
115 return os.path.isfile(path) and os.access(path, os.X_OK)
116
117# TODO: Use shutil.which once we switch to Python3.
118def _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
Robert Iannucci2ba659e2017-03-15 18:04:05 -0700124
125
borenet1ed2ae42016-07-26 11:52:17 -0700126def _subprocess_call(argv, **kwargs):
127 logging.info('Running %r', argv)
128 return subprocess.call(argv, **kwargs)
Eric Borenf171e162016-11-14 12:18:34 -0500129
Robert Iannuccie8b50852017-03-15 01:24:56 -0700130
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700131def _git_check_call(argv, **kwargs):
132 argv = [GIT]+argv
borenet1ed2ae42016-07-26 11:52:17 -0700133 logging.info('Running %r', argv)
134 subprocess.check_call(argv, **kwargs)
Eric Borenf171e162016-11-14 12:18:34 -0500135
136
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700137def _git_output(argv, **kwargs):
138 argv = [GIT]+argv
139 logging.info('Running %r', argv)
140 return subprocess.check_output(argv, **kwargs)
141
142
143def parse_args(argv):
144 """This extracts a subset of the arguments that this bootstrap script cares
145 about. Currently this consists of:
Robert Iannuccie1c23542018-12-07 17:42:12 -0800146 * an override for the recipe engine in the form of `-O recipe_engine=/path`
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700147 * the --package option.
148 """
Robert Iannuccie8b50852017-03-15 01:24:56 -0700149 PREFIX = 'recipe_engine='
150
Eric Borenc1e96172017-04-19 11:12:20 +0000151 p = argparse.ArgumentParser(add_help=False)
Robert Iannuccie8b50852017-03-15 01:24:56 -0700152 p.add_argument('-O', '--project-override', action='append')
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700153 p.add_argument('--package', type=os.path.abspath)
Robert Iannuccie8b50852017-03-15 01:24:56 -0700154 args, _ = p.parse_known_args(argv)
155 for override in args.project_override or ():
156 if override.startswith(PREFIX):
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700157 return override[len(PREFIX):], args.package
158 return None, args.package
159
160
161def checkout_engine(engine_path, repo_root, recipes_cfg_path):
162 dep, recipes_path = parse(repo_root, recipes_cfg_path)
Eric Boren8e0c2c92017-09-27 13:03:35 -0400163 if dep is None:
164 # we're running from the engine repo already!
165 return os.path.join(repo_root, recipes_path)
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700166
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
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700174 branch = dep.branch
175
176 # Ensure that we have the recipe engine cloned.
recipe-roller1ce80fb2019-01-16 15:11:31 -0800177 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700178
179 with open(os.devnull, 'w') as NUL:
180 # Note: this logic mirrors the logic in recipe_engine/fetch.py
recipe-roller1ce80fb2019-01-16 15:11:31 -0800181 _git_check_call(['init', engine_path], stdout=NUL)
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700182
183 try:
184 _git_check_call(['rev-parse', '--verify', '%s^{commit}' % revision],
recipe-roller1ce80fb2019-01-16 15:11:31 -0800185 cwd=engine_path, stdout=NUL, stderr=NUL)
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700186 except subprocess.CalledProcessError:
recipe-roller1ce80fb2019-01-16 15:11:31 -0800187 _git_check_call(['fetch', url, branch], cwd=engine_path, stdout=NUL,
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700188 stderr=NUL)
189
190 try:
recipe-roller1ce80fb2019-01-16 15:11:31 -0800191 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700192 except subprocess.CalledProcessError:
recipe-roller1ce80fb2019-01-16 15:11:31 -0800193 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700194
recipe-roller98b14ff2019-03-28 13:03:40 -0700195 # If the engine has refactored/moved modules we need to clean all .pyc files
196 # or things will get squirrely.
197 _git_check_call(['clean', '-qxf'], cwd=engine_path)
198
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700199 return engine_path
Robert Iannuccie8b50852017-03-15 01:24:56 -0700200
201
borenet1ed2ae42016-07-26 11:52:17 -0700202def main():
recipe-roller11078c652019-04-05 11:34:44 -0700203 for required_binary in REQUIRED_BINARIES:
204 if not _is_on_path(required_binary):
205 return 'Required binary is not found on PATH: %s' % required_binary
206
borenet1ed2ae42016-07-26 11:52:17 -0700207 if '--verbose' in sys.argv:
208 logging.getLogger().setLevel(logging.INFO)
Eric Borenf171e162016-11-14 12:18:34 -0500209
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700210 args = sys.argv[1:]
211 engine_override, recipes_cfg_path = parse_args(args)
Robert Iannuccie8b50852017-03-15 01:24:56 -0700212
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700213 if recipes_cfg_path:
214 # calculate repo_root from recipes_cfg_path
215 repo_root = os.path.dirname(
216 os.path.dirname(
217 os.path.dirname(recipes_cfg_path)))
borenet1ed2ae42016-07-26 11:52:17 -0700218 else:
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700219 # find repo_root with git and calculate recipes_cfg_path
220 repo_root = (_git_output(
221 ['rev-parse', '--show-toplevel'],
222 cwd=os.path.abspath(os.path.dirname(__file__))).strip())
223 repo_root = os.path.abspath(repo_root)
224 recipes_cfg_path = os.path.join(repo_root, 'infra', 'config', 'recipes.cfg')
225 args = ['--package', recipes_cfg_path] + args
Eric Borenf171e162016-11-14 12:18:34 -0500226
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700227 engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path)
Eric Borenf171e162016-11-14 12:18:34 -0500228
recipe-roller955ddaf2019-05-20 13:03:55 -0700229 try:
230 return _subprocess_call([
231 VPYTHON, '-u',
232 os.path.join(engine_path, 'recipe_engine', 'main.py')] + args)
233 except KeyboardInterrupt:
234 return 1
Eric Borenf171e162016-11-14 12:18:34 -0500235
Robert Iannucci3734c7d2017-05-09 11:06:48 -0700236
borenet1ed2ae42016-07-26 11:52:17 -0700237if __name__ == '__main__':
238 sys.exit(main())