Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 1 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Download a file from CIPD. |
| 6 | |
| 7 | This is used for downloading a tool executable managed in CIPD. |
| 8 | |
| 9 | CIPD URI format is locally defined for this script. |
| 10 | Example: |
| 11 | cipd://chromiumos/infra/tclint/linux-amd64:${version} |
| 12 | chromiumos/infra/tclint/linux-amd64 is the path sent to cipd tool, |
| 13 | and ${version} is the version defined in CIPD. |
| 14 | """ |
| 15 | |
| 16 | import os |
| 17 | import urllib.parse |
| 18 | |
| 19 | from chromite.lib import commandline |
| 20 | from chromite.lib import constants |
| 21 | from chromite.lib import cros_build_lib |
| 22 | |
| 23 | |
| 24 | def GetParser(): |
| 25 | """Creates the argparse parser.""" |
| 26 | parser = commandline.ArgumentParser(description=__doc__) |
| 27 | parser.add_argument('uri', type='cipd', |
| 28 | help='CIPD URI of a file to download.') |
| 29 | parser.add_argument('output', type='path', |
| 30 | help='Location to store the file.') |
| 31 | return parser |
| 32 | |
| 33 | |
| 34 | def ParseCipdUri(uri): |
| 35 | o = urllib.parse.urlparse(uri) |
| 36 | if o.scheme != 'cipd': |
| 37 | raise ValueError('wrong scheme: ', o.scheme) |
| 38 | if ':' not in o.path: |
| 39 | raise ValueError('version not specified') |
| 40 | pkgpath, version = o.path.rsplit(':', 1) |
| 41 | return (o.netloc + pkgpath, version) |
| 42 | |
| 43 | |
| 44 | def main(argv): |
| 45 | parser = GetParser() |
| 46 | options = parser.parse_args(argv) |
| 47 | options.Freeze() |
| 48 | |
| 49 | (pkgpath, version) = ParseCipdUri(options.uri) |
| 50 | try: |
| 51 | cros_build_lib.run( |
| 52 | [os.path.join(constants.DEPOT_TOOLS_DIR, 'cipd'), 'pkg-fetch', |
| 53 | '-out', options.output, '-version', version, '-verbose', pkgpath], |
| 54 | check=True) |
| 55 | |
| 56 | except cros_build_lib.RunCommandError as ex: |
| 57 | # Hide the stack trace using Die. |
| 58 | cros_build_lib.Die('%s', ex) |