blob: cce796fcd412e5b244d78e2f3ea0d09d3215aeff [file] [log] [blame]
David James0f5252f2013-04-19 08:03:14 -07001# 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
7This 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
16import shutil
17
18from chromite.lib import commandline
19from chromite.lib import cros_build_lib
20from chromite.lib import gs
21from chromite.lib import osutils
22
23
24def GetParser():
25 """Creates the argparse parser."""
26 parser = commandline.ArgumentParser(description=__doc__)
27 parser.add_argument('uri', help='Google Storage URI to download')
28 parser.add_argument('filename', help='Location to store the file.')
29 return parser
30
31def Copy(ctx, uri, filename):
32 """Run the copy using a temp file."""
33 temp_path = '%s.tmp' % filename
34 osutils.SafeUnlink(temp_path)
35 try:
36 ctx.Copy(uri, temp_path, log_output=True)
37 shutil.move(temp_path, filename)
38 finally:
39 osutils.SafeUnlink(temp_path)
40
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040041def main(argv):
David James0f5252f2013-04-19 08:03:14 -070042 parser = GetParser()
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040043 options = parser.parse_args(argv)
David James0f5252f2013-04-19 08:03:14 -070044 ctx = gs.GSContext(retries=0)
45 try:
46 cros_build_lib.RetryCommand(Copy, ctx.DEFAULT_RETRIES, ctx, options.uri,
47 options.filename, sleep=ctx.DEFAULT_SLEEP_TIME)
48 except (gs.GSContextException, cros_build_lib.RunCommandError) as ex:
49 # Hide the stack trace using Die.
50 cros_build_lib.Die('%s', ex)