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