David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 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 binpkg from Google Storage. |
| 6 | |
| 7 | This is needed for two reasons: |
| 8 | 1) In the case where a binpkg is left over in the packages dir, |
| 9 | portage doesn't handle retries well and reports an error. |
| 10 | 2) gsutil retries when a download is interrupted, but it doesn't |
| 11 | handle the case where we are unable to resume a transfer and the |
| 12 | transfer needs to be restarted from scratch. Ensuring that the |
| 13 | file is deleted between each retry helps handle that eventuality. |
| 14 | """ |
| 15 | |
| 16 | import shutil |
| 17 | |
| 18 | from chromite.lib import commandline |
| 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import gs |
| 21 | from chromite.lib import osutils |
| 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("--boto", type="path", help="Path to boto auth file.") |
| 28 | parser.add_argument( |
| 29 | "uri", type="gs_path", help="Google Storage URI to download" |
| 30 | ) |
| 31 | parser.add_argument( |
| 32 | "filename", type="path", help="Location to store the file." |
| 33 | ) |
| 34 | return parser |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 35 | |
Mike Frysinger | 3349400 | 2014-05-07 23:46:08 -0400 | [diff] [blame] | 36 | |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 37 | def Copy(ctx, uri, filename): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | """Run the copy using a temp file.""" |
| 39 | temp_path = "%s.tmp" % filename |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 40 | osutils.SafeUnlink(temp_path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | try: |
| 42 | ctx.Copy(uri, temp_path) |
| 43 | shutil.move(temp_path, filename) |
| 44 | finally: |
| 45 | osutils.SafeUnlink(temp_path) |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 46 | |
Mike Frysinger | 3349400 | 2014-05-07 23:46:08 -0400 | [diff] [blame] | 47 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 48 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | parser = GetParser() |
| 50 | options = parser.parse_args(argv) |
| 51 | options.Freeze() |
| 52 | ctx = gs.GSContext(boto_file=options.boto) |
| 53 | try: |
| 54 | Copy(ctx, options.uri, options.filename) |
| 55 | except gs.GSContextException as ex: |
| 56 | # Hide the stack trace using Die. |
| 57 | cros_build_lib.Die("%s", ex) |