blob: c6b37aea1abb863c4d859b49f3f203011c95a4c8 [file] [log] [blame]
recipe-rollerf8a03292019-05-17 13:55:55 -07001#!/bin/sh
recipe-roller18f0e712019-05-17 15:36:20 -07002# 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-rollerf8a03292019-05-17 13:55: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.
recipe-roller18f0e712019-05-17 15:36:20 -070013# pylint: disable=pointless-string-statement
recipe-rollerf8a03292019-05-17 13:55:55 -070014''''exec python -u -- "$0" ${1+"$@"} # '''
15# vi: syntax=python
Lann Martin079e5aa2018-10-29 12:24:54 -060016"""Bootstrap script to clone and forward to the recipe engine tool.
17
18*******************
19** DO NOT MODIFY **
20*******************
21
Yaakov Shaul3114e172019-02-05 21:45:16 -070022This is a copy of https://chromium.googlesource.com/infra/luci/recipes-py/+/master/recipes.py.
Lann Martin079e5aa2018-10-29 12:24:54 -060023To fix bugs, fix in the googlesource repo then run the autoroller.
24"""
25
recipe-rollerf8a03292019-05-17 13:55:55 -070026# pylint: disable=wrong-import-position
Lann Martin079e5aa2018-10-29 12:24:54 -060027import argparse
28import json
29import logging
30import os
Lann Martin079e5aa2018-10-29 12:24:54 -060031import subprocess
32import sys
Lann Martin079e5aa2018-10-29 12:24:54 -060033import urlparse
34
35from collections import namedtuple
36
Lann Martin079e5aa2018-10-29 12:24:54 -060037# 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 Martin079e5aa2018-10-29 12:24:54 -060041# branch (str) - the branch to fetch for the engine as an absolute ref (e.g.
42# refs/heads/master)
recipe-roller7107df22019-05-22 16:47:18 -070043EngineDep = namedtuple('EngineDep', 'url revision branch')
Lann Martin079e5aa2018-10-29 12:24:54 -060044
45
46class MalformedRecipesCfg(Exception):
recipe-roller7107df22019-05-22 16:47:18 -070047
Lann Martin079e5aa2018-10-29 12:24:54 -060048 def __init__(self, msg, path):
recipe-roller7107df22019-05-22 16:47:18 -070049 full_message = 'malformed recipes.cfg: %s: %r' % (msg, path)
50 super(MalformedRecipesCfg, self).__init__(full_message)
Lann Martin079e5aa2018-10-29 12:24:54 -060051
52
53def 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 Shaul3114e172019-02-05 21:45:16 -070076 # If we're running ./recipes.py from the recipe_engine repo itself, then
Lann Martin079e5aa2018-10-29 12:24:54 -060077 # return None to signal that there's no EngineDep.
Lann Martinee0b13d2019-03-05 12:52:28 -070078 repo_name = pb.get('repo_name')
79 if not repo_name:
80 repo_name = pb['project_id']
81 if repo_name == 'recipe_engine':
Lann Martin079e5aa2018-10-29 12:24:54 -060082 return None, pb.get('recipes_path', '')
83
84 engine = pb['deps']['recipe_engine']
85
86 if 'url' not in engine:
87 raise MalformedRecipesCfg(
recipe-roller7107df22019-05-22 16:47:18 -070088 'Required field "url" in dependency "recipe_engine" not found',
89 recipes_cfg_path)
Lann Martin079e5aa2018-10-29 12:24:54 -060090
91 engine.setdefault('revision', '')
Lann Martin079e5aa2018-10-29 12:24:54 -060092 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-roller7107df22019-05-22 16:47:18 -070099 recipes_path = os.path.join(repo_root,
100 recipes_path.replace('/', os.path.sep))
Lann Martin079e5aa2018-10-29 12:24:54 -0600101 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 ''
107GIT = 'git' + _BAT
108VPYTHON = 'vpython' + _BAT
recipe-rollera2fcee52019-04-09 08:22:38 -0700109CIPD = '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-roller7107df22019-05-22 16:47:18 -0700116
recipe-rollera2fcee52019-04-09 08:22:38 -0700117# 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
Lann Martin079e5aa2018-10-29 12:24:54 -0600124
125
126def _subprocess_call(argv, **kwargs):
127 logging.info('Running %r', argv)
128 return subprocess.call(argv, **kwargs)
129
130
131def _git_check_call(argv, **kwargs):
recipe-roller7107df22019-05-22 16:47:18 -0700132 argv = [GIT] + argv
Lann Martin079e5aa2018-10-29 12:24:54 -0600133 logging.info('Running %r', argv)
134 subprocess.check_call(argv, **kwargs)
135
136
137def _git_output(argv, **kwargs):
recipe-roller7107df22019-05-22 16:47:18 -0700138 argv = [GIT] + argv
Lann Martin079e5aa2018-10-29 12:24:54 -0600139 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:
Yaakov Shaul3114e172019-02-05 21:45:16 -0700146 * an override for the recipe engine in the form of `-O recipe_engine=/path`
Lann Martin079e5aa2018-10-29 12:24:54 -0600147 * 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
161def 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 Martin079e5aa2018-10-29 12:24:54 -0600174 branch = dep.branch
175
176 # Ensure that we have the recipe engine cloned.
Yaakov Shaul3114e172019-02-05 21:45:16 -0700177 engine_path = os.path.join(recipes_path, '.recipe_deps', 'recipe_engine')
Lann Martin079e5aa2018-10-29 12:24:54 -0600178
179 with open(os.devnull, 'w') as NUL:
180 # Note: this logic mirrors the logic in recipe_engine/fetch.py
Yaakov Shaul3114e172019-02-05 21:45:16 -0700181 _git_check_call(['init', engine_path], stdout=NUL)
Lann Martin079e5aa2018-10-29 12:24:54 -0600182
183 try:
recipe-roller7107df22019-05-22 16:47:18 -0700184 _git_check_call(['rev-parse', '--verify',
185 '%s^{commit}' % revision],
186 cwd=engine_path,
187 stdout=NUL,
188 stderr=NUL)
Lann Martin079e5aa2018-10-29 12:24:54 -0600189 except subprocess.CalledProcessError:
recipe-roller7107df22019-05-22 16:47:18 -0700190 _git_check_call(['fetch', url, branch],
191 cwd=engine_path,
192 stdout=NUL,
Lann Martin079e5aa2018-10-29 12:24:54 -0600193 stderr=NUL)
194
195 try:
Yaakov Shaul3114e172019-02-05 21:45:16 -0700196 _git_check_call(['diff', '--quiet', revision], cwd=engine_path)
Lann Martin079e5aa2018-10-29 12:24:54 -0600197 except subprocess.CalledProcessError:
Yaakov Shaul3114e172019-02-05 21:45:16 -0700198 _git_check_call(['reset', '-q', '--hard', revision], cwd=engine_path)
Lann Martin079e5aa2018-10-29 12:24:54 -0600199
recipe-roller149c1572019-03-28 15:53:36 -0700200 # 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 Martin079e5aa2018-10-29 12:24:54 -0600204 return engine_path
205
206
207def main():
recipe-rollera2fcee52019-04-09 08:22:38 -0700208 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 Martin079e5aa2018-10-29 12:24:54 -0600212 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-roller7107df22019-05-22 16:47:18 -0700221 os.path.dirname(os.path.dirname(recipes_cfg_path)))
Lann Martin079e5aa2018-10-29 12:24:54 -0600222 else:
223 # find repo_root with git and calculate recipes_cfg_path
recipe-roller7107df22019-05-22 16:47:18 -0700224 repo_root = (
225 _git_output(['rev-parse', '--show-toplevel'],
226 cwd=os.path.abspath(os.path.dirname(__file__))).strip())
Lann Martin079e5aa2018-10-29 12:24:54 -0600227 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-rollerf8a03292019-05-17 13:55:55 -0700233 try:
recipe-roller7107df22019-05-22 16:47:18 -0700234 return _subprocess_call(
235 [VPYTHON, '-u',
236 os.path.join(engine_path, 'recipe_engine', 'main.py')] + args)
recipe-rollerf8a03292019-05-17 13:55:55 -0700237 except KeyboardInterrupt:
238 return 1
Lann Martin079e5aa2018-10-29 12:24:54 -0600239
recipe-roller978cd712020-03-30 12:16:42 -0700240
Lann Martin079e5aa2018-10-29 12:24:54 -0600241if __name__ == '__main__':
242 sys.exit(main())