| #!/usr/bin/env python3 | |
| # Copyright 2019 The ChromiumOS Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """Simpler helper to download files.""" | |
| import logging | |
| from pathlib import Path | |
| import sys | |
| import libdot | |
| def get_parser(): | |
| """Get a command line parser.""" | |
| parser = libdot.ArgumentParser(description=__doc__) | |
| parser.add_argument( | |
| "--base64", action="store_true", help="Decode file using base64." | |
| ) | |
| parser.add_argument( | |
| "--manifest", type=Path, help="Lookup sources in a manifest." | |
| ) | |
| parser.add_argument( | |
| "-o", "--output", type=Path, help="Alternative path to save to." | |
| ) | |
| parser.add_argument("args", nargs="+", help="URIs or files to download.") | |
| return parser | |
| def main(argv): | |
| """The main func!""" | |
| parser = get_parser() | |
| opts = parser.parse_args(argv) | |
| if opts.manifest: | |
| manifest = libdot.ArtifactManifest.from_file(opts.manifest) | |
| else: | |
| manifest = None | |
| for arg in opts.args: | |
| output = opts.output | |
| if manifest: | |
| artifact = manifest[arg] | |
| if not artifact: | |
| logging.error("%s: Unable to locate '%s'", opts.manifest, arg) | |
| return 1 | |
| if not output: | |
| output = artifact.url.rsplit("/", 1)[-1] | |
| libdot.fetch_manifest(opts.manifest, arg, output) | |
| else: | |
| if not output: | |
| output = arg.rsplit("/", 1)[-1] | |
| libdot.fetch(arg, output, b64=opts.base64) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main(sys.argv[1:])) |