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 time | |
17 import urllib2 | 18 import urllib2 |
18 import zipfile | 19 import zipfile |
19 | 20 |
20 | 21 |
21 GSUTIL_URL = 'https://storage.googleapis.com/pub/' | 22 GSUTIL_URL = 'https://storage.googleapis.com/pub/' |
22 API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' | 23 API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' |
23 | 24 |
24 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | 25 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
25 DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') | 26 DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') |
26 DEFAULT_FALLBACK_GSUTIL = os.path.join( | 27 DEFAULT_FALLBACK_GSUTIL = os.path.join( |
27 THIS_DIR, 'third_party', 'gsutil', 'gsutil') | 28 THIS_DIR, 'third_party', 'gsutil', 'gsutil') |
28 | 29 |
29 | |
30 class InvalidGsutilError(Exception): | 30 class InvalidGsutilError(Exception): |
31 pass | 31 pass |
32 | 32 |
33 | 33 |
34 def download_gsutil(version, target_dir): | 34 def download_gsutil(version, target_dir): |
35 """Downloads gsutil into the target_dir.""" | 35 """Downloads gsutil into the target_dir.""" |
36 filename = 'gsutil_%s.zip' % version | 36 filename = 'gsutil_%s.zip' % version |
37 target_filename = os.path.join(target_dir, filename) | 37 target_filename = os.path.join(target_dir, filename) |
38 | 38 |
39 # Check if the target exists already. | 39 # Check if the target exists already. |
(...skipping 26 matching lines...) Expand all Loading... | |
66 f.write(buf) | 66 f.write(buf) |
67 return target_filename | 67 return target_filename |
68 | 68 |
69 | 69 |
70 def check_gsutil(gsutil_bin): | 70 def check_gsutil(gsutil_bin): |
71 """Run gsutil version and make sure it runs.""" | 71 """Run gsutil version and make sure it runs.""" |
72 return subprocess.call( | 72 return subprocess.call( |
73 [sys.executable, gsutil_bin, 'version'], | 73 [sys.executable, gsutil_bin, 'version'], |
74 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) == 0 | 74 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) == 0 |
75 | 75 |
76 def ensure_gsutil(version, target): | 76 def ensure_gsutil(version, target, clean): |
77 bin_dir = os.path.join(target, 'gsutil_%s' % version) | 77 bin_dir = os.path.join(target, 'gsutil_%s' % version) |
78 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') | 78 gsutil_bin = os.path.join(bin_dir, 'gsutil', 'gsutil') |
79 if os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): | 79 if not clean and os.path.isfile(gsutil_bin) and check_gsutil(gsutil_bin): |
80 # Everything is awesome! we're all done here. | 80 # Everything is awesome! we're all done here. |
81 return gsutil_bin | 81 return gsutil_bin |
82 | 82 |
83 if os.path.isdir(bin_dir): | 83 # Clean up if we're redownloading a corrupted gsutil. |
84 # Clean up if we're redownloading a corrupted gsutil. | 84 suffix = '%d_%d' % (time.time(), os.getpid()) |
vapier
2015/09/17 19:31:00
this isn't parallel safe. you cannot rely on PID'
dnj
2015/09/17 19:51:47
Using "tempfile.mkdtemp" now.
| |
85 shutil.rmtree(bin_dir) | 85 try: |
86 cache_dir = os.path.join(target, '.cache_dir') | 86 cleanup_path = '%s.%s' % (bin_dir, suffix) |
87 shutil.move(bin_dir, cleanup_path) | |
88 shutil.rmtree(cleanup_path) | |
89 except IOError: | |
90 # Directory was not present. | |
91 pass | |
92 | |
93 cache_dir = os.path.join(target, '.cache_dir.%s' % (suffix,)) | |
Ryan Tseng
2015/09/17 19:26:45
this would be more aptly called "tempdir" now?
ma
dnj
2015/09/17 19:51:46
Acknowledged.
| |
87 if not os.path.isdir(cache_dir): | 94 if not os.path.isdir(cache_dir): |
88 os.makedirs(cache_dir) | 95 os.makedirs(cache_dir) |
89 target_zip_filename = download_gsutil(version, cache_dir) | 96 target_zip_filename = download_gsutil(version, cache_dir) |
90 with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: | 97 with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: |
91 target_zip.extractall(bin_dir) | 98 target_zip.extractall(cache_dir) |
99 | |
100 try: | |
101 shutil.move(cache_dir, bin_dir) | |
102 except IOError: | |
103 # Something else did this in parallel. | |
104 pass | |
92 | 105 |
93 # Final check that the gsutil bin is okay. This should never fail. | 106 # Final check that the gsutil bin is okay. This should never fail. |
94 if not check_gsutil(gsutil_bin): | 107 if not check_gsutil(gsutil_bin): |
95 raise InvalidGsutilError() | 108 raise InvalidGsutilError() |
96 | 109 |
97 return gsutil_bin | 110 return gsutil_bin |
98 | 111 |
99 | 112 |
100 def run_gsutil(force_version, fallback, target, args): | 113 def run_gsutil(force_version, fallback, target, args, clean=False): |
101 if force_version: | 114 if force_version: |
102 gsutil_bin = ensure_gsutil(force_version, target) | 115 gsutil_bin = ensure_gsutil(force_version, target, clean) |
103 else: | 116 else: |
104 gsutil_bin = fallback | 117 gsutil_bin = fallback |
105 cmd = [sys.executable, gsutil_bin] + args | 118 cmd = [sys.executable, gsutil_bin] + args |
106 return subprocess.call(cmd) | 119 return subprocess.call(cmd) |
107 | 120 |
108 | 121 |
109 def parse_args(): | 122 def parse_args(): |
123 bin_dir = os.environ.get('DEPOT_TOOLS_GSUTIL_BIN_DIR') | |
Ryan Tseng
2015/09/17 19:26:45
get(key, default)
dnj
2015/09/17 19:51:47
Done.
| |
124 if not bin_dir: | |
125 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', | |
Ryan Tseng
2015/09/17 19:26:45
What do you see the use case for this being?
dnj
2015/09/17 19:51:47
Useful for testing massively in parallel.
| |
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 |