diff options
author | luqui <luqui@chromium.org> | 2016-02-05 17:41:59 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-06 01:42:42 +0000 |
commit | b2555280f21a042179427674884fb209817ecca9 (patch) | |
tree | 12123c3599afcfa15fc38d97821462234f906e3e /infra/recipes.py | |
parent | 08651498772974b3bac504633262c55ad4d1f028 (diff) | |
download | chromium_src-b2555280f21a042179427674884fb209817ecca9.zip chromium_src-b2555280f21a042179427674884fb209817ecca9.tar.gz chromium_src-b2555280f21a042179427674884fb209817ecca9.tar.bz2 |
Made chromium into a recipe package, and added canary recipe.
This is a small step on the path to src-side recipes in chromium.
BUG=584079
R=iannucci@chromium.org,martiniss@chromium.org
CC=kbr@chromium.org,dpranke@chromium.org,phajdan.jr@chromium.org
Review URL: https://codereview.chromium.org/1660853004
Cr-Commit-Position: refs/heads/master@{#373988}
Diffstat (limited to 'infra/recipes.py')
-rwxr-xr-x | infra/recipes.py | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/infra/recipes.py b/infra/recipes.py new file mode 100755 index 0000000..f3d3081 --- /dev/null +++ b/infra/recipes.py @@ -0,0 +1,149 @@ +#!/usr/bin/env python + +# Copyright 2016 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Bootstrap script to clone and forward to the recipe engine tool. + +*********************************************************************** +** DO NOT MODIFY EXCEPT IN THE PER-REPO CONFIGURATION SECTION BELOW. ** +*********************************************************************** + +This is a copy of https://github.com/luci/recipes-py/blob/master/doc/recipes.py. +To fix bugs, fix in the github repo then copy it back to here and fix the +PER-REPO CONFIGURATION section to look like this one. +""" + +import os + +#### PER-REPO CONFIGURATION (editable) #### +# The root of the repository relative to the directory of this file. +REPO_ROOT = os.pardir +# The path of the recipes.cfg file relative to the root of the repository. +RECIPES_CFG = os.path.join('infra', 'config', 'recipes.cfg') +#### END PER-REPO CONFIGURATION #### + +BOOTSTRAP_VERSION = 1 + +import ast +import logging +import random +import re +import subprocess +import sys +import time +import traceback + + +def parse_protobuf(fh): + """Parse the protobuf text format just well enough to understand recipes.cfg. + + We don't use the protobuf library because we want to be as self-contained + as possible in this bootstrap, so it can be simply vendored into a client + repo. + + We assume all fields are repeated since we don't have a proto spec to work + with. + + Args: + fh: a filehandle containing the text format protobuf. + Returns: + A recursive dictionary of lists. + """ + def parse_atom(text): + if text == 'true': return True + if text == 'false': return False + return ast.literal_eval(text) + + ret = {} + for line in fh: + line = line.strip() + m = re.match(r'(\w+)\s*:\s*(.*)', line) + if m: + ret.setdefault(m.group(1), []).append(parse_atom(m.group(2))) + continue + + m = re.match(r'(\w+)\s*{', line) + if m: + subparse = parse_protobuf(fh) + ret.setdefault(m.group(1), []).append(subparse) + continue + + if line == '}': return ret + if line == '': continue + + raise ValueError('Could not understand line: <%s>' % line) + + return ret + + +def get_unique(things): + if len(things) == 1: + return things[0] + elif len(things) == 0: + raise ValueError("Expected to get one thing, but dinna get none.") + else: + logging.warn('Expected to get one thing, but got a bunch: %s\n%s' % + (things, traceback.format_stack())) + return things[0] + + +def main(): + if sys.platform.startswith(('win', 'cygwin')): + git = 'git.bat' + else: + git = 'git' + + # Find the repository and config file to operate on. + repo_root = os.path.abspath( + os.path.join(os.path.dirname(__file__), REPO_ROOT)) + recipes_cfg_path = os.path.join(repo_root, RECIPES_CFG) + + with open(recipes_cfg_path, 'rU') as fh: + protobuf = parse_protobuf(fh) + + engine_buf = get_unique([ + b for b in protobuf['deps'] if b.get('project_id') == ['recipe_engine'] ]) + engine_url = get_unique(engine_buf['url']) + engine_revision = get_unique(engine_buf['revision']) + engine_subpath = (get_unique(engine_buf.get('path_override', [''])) + .replace('/', os.path.sep)) + + recipes_path = os.path.join(repo_root, + get_unique(protobuf['recipes_path']).replace('/', os.path.sep)) + deps_path = os.path.join(recipes_path, '.recipe_deps') + engine_path = os.path.join(deps_path, 'recipe_engine') + + # Ensure that we have the recipe engine cloned. + def ensure_engine(): + if not os.path.exists(deps_path): + os.makedirs(deps_path) + if not os.path.exists(engine_path): + subprocess.check_call([git, 'clone', engine_url, engine_path]) + + needs_fetch = subprocess.call( + [git, 'rev-parse', '--verify', '%s^{commit}' % engine_revision], + cwd=engine_path, stdout=open(os.devnull, 'w')) + if needs_fetch: + subprocess.check_call([git, 'fetch'], cwd=engine_path) + subprocess.check_call( + [git, 'checkout', '--quiet', engine_revision], cwd=engine_path) + + try: + ensure_engine() + except subprocess.CalledProcessError as e: + if e.returncode == 128: # Thrown when git gets a lock error. + time.sleep(random.uniform(2,5)) + ensure_engine() + else: + raise + + args = ['--package', recipes_cfg_path, + '--bootstrap-script', __file__] + sys.argv[1:] + return subprocess.call([ + sys.executable, '-u', + os.path.join(engine_path, engine_subpath, 'recipes.py')] + args) + +if __name__ == '__main__': + sys.exit(main()) |