Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / scripts / gs_fetch_binpkg.py
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 from __future__ import print_function
17
18 import shutil
19
20 from chromite.lib import commandline
21 from chromite.lib import cros_build_lib
22 from chromite.lib import gs
23 from chromite.lib import osutils
24
25
26 def GetParser():
27   """Creates the argparse parser."""
28   parser = commandline.ArgumentParser(description=__doc__)
29   parser.add_argument('uri', help='Google Storage URI to download')
30   parser.add_argument('filename', help='Location to store the file.')
31   return parser
32
33
34 def Copy(ctx, uri, filename):
35   """Run the copy using a temp file."""
36   temp_path = '%s.tmp' % filename
37   osutils.SafeUnlink(temp_path)
38   try:
39     ctx.Copy(uri, temp_path)
40     shutil.move(temp_path, filename)
41   finally:
42     osutils.SafeUnlink(temp_path)
43
44
45 def main(argv):
46   parser = GetParser()
47   options = parser.parse_args(argv)
48   ctx = gs.GSContext()
49   try:
50     Copy(ctx, options.uri, options.filename)
51   except gs.GSContextException as ex:
52     # Hide the stack trace using Die.
53     cros_build_lib.Die('%s', ex)