Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """cipd.py bootstraps a CIPD client and installs named CIPD packages to a | |
| 7 directory. | |
| 8 """ | |
| 9 | |
| 10 import argparse | |
| 11 import collections | |
| 12 import json | |
| 13 import logging | |
| 14 import os | |
| 15 import platform | |
| 16 import subprocess | |
| 17 import sys | |
| 18 import tempfile | |
| 19 | |
| 20 # Install Infra build environment. | |
| 21 BUILD_ROOT = os.path.dirname(os.path.dirname(os.path.dirname( | |
| 22 os.path.abspath(__file__)))) | |
| 23 sys.path.insert(0, os.path.join(BUILD_ROOT, 'scripts')) | |
| 24 | |
| 25 import common.env | |
| 26 common.env.Install() | |
| 27 | |
| 28 import common.chromium_utils | |
| 29 | |
| 30 # Instance-wide logger. | |
| 31 LOGGER = logging.getLogger('cipd') | |
| 32 | |
| 33 # Used to contain a CIPD package specification. | |
| 34 CipdPackage = collections.namedtuple('CipdPackage', ('name', 'version')) | |
| 35 | |
| 36 | |
| 37 def get_normalized_platform(plat, machine): | |
| 38 if plat.startswith('linux'): | |
| 39 plat = 'linux' | |
| 40 elif plat.startswith(('win', 'cygwin')): | |
| 41 plat = 'win' | |
| 42 elif plat.startswith(('darwin', 'mac')): | |
| 43 plat = 'mac' | |
| 44 else: # pragma: no cover | |
| 45 raise ValueError("Don't understand platform [%s]" % (plat,)) | |
| 46 | |
| 47 if machine == 'x86_64': | |
| 48 bits = 64 | |
| 49 elif machine == 'i386': | |
| 50 bits = 32 | |
| 51 else: # pragma: no cover | |
| 52 raise ValueError("Don't understand machine [%s]" % (machine,)) | |
| 53 | |
| 54 return plat, bits | |
| 55 | |
| 56 def bootstrap(path): | |
| 57 bootstrap_path = os.path.join(common.env.Build, 'scripts', 'slave', | |
| 58 'recipe_modules', 'cipd', 'resources', | |
| 59 'bootstrap.py') | |
| 60 | |
| 61 plat, bits = get_normalized_platform(sys.platform, platform.machine()) | |
| 62 plat = '%s-%s' % ( | |
| 63 plat.replace('win', 'windows'), | |
| 64 { | |
| 65 32: '386', | |
| 66 64: 'amd64', | |
| 67 }[bits] | |
| 68 ) | |
| 69 json_output = os.path.join(path, 'cipd_bootstrap.json') | |
| 70 | |
| 71 cmd = [ | |
| 72 sys.executable, | |
| 73 bootstrap_path, | |
| 74 '--platform', plat, | |
| 75 '--dest-directory', path, | |
| 76 '--json-output', json_output, | |
| 77 ] | |
| 78 LOGGER.debug('Installing CIPD client: %s', cmd) | |
| 79 subprocess.check_call(cmd) | |
| 80 | |
| 81 with open(json_output, 'r') as fd: | |
| 82 return json.load(fd)['executable'] | |
| 83 | |
| 84 | |
| 85 def cipd_ensure(client, path, packages, service_account_json=None, | |
| 86 json_output=None): | |
| 87 manifest_path = os.path.join(path, 'cipd_manifest.txt') | |
| 88 with open(manifest_path, 'w') as fd: | |
| 89 fd.write('# This file is autogenerated by %s\n\n' % ( | |
| 90 os.path.abspath(__file__),)) | |
| 91 for pkg in packages: | |
| 92 LOGGER.debug('Adding package [%s] version [%s] to manifest.', | |
| 93 pkg.name, pkg.version) | |
| 94 fd.write('%s %s\n' % (pkg.name, pkg.version)) | |
| 95 | |
| 96 cmd = [ | |
| 97 client, | |
| 98 'ensure', | |
| 99 '-list', manifest_path, | |
| 100 '-root', path, | |
| 101 ] | |
| 102 if service_account_json: | |
| 103 cmd += ['-service-account-json', service_account_json] | |
| 104 if json_output: | |
| 105 cmd += ['-json-output', json_output] | |
| 106 LOGGER.debug('Ensuring CIPD packages: %s', cmd) | |
| 107 subprocess.check_call(cmd) | |
| 108 | |
| 109 | |
| 110 def parse_cipd_package(v): | |
| 111 idx = v.index('@') | |
| 112 if idx > 0: | |
| 113 package, version = v[:idx], v[idx+1:] | |
| 114 if package and version: | |
| 115 return CipdPackage(package, version) | |
| 116 raise argparse.ArgumentTypeError('Package must be expressed using the format ' | |
| 117 '"PACKAGE@VERSION".') | |
| 118 | |
| 119 | |
| 120 def main(argv): | |
| 121 parser = argparse.ArgumentParser() | |
| 122 parser.add_argument('-v', '--verbose', action='count', | |
| 123 help='Increase logging. Can be specified multiple times.') | |
| 124 parser.add_argument('-P', '--package', metavar='PACKAGE@VERSION', | |
| 125 action='append', default=[], required=True, | |
| 126 type=parse_cipd_package, | |
| 127 help='Add a <package:version> to the CIPD manifest.') | |
| 128 parser.add_argument('-d', '--dest-directory', required=True, | |
| 129 help='Add a <package:version> to the CIPD manifest.') | |
|
Vadim Sh.
2016/01/09 01:25:13
wrong help
dnj (Google)
2016/01/09 02:50:29
Hah so it is.
| |
| 130 parser.add_argument('-o', '--json-output', | |
| 131 help='Output package results to a JSON file.') | |
| 132 parser.add_argument('--service-account-json', | |
| 133 help='If specified, use this service account JSON.') | |
| 134 opts = parser.parse_args(argv[1:]) | |
| 135 | |
| 136 # Verbosity. | |
| 137 if opts.verbose == 0: | |
| 138 level = logging.WARNING | |
| 139 elif opts.verbose == 1: | |
| 140 level = logging.INFO | |
| 141 else: | |
| 142 level = logging.DEBUG | |
| 143 logging.getLogger().setLevel(level) | |
| 144 | |
| 145 if not os.path.isdir(opts.dest_directory): | |
| 146 LOGGER.info('Creating destination directory: %s', opts.dest_directory) | |
| 147 os.makedirs(opts.dest_directory) | |
| 148 | |
| 149 LOGGER.debug('Bootstrapping CIPD client...') | |
| 150 client = bootstrap(opts.dest_directory) | |
| 151 LOGGER.info('Using CIPD client at [%s].', client) | |
| 152 | |
| 153 LOGGER.info('CIPD ensuring %d package(s)...', len(opts.package)) | |
| 154 cipd_ensure(client, opts.dest_directory, opts.package, | |
| 155 service_account_json=opts.service_account_json, | |
| 156 json_output=opts.json_output) | |
| 157 return 0 | |
| 158 | |
| 159 | |
| 160 if __name__ == '__main__': | |
| 161 logging.basicConfig(level=logging.WARNING) | |
| 162 sys.exit(main(sys.argv)) | |
| OLD | NEW |