blob: a4a5ef10614494af7b6e24c9c1dd639d936e6201 [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
Mike Frysinger383367e2014-09-16 15:06:17 -040016from __future__ import print_function
17
David James0f5252f2013-04-19 08:03:14 -070018import shutil
19
20from chromite.lib import commandline
21from chromite.lib import cros_build_lib
22from chromite.lib import gs
23from chromite.lib import osutils
24
25
26def GetParser():
27 """Creates the argparse parser."""
28 parser = commandline.ArgumentParser(description=__doc__)
Mike Frysingerc9bfb472015-05-13 23:11:58 -040029 parser.add_argument('--boto', type='path', help='Path to boto auth file.')
Mike Frysinger74c70612015-12-02 18:01:41 -050030 parser.add_argument('uri', type='gs_path',
31 help='Google Storage URI to download')
32 parser.add_argument('filename', type='path',
33 help='Location to store the file.')
David James0f5252f2013-04-19 08:03:14 -070034 return parser
35
Mike Frysinger33494002014-05-07 23:46:08 -040036
David James0f5252f2013-04-19 08:03:14 -070037def Copy(ctx, uri, filename):
38 """Run the copy using a temp file."""
39 temp_path = '%s.tmp' % filename
40 osutils.SafeUnlink(temp_path)
41 try:
Mike Frysinger33494002014-05-07 23:46:08 -040042 ctx.Copy(uri, temp_path)
David James0f5252f2013-04-19 08:03:14 -070043 shutil.move(temp_path, filename)
44 finally:
45 osutils.SafeUnlink(temp_path)
46
Mike Frysinger33494002014-05-07 23:46:08 -040047
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040048def main(argv):
David James0f5252f2013-04-19 08:03:14 -070049 parser = GetParser()
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040050 options = parser.parse_args(argv)
Mike Frysingerc9bfb472015-05-13 23:11:58 -040051 options.Freeze()
52 ctx = gs.GSContext(boto_file=options.boto)
David James0f5252f2013-04-19 08:03:14 -070053 try:
Mike Frysinger11291062014-08-18 00:01:11 -040054 Copy(ctx, options.uri, options.filename)
55 except gs.GSContextException as ex:
David James0f5252f2013-04-19 08:03:14 -070056 # Hide the stack trace using Die.
57 cros_build_lib.Die('%s', ex)