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 urllib2 | 17 import urllib2 |
18 import urlparse | |
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') |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: | 115 with zipfile.ZipFile(target_zip_filename, 'r') as target_zip: |
115 target_zip.extractall(bin_dir) | 116 target_zip.extractall(bin_dir) |
116 | 117 |
117 # Final check that the gsutil bin is okay. This should never fail. | 118 # Final check that the gsutil bin is okay. This should never fail. |
118 if not check_gsutil(gsutil_bin): | 119 if not check_gsutil(gsutil_bin): |
119 raise InvalidGsutilError() | 120 raise InvalidGsutilError() |
120 | 121 |
121 return gsutil_bin | 122 return gsutil_bin |
122 | 123 |
123 | 124 |
125 def get_boto_proxy_options_from_environment(): | |
Ryan Tseng
2015/01/07 22:21:58
for completeness sake (since I've seen all these p
| |
126 """Converts the contents of the 'http_proxy' environment variable into the | |
Ryan Tseng
2015/01/07 22:21:58
"""One line
More lines
...
"""
So rewrite this a
| |
127 corresponding Boto options that gsutil accepts.""" | |
128 if 'http_proxy' not in os.environ: | |
129 return [] | |
130 proxy_url = urlparse.urlsplit(os.environ['http_proxy']) | |
131 if proxy_url.hostname is None or proxy_url.port is None: | |
132 raise EnvironmentError( | |
133 'The "http_proxy" environment is set, but has an invalid format. Its ' | |
Ryan Tseng
2015/01/07 22:21:58
2 indents
| |
134 'format must be "http://[user[:password]]host:port".') | |
135 boto_options = ['-o', 'Boto:proxy=%s' % proxy_url.hostname, | |
136 '-o', 'Boto:proxy_port=%d' % proxy_url.port] | |
137 if proxy_url.username: | |
138 boto_options.extend(['-o', 'Boto:proxy_user=%s' % proxy_url.username]) | |
139 if proxy_url.password: | |
140 boto_options.extend(['-o', 'Boto:proxy_pass=%s' % proxy_url.password]) | |
141 return boto_options | |
142 | |
143 | |
124 def run_gsutil(force_version, fallback, target, args): | 144 def run_gsutil(force_version, fallback, target, args): |
125 if force_version: | 145 if force_version: |
126 gsutil_bin = ensure_gsutil(force_version, target) | 146 gsutil_bin = ensure_gsutil(force_version, target) |
127 else: | 147 else: |
128 gsutil_bin = fallback | 148 gsutil_bin = fallback |
129 cmd = [sys.executable, gsutil_bin] + args | 149 proxy_options = get_boto_proxy_options_from_environment() |
150 cmd = [sys.executable, gsutil_bin] + proxy_options + args | |
130 try: | 151 try: |
131 call(cmd) | 152 call(cmd) |
132 except SubprocessError as e: | 153 except SubprocessError as e: |
133 return e.code | 154 return e.code |
134 return 0 | 155 return 0 |
135 | 156 |
136 | 157 |
137 def parse_args(): | 158 def parse_args(): |
138 parser = argparse.ArgumentParser() | 159 parser = argparse.ArgumentParser() |
139 parser.add_argument('--force-version') | 160 parser.add_argument('--force-version') |
140 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) | 161 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) |
141 parser.add_argument('--target', default=DEFAULT_BIN_DIR) | 162 parser.add_argument('--target', default=DEFAULT_BIN_DIR) |
142 parser.add_argument('args', nargs=argparse.REMAINDER) | 163 parser.add_argument('args', nargs=argparse.REMAINDER) |
143 | 164 |
144 args, extras = parser.parse_known_args() | 165 args, extras = parser.parse_known_args() |
145 if args.args and args.args[0] == '--': | 166 if args.args and args.args[0] == '--': |
146 args.args.pop(0) | 167 args.args.pop(0) |
147 if extras: | 168 if extras: |
148 args.args = extras + args.args | 169 args.args = extras + args.args |
149 return args.force_version, args.fallback, args.target, args.args | 170 return args.force_version, args.fallback, args.target, args.args |
150 | 171 |
151 | 172 |
152 def main(): | 173 def main(): |
153 force_version, fallback, target, args = parse_args() | 174 force_version, fallback, target, args = parse_args() |
154 return run_gsutil(force_version, fallback, target, args) | 175 return run_gsutil(force_version, fallback, target, args) |
155 | 176 |
156 if __name__ == '__main__': | 177 if __name__ == '__main__': |
157 sys.exit(main()) | 178 sys.exit(main()) |
OLD | NEW |