blob: c3b8b5ce2dee7fce14e70039680c219fdb41d1e0 [file] [log] [blame]
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +00001# 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
7This is used for downloading a tool executable managed in CIPD.
8
9CIPD URI format is locally defined for this script.
10Example:
11cipd://chromiumos/infra/tclint/linux-amd64:${version}
12chromiumos/infra/tclint/linux-amd64 is the path sent to cipd tool,
13and ${version} is the version defined in CIPD.
14"""
15
16import os
17import urllib.parse
18
19from chromite.lib import commandline
20from chromite.lib import constants
21from chromite.lib import cros_build_lib
22
23
24def 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
34def 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
44def 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)