Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2020 The ChromiumOS Authors |
Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 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 | |
Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 16 | import urllib.parse |
| 17 | |
| 18 | from chromite.lib import commandline |
| 19 | from chromite.lib import constants |
| 20 | from chromite.lib import cros_build_lib |
| 21 | |
| 22 | |
| 23 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | """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 Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def ParseCipdUri(uri): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | 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 Frysinger | 3270354 | 2023-01-18 21:41:15 -0500 | [diff] [blame] | 41 | pkgpath, version = o.path.split(":", 1) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 42 | return (o.netloc + pkgpath, version) |
Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | parser = GetParser() |
| 47 | options = parser.parse_args(argv) |
| 48 | options.Freeze() |
Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | (pkgpath, version) = ParseCipdUri(options.uri) |
| 51 | try: |
| 52 | cros_build_lib.run( |
| 53 | [ |
Mike Frysinger | a95870e | 2023-03-27 15:45:34 -0400 | [diff] [blame^] | 54 | constants.DEPOT_TOOLS_DIR / "cipd", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | "pkg-fetch", |
| 56 | "-out", |
| 57 | options.output, |
| 58 | "-version", |
| 59 | version, |
| 60 | "-verbose", |
| 61 | pkgpath, |
| 62 | ], |
| 63 | check=True, |
| 64 | ) |
Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | except cros_build_lib.RunCommandError as ex: |
| 67 | # Hide the stack trace using Die. |
| 68 | cros_build_lib.Die("%s", ex) |