blob: 5fd36fb7c038091648326b232250ed18193bc3e1 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2020 The ChromiumOS Authors
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +00002# 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():
Alex Klein1699fab2022-09-08 08:46:06 -060025 """Creates the argparse parser."""
26 parser = commandline.ArgumentParser(description=__doc__)
27 parser.add_argument(
28 "uri", type="cipd", help="CIPD URI of a file to download."
29 )
30 parser.add_argument(
31 "output", type="path", help="Location to store the file."
32 )
33 return parser
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000034
35
36def ParseCipdUri(uri):
Alex Klein1699fab2022-09-08 08:46:06 -060037 o = urllib.parse.urlparse(uri)
38 if o.scheme != "cipd":
39 raise ValueError("wrong scheme: ", o.scheme)
40 if ":" not in o.path:
41 raise ValueError("version not specified")
Mike Frysinger32703542023-01-18 21:41:15 -050042 pkgpath, version = o.path.split(":", 1)
Alex Klein1699fab2022-09-08 08:46:06 -060043 return (o.netloc + pkgpath, version)
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000044
45
46def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060047 parser = GetParser()
48 options = parser.parse_args(argv)
49 options.Freeze()
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000050
Alex Klein1699fab2022-09-08 08:46:06 -060051 (pkgpath, version) = ParseCipdUri(options.uri)
52 try:
53 cros_build_lib.run(
54 [
55 os.path.join(constants.DEPOT_TOOLS_DIR, "cipd"),
56 "pkg-fetch",
57 "-out",
58 options.output,
59 "-version",
60 version,
61 "-verbose",
62 pkgpath,
63 ],
64 check=True,
65 )
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000066
Alex Klein1699fab2022-09-08 08:46:06 -060067 except cros_build_lib.RunCommandError as ex:
68 # Hide the stack trace using Die.
69 cros_build_lib.Die("%s", ex)