blob: b4a370572a4004a152eb1722888d41963fe6ac60 [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
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000016import urllib.parse
17
18from chromite.lib import commandline
19from chromite.lib import constants
20from chromite.lib import cros_build_lib
21
22
23def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060024 """Creates the argparse parser."""
25 parser = commandline.ArgumentParser(description=__doc__)
26 parser.add_argument(
27 "uri", type="cipd", help="CIPD URI of a file to download."
28 )
29 parser.add_argument(
30 "output", type="path", help="Location to store the file."
31 )
32 return parser
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000033
34
35def ParseCipdUri(uri):
Alex Klein1699fab2022-09-08 08:46:06 -060036 o = urllib.parse.urlparse(uri)
37 if o.scheme != "cipd":
38 raise ValueError("wrong scheme: ", o.scheme)
39 if ":" not in o.path:
40 raise ValueError("version not specified")
Mike Frysinger32703542023-01-18 21:41:15 -050041 pkgpath, version = o.path.split(":", 1)
Alex Klein1699fab2022-09-08 08:46:06 -060042 return (o.netloc + pkgpath, version)
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000043
44
45def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060046 parser = GetParser()
47 options = parser.parse_args(argv)
48 options.Freeze()
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000049
Alex Klein1699fab2022-09-08 08:46:06 -060050 (pkgpath, version) = ParseCipdUri(options.uri)
51 try:
52 cros_build_lib.run(
53 [
Mike Frysingera95870e2023-03-27 15:45:34 -040054 constants.DEPOT_TOOLS_DIR / "cipd",
Alex Klein1699fab2022-09-08 08:46:06 -060055 "pkg-fetch",
56 "-out",
57 options.output,
58 "-version",
59 version,
60 "-verbose",
61 pkgpath,
62 ],
63 check=True,
64 )
Tatsuhisa Yamaguchic439ce62020-07-08 10:58:26 +000065
Alex Klein1699fab2022-09-08 08:46:06 -060066 except cros_build_lib.RunCommandError as ex:
67 # Hide the stack trace using Die.
68 cros_build_lib.Die("%s", ex)