blob: c6b37aea1abb863c4d859b49f3f203011c95a4c8 [file] [log] [blame]
recipe-roller25331bf2019-05-17 20:49:29 +00001#!/bin/sh
recipe-rollera86225e2019-05-17 22:28:56 +00002# 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-roller25331bf2019-05-17 20:49:29 +00006# 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-rollera86225e2019-05-17 22:28:56 +000013# pylint: disable=pointless-string-statement
recipe-roller25331bf2019-05-17 20:49:29 +000014''''exec python -u -- "$0" ${1+"$@"} # '''
15# vi: syntax=python
phajdan.jr90b9f5f2016-07-08 05:19:18 -070016"""Bootstrap script to clone and forward to the recipe engine tool.
17
recipe-roller80b30ca2017-05-08 19:59:44 -070018*******************
19** DO NOT MODIFY **
20*******************
phajdan.jr90b9f5f2016-07-08 05:19:18 -070021
Robert Iannucci03ee2d62018-12-08 01:04:49 +000022This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/master/recipes.py.
recipe-rollerfb3fa782017-08-08 18:33:49 -070023To fix bugs, fix in the googlesource repo then run the autoroller.
phajdan.jr90b9f5f2016-07-08 05:19:18 -070024"""
25
recipe-roller25331bf2019-05-17 20:49:29 +000026# pylint: disable=wrong-import-position
Robert Iannucciaa6520e2017-02-21 12:15:41 -080027import argparse
Robert Iannucci370ca1b2017-03-15 17:57:48 -070028import json
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +000029import logging
recipe-roller8a2f54e2017-05-10 18:23:02 -070030import os
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +000031import subprocess
32import sys
recipe-roller328f60d2017-03-20 14:54:10 -070033import urlparse
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +000034
recipe-roller80b30ca2017-05-08 19:59:44 -070035from collections import namedtuple
36
recipe-roller80b30ca2017-05-08 19:59:44 -070037# 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.
recipe-roller80b30ca2017-05-08 19:59:44 -070041# branch (str) - the branch to fetch for the engine as an absolute ref (e.g.
42# refs/heads/master)
recipe-roller515b1542019-05-22 17:07:22 +000043EngineDep = namedtuple('EngineDep', 'url revision branch')
Robert Iannucci370ca1b2017-03-15 17:57:48 -070044
recipe-roller1ae7d332017-05-08 20:48:42 -070045
46class MalformedRecipesCfg(Exception):
recipe-roller515b1542019-05-22 17:07:22 +000047
recipe-roller1ae7d332017-05-08 20:48:42 -070048 def __init__(self, msg, path):
recipe-roller515b1542019-05-22 17:07:22 +000049 full_message = 'malformed recipes.cfg: %s: %r' % (msg, path)
50 super(MalformedRecipesCfg, self).__init__(full_message)
recipe-roller1ae7d332017-05-08 20:48:42 -070051
52
Robert Iannucci370ca1b2017-03-15 17:57:48 -070053def parse(repo_root, recipes_cfg_path):
recipe-roller1ae7d332017-05-08 20:48:42 -070054 """Parse is a lightweight a recipes.cfg file parser.
Robert Iannucci370ca1b2017-03-15 17:57:48 -070055
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):
recipe-roller603545b2017-07-03 15:39:22 -070062 engine_dep (EngineDep|None): The recipe_engine dependency, or None, if the
63 current repo IS the recipe_engine.
Robert Iannucci370ca1b2017-03-15 17:57:48 -070064 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:
recipe-roller2e401be2017-03-24 19:27:32 -070069 pb = json.load(fh)
Robert Iannucci370ca1b2017-03-15 17:57:48 -070070
recipe-roller1ae7d332017-05-08 20:48:42 -070071 try:
72 if pb['api_version'] != 2:
73 raise MalformedRecipesCfg('unknown version %d' % pb['api_version'],
74 recipes_cfg_path)
75
Robert Iannucci03ee2d62018-12-08 01:04:49 +000076 # If we're running ./recipes.py from the recipe_engine repo itself, then
recipe-roller603545b2017-07-03 15:39:22 -070077 # return None to signal that there's no EngineDep.
Robert Iannucciefc7e912019-02-15 19:48:48 +000078 repo_name = pb.get('repo_name')
79 if not repo_name:
80 repo_name = pb['project_id']
81 if repo_name == 'recipe_engine':
recipe-roller603545b2017-07-03 15:39:22 -070082 return None, pb.get('recipes_path', '')
83
recipe-roller1f85eb32017-03-29 15:27:36 -070084 engine = pb['deps']['recipe_engine']
recipe-roller80b30ca2017-05-08 19:59:44 -070085
recipe-roller1ae7d332017-05-08 20:48:42 -070086 if 'url' not in engine:
87 raise MalformedRecipesCfg(
recipe-roller515b1542019-05-22 17:07:22 +000088 'Required field "url" in dependency "recipe_engine" not found',
89 recipes_cfg_path)
recipe-roller80b30ca2017-05-08 19:59:44 -070090
recipe-roller1ae7d332017-05-08 20:48:42 -070091 engine.setdefault('revision', '')
recipe-roller1ae7d332017-05-08 20:48:42 -070092 engine.setdefault('branch', 'refs/heads/master')
93 recipes_path = pb.get('recipes_path', '')
Robert Iannucci370ca1b2017-03-15 17:57:48 -070094
recipe-roller1ae7d332017-05-08 20:48:42 -070095 # TODO(iannucci): only support absolute refs
96 if not engine['branch'].startswith('refs/'):
97 engine['branch'] = 'refs/heads/' + engine['branch']
recipe-roller80b30ca2017-05-08 19:59:44 -070098
recipe-roller515b1542019-05-22 17:07:22 +000099 recipes_path = os.path.join(repo_root,
100 recipes_path.replace('/', os.path.sep))
recipe-roller1ae7d332017-05-08 20:48:42 -0700101 return EngineDep(**engine), recipes_path
102 except KeyError as ex:
103 raise MalformedRecipesCfg(ex.message, recipes_cfg_path)
recipe-roller80b30ca2017-05-08 19:59:44 -0700104
105
recipe-rollera28b14f2018-06-21 01:24:29 +0000106_BAT = '.bat' if sys.platform.startswith(('win', 'cygwin')) else ''
107GIT = 'git' + _BAT
108VPYTHON = 'vpython' + _BAT
recipe-roller549bde12019-04-05 18:17:55 +0000109CIPD = 'cipd' + _BAT
110REQUIRED_BINARIES = {GIT, VPYTHON, CIPD}
111
112
113def _is_executable(path):
114 return os.path.isfile(path) and os.access(path, os.X_OK)
115
recipe-roller515b1542019-05-22 17:07:22 +0000116
recipe-roller549bde12019-04-05 18:17:55 +0000117# 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 Iannucci370ca1b2017-03-15 17:57:48 -0700124
125
phajdan.jr90b9f5f2016-07-08 05:19:18 -0700126def _subprocess_call(argv, **kwargs):
127 logging.info('Running %r', argv)
128 return subprocess.call(argv, **kwargs)
129
Robert Iannucciaa6520e2017-02-21 12:15:41 -0800130
recipe-roller80b30ca2017-05-08 19:59:44 -0700131def _git_check_call(argv, **kwargs):
recipe-roller515b1542019-05-22 17:07:22 +0000132 argv = [GIT] + argv
phajdan.jr90b9f5f2016-07-08 05:19:18 -0700133 logging.info('Running %r', argv)
134 subprocess.check_call(argv, **kwargs)
135
136
recipe-roller80b30ca2017-05-08 19:59:44 -0700137def _git_output(argv, **kwargs):
recipe-roller515b1542019-05-22 17:07:22 +0000138 argv = [GIT] + argv
recipe-roller80b30ca2017-05-08 19:59:44 -0700139 logging.info('Running %r', argv)
140 return subprocess.check_output(argv, **kwargs)
141
142
recipe-roller070c2e32017-05-09 10:39:38 -0700143def parse_args(argv):
144 """This extracts a subset of the arguments that this bootstrap script cares
145 about. Currently this consists of:
Robert Iannucci03ee2d62018-12-08 01:04:49 +0000146 * an override for the recipe engine in the form of `-O recipe_engine=/path`
recipe-roller070c2e32017-05-09 10:39:38 -0700147 * the --package option.
148 """
Robert Iannucciaa6520e2017-02-21 12:15:41 -0800149 PREFIX = 'recipe_engine='
150
recipe-roller94fd88e2017-04-10 21:15:16 -0700151 p = argparse.ArgumentParser(add_help=False)
Robert Iannucciaa6520e2017-02-21 12:15:41 -0800152 p.add_argument('-O', '--project-override', action='append')
recipe-roller070c2e32017-05-09 10:39:38 -0700153 p.add_argument('--package', type=os.path.abspath)
Robert Iannucciaa6520e2017-02-21 12:15:41 -0800154 args, _ = p.parse_known_args(argv)
155 for override in args.project_override or ():
156 if override.startswith(PREFIX):
recipe-roller070c2e32017-05-09 10:39:38 -0700157 return override[len(PREFIX):], args.package
158 return None, args.package
Robert Iannucciaa6520e2017-02-21 12:15:41 -0800159
160
recipe-roller070c2e32017-05-09 10:39:38 -0700161def checkout_engine(engine_path, repo_root, recipes_cfg_path):
recipe-roller80b30ca2017-05-08 19:59:44 -0700162 dep, recipes_path = parse(repo_root, recipes_cfg_path)
recipe-roller603545b2017-07-03 15:39:22 -0700163 if dep is None:
164 # we're running from the engine repo already!
165 return os.path.join(repo_root, recipes_path)
recipe-roller80b30ca2017-05-08 19:59:44 -0700166
167 url = dep.url
168
recipe-roller80b30ca2017-05-08 19:59:44 -0700169 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
recipe-roller80b30ca2017-05-08 19:59:44 -0700174 branch = dep.branch
175
176 # Ensure that we have the recipe engine cloned.
recipe-rollerd1dba642019-01-16 23:03:30 +0000177 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
recipe-roller80b30ca2017-05-08 19:59:44 -0700178
179 with open(os.devnull, 'w') as NUL:
180 # Note: this logic mirrors the logic in recipe_engine/fetch.py
recipe-rollerd1dba642019-01-16 23:03:30 +0000181 _git_check_call(['init', engine_path], stdout=NUL)
recipe-roller80b30ca2017-05-08 19:59:44 -0700182
183 try:
recipe-roller515b1542019-05-22 17:07:22 +0000184 _git_check_call(['rev-parse', '--verify',
185 '%s^{commit}' % revision],
186 cwd=engine_path,
187 stdout=NUL,
188 stderr=NUL)
recipe-roller80b30ca2017-05-08 19:59:44 -0700189 except subprocess.CalledProcessError:
recipe-roller515b1542019-05-22 17:07:22 +0000190 _git_check_call(['fetch', url, branch],
191 cwd=engine_path,
192 stdout=NUL,
recipe-roller80b30ca2017-05-08 19:59:44 -0700193 stderr=NUL)
194
195 try:
recipe-rollerd1dba642019-01-16 23:03:30 +0000196 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
recipe-roller80b30ca2017-05-08 19:59:44 -0700197 except subprocess.CalledProcessError:
recipe-rollerd1dba642019-01-16 23:03:30 +0000198 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
recipe-roller80b30ca2017-05-08 19:59:44 -0700199
recipe-rollerd3d7e202019-03-28 19:37:34 +0000200 # 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
recipe-roller80b30ca2017-05-08 19:59:44 -0700204 return engine_path
205
206
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +0000207def main():
recipe-roller549bde12019-04-05 18:17:55 +0000208 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
phajdan.jr90b9f5f2016-07-08 05:19:18 -0700212 if '--verbose' in sys.argv:
213 logging.getLogger().setLevel(logging.INFO)
214
recipe-roller070c2e32017-05-09 10:39:38 -0700215 args = sys.argv[1:]
216 engine_override, recipes_cfg_path = parse_args(args)
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +0000217
recipe-roller070c2e32017-05-09 10:39:38 -0700218 if recipes_cfg_path:
219 # calculate repo_root from recipes_cfg_path
220 repo_root = os.path.dirname(
recipe-roller515b1542019-05-22 17:07:22 +0000221 os.path.dirname(os.path.dirname(recipes_cfg_path)))
recipe-roller070c2e32017-05-09 10:39:38 -0700222 else:
223 # find repo_root with git and calculate recipes_cfg_path
recipe-roller515b1542019-05-22 17:07:22 +0000224 repo_root = (
225 _git_output(['rev-parse', '--show-toplevel'],
226 cwd=os.path.abspath(os.path.dirname(__file__))).strip())
recipe-roller070c2e32017-05-09 10:39:38 -0700227 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
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +0000230
recipe-roller070c2e32017-05-09 10:39:38 -0700231 engine_path = checkout_engine(engine_override, repo_root, recipes_cfg_path)
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +0000232
recipe-roller25331bf2019-05-17 20:49:29 +0000233 try:
recipe-roller515b1542019-05-22 17:07:22 +0000234 return _subprocess_call(
235 [VPYTHON, '-u',
236 os.path.join(engine_path, 'recipe_engine', 'main.py')] + args)
recipe-roller25331bf2019-05-17 20:49:29 +0000237 except KeyboardInterrupt:
238 return 1
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +0000239
recipe-roller80b30ca2017-05-08 19:59:44 -0700240
luqui@chromium.orgf7bb4cf2015-12-04 23:30:38 +0000241if __name__ == '__main__':
242 sys.exit(main())