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 | |
| 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(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | """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 Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def ParseCipdUri(uri): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | 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") |
| 42 | pkgpath, version = o.path.rsplit(":", 1) |
| 43 | return (o.netloc + pkgpath, version) |
Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | parser = GetParser() |
| 48 | options = parser.parse_args(argv) |
| 49 | options.Freeze() |
Tatsuhisa Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 50 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | (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 Yamaguchi | c439ce6 | 2020-07-08 10:58:26 +0000 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | except cros_build_lib.RunCommandError as ex: |
| 68 | # Hide the stack trace using Die. |
| 69 | cros_build_lib.Die("%s", ex) |