OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Run a pinned gsutil.""" | 6 """Run a pinned gsutil.""" |
7 | 7 |
8 | 8 |
9 import argparse | 9 import argparse |
10 import base64 | 10 import base64 |
11 import hashlib | 11 import hashlib |
12 import json | 12 import json |
13 import os | 13 import os |
14 import shutil | 14 import shutil |
15 import subprocess | 15 import subprocess |
16 import sys | 16 import sys |
17 import tempfile | |
18 import time | |
17 import urllib2 | 19 import urllib2 |
18 import zipfile | 20 import zipfile |
19 | 21 |
20 | 22 |
21 GSUTIL_URL = 'https://storage.googleapis.com/pub/' | 23 GSUTIL_URL = 'https://storage.googleapis.com/pub/' |
22 API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' | 24 API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' |
23 | 25 |
24 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | 26 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
25 DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') | 27 DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') |
26 DEFAULT_FALLBACK_GSUTIL = os.path.join( | 28 DEFAULT_FALLBACK_GSUTIL = os.path.join( |
27 THIS_DIR, 'third_party', 'gsutil', 'gsutil') | 29 THIS_DIR, 'third_party', 'gsutil', 'gsutil') |
28 | 30 |
29 | |
30 class InvalidGsutilError(Exception): | 31 class InvalidGsutilError(Exception): |
31 pass | 32 pass |
32 | 33 |
33 | 34 |
34 def download_gsutil(version, target_dir): | 35 def download_gsutil(version, target_dir): |
35 """Downloads gsutil into the target_dir.""" | 36 """Downloads gsutil into the target_dir.""" |
36 filename = 'gsutil_%s.zip' % version | 37 filename = 'gsutil_%s.zip' % version |
37 target_filename = os.path.join(target_dir, filename) | 38 target_filename = os.path.join(target_dir, filename) |
38 | 39 |
39 # Check if the target exists already. | 40 # Check if the target exists already. |
(...skipping 26 matching lines...) Expand all Loading... | |
66 f.write(buf) | 67 f.write(buf) |
67 return target_filename | 68 return target_filename |
68 | 69 |
69 | 70 |
70 def check_gsutil(gsutil_bin): | 71 def check_gsutil(gsutil_bin): |
71 """Run gsutil version and make sure it runs.""" | 72 """Run gsutil version and make sure it runs.""" |
72 return subprocess.call( | 73 return subprocess.call( |
73 [sys.executable, gsutil_bin, 'version'], | 74 [sys.executable, gsutil_bin, 'version'], |
74 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) == 0 | 75 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) == 0 |
75 | 76 |
76 def ensure_gsutil(version, target): | 77 def ensure_gsutil(version, target, clean): |
77 bin_dir = os.path.join(target, 'gsutil_%s' % version) | 78 bin_dir = os.path.join(target, 'gsutil_%s' % version) |
78 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') | 79 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') |
79 if os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): | 80 if not clean and os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): |
80 # Everything is awesome! we're all done here. | 81 # Everything is awesome! we're all done here. |
81 return gsutil_bin | 82 return gsutil_bin |
82 | 83 |
83 if os.path.isdir(bin_dir): | 84 # Clean up if we're redownloading a corrupted gsutil. |
84 # Clean up if we're redownloading a corrupted gsutil. | 85 suffix = '%d_%d' % (time.time(), os.getpid()) |
85 shutil.rmtree(bin_dir) | 86 try: |
86 cache_dir = os.path.join(target, '.cache_dir') | 87 cleanup_path = '%s.%s' % (bin_dir, suffix) |
87 if not os.path.isdir(cache_dir): | 88 shutil.move(bin_dir, cleanup_path) |
88 os.makedirs(cache_dir) | 89 shutil.rmtree(cleanup_path) |
David James
2015/09/17 20:44:31
did you intend to catch errors from rmtree?
dnj (Google)
2015/09/17 21:16:39
Hmm, probably shouldn't.
| |
89 target_zip_filename = download_gsutil(version, cache_dir) | 90 except IOError: |
David James
2015/09/17 20:44:31
These can return OSError too I believe.
dnj (Google)
2015/09/17 21:16:39
Multi-user, gross. I don't think that's an intende
| |
90 with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: | 91 # Directory was not present. |
91 target_zip.extractall(bin_dir) | 92 pass |
92 | 93 |
93 # Final check that the gsutil bin is okay. This should never fail. | 94 download_dir = tempfile.mkdtemp(prefix='gsutil_py') |
94 if not check_gsutil(gsutil_bin): | 95 try: |
95 raise InvalidGsutilError() | 96 target_zip_filename = download_gsutil(version, download_dir) |
97 with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: | |
98 target_zip.extractall(download_dir) | |
96 | 99 |
100 try: | |
101 shutil.move(download_dir, bin_dir) | |
102 except IOError: | |
David James
2015/09/17 20:44:31
shutil.Error:
dnj (Google)
2015/09/17 21:16:39
Hah did that before I read your comment. Done.
| |
103 # Something else did this in parallel. | |
104 pass | |
105 | |
106 # Final check that the gsutil bin is okay. This should never fail. | |
107 if not check_gsutil(gsutil_bin): | |
108 raise InvalidGsutilError() | |
109 finally: | |
110 if os.path.isdir(download_dir): | |
111 shutil.rmtree(download_dir) | |
97 return gsutil_bin | 112 return gsutil_bin |
98 | 113 |
99 | 114 |
100 def run_gsutil(force_version, fallback, target, args): | 115 def run_gsutil(force_version, fallback, target, args, clean=False): |
101 if force_version: | 116 if force_version: |
102 gsutil_bin = ensure_gsutil(force_version, target) | 117 gsutil_bin = ensure_gsutil(force_version, target, clean) |
103 else: | 118 else: |
104 gsutil_bin = fallback | 119 gsutil_bin = fallback |
105 cmd = [sys.executable, gsutil_bin] + args | 120 cmd = [sys.executable, gsutil_bin] + args |
106 return subprocess.call(cmd) | 121 return subprocess.call(cmd) |
107 | 122 |
108 | 123 |
109 def parse_args(): | 124 def parse_args(): |
125 bin_dir = os.environ.get('DEPOT_TOOLS_GSUTIL_BIN_DIR', DEFAULT_BIN_DIR) | |
126 | |
110 parser = argparse.ArgumentParser() | 127 parser = argparse.ArgumentParser() |
111 parser.add_argument('--force-version', default='4.13') | 128 parser.add_argument('--force-version', default='4.13') |
129 parser.add_argument('--clean', action='store_true', | |
130 help='Clear any existing gsutil package, forcing a new download.') | |
112 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) | 131 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) |
113 parser.add_argument('--target', default=DEFAULT_BIN_DIR) | 132 parser.add_argument('--target', default=bin_dir, |
133 help='The target directory to download/store a gsutil version in. ' | |
134 '(default is %(default)s).') | |
114 parser.add_argument('args', nargs=argparse.REMAINDER) | 135 parser.add_argument('args', nargs=argparse.REMAINDER) |
115 | 136 |
116 args, extras = parser.parse_known_args() | 137 args, extras = parser.parse_known_args() |
117 if args.args and args.args[0] == '--': | 138 if args.args and args.args[0] == '--': |
118 args.args.pop(0) | 139 args.args.pop(0) |
119 if extras: | 140 if extras: |
120 args.args = extras + args.args | 141 args.args = extras + args.args |
121 return args.force_version, args.fallback, args.target, args.args | 142 return args |
122 | 143 |
123 | 144 |
124 def main(): | 145 def main(): |
125 force_version, fallback, target, args = parse_args() | 146 args = parse_args() |
126 return run_gsutil(force_version, fallback, target, args) | 147 return run_gsutil(args.force_version, args.fallback, args.target, args.args, |
148 clean=args.clean) | |
127 | 149 |
128 if __name__ == '__main__': | 150 if __name__ == '__main__': |
129 sys.exit(main()) | 151 sys.exit(main()) |
OLD | NEW |