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 |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame^] | 22 | from chromite.lib import retry_util |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def GetParser(): |
| 26 | """Creates the argparse parser.""" |
| 27 | parser = commandline.ArgumentParser(description=__doc__) |
| 28 | parser.add_argument('uri', help='Google Storage URI to download') |
| 29 | parser.add_argument('filename', help='Location to store the file.') |
| 30 | return parser |
| 31 | |
| 32 | def Copy(ctx, uri, filename): |
| 33 | """Run the copy using a temp file.""" |
| 34 | temp_path = '%s.tmp' % filename |
| 35 | osutils.SafeUnlink(temp_path) |
| 36 | try: |
| 37 | ctx.Copy(uri, temp_path, log_output=True) |
| 38 | shutil.move(temp_path, filename) |
| 39 | finally: |
| 40 | osutils.SafeUnlink(temp_path) |
| 41 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 42 | def main(argv): |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 43 | parser = GetParser() |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 44 | options = parser.parse_args(argv) |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 45 | ctx = gs.GSContext(retries=0) |
| 46 | try: |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame^] | 47 | retry_util.RetryCommand(Copy, ctx.DEFAULT_RETRIES, ctx, options.uri, |
| 48 | options.filename, sleep=ctx.DEFAULT_SLEEP_TIME) |
David James | 0f5252f | 2013-04-19 08:03:14 -0700 | [diff] [blame] | 49 | except (gs.GSContextException, cros_build_lib.RunCommandError) as ex: |
| 50 | # Hide the stack trace using Die. |
| 51 | cros_build_lib.Die('%s', ex) |