Chromium Code Reviews

Side by Side Diff: gsutil.py

Issue 841823002: gsutil: Pass proxy information via Boto options. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Patch v2 Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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...)
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():
126 """Derive proxy options for gsutil from the environment.
127
128 Parses the contents of the 'http_proxy' (and variations) environment variable
129 and converts it to the corresponding Boto options that gsutil accepts.
130 """
131 proxy_variable = None
132 for env_var in ('http_proxy', 'HTTP_PROXY', 'https_proxy', 'HTTPS_PROXY'):
agable 2015/01/08 20:39:01 Shouldn't we prioritize an https proxy over an htt
Raphael Kubo da Costa (rakuco) 2015/01/08 22:29:02 I don't have a strong opinion here, as the whole t
133 if env_var in os.environ:
134 proxy_variable = env_var
135 break
136 if proxy_variable is None:
137 return []
138
139 proxy_url = urlparse.urlsplit(os.environ[proxy_variable])
140 if proxy_url.hostname is None or proxy_url.port is None:
141 raise EnvironmentError(
142 'The "%s" environment is set, but has an invalid format. Its '
143 'format must be "http://[user[:password]]host:port".' % proxy_variable)
agable 2015/01/08 20:39:01 [user[:password]@]host:port
144 boto_options = ['-o', 'Boto:proxy=%s' % proxy_url.hostname,
145 '-o', 'Boto:proxy_port=%d' % proxy_url.port]
146 if proxy_url.username:
147 boto_options.extend(['-o', 'Boto:proxy_user=%s' % proxy_url.username])
148 if proxy_url.password:
149 boto_options.extend(['-o', 'Boto:proxy_pass=%s' % proxy_url.password])
150 return boto_options
151
152
124 def run_gsutil(force_version, fallback, target, args): 153 def run_gsutil(force_version, fallback, target, args):
125 if force_version: 154 if force_version:
126 gsutil_bin = ensure_gsutil(force_version, target) 155 gsutil_bin = ensure_gsutil(force_version, target)
156 proxy_options = get_boto_proxy_options_from_environment()
127 else: 157 else:
128 gsutil_bin = fallback 158 gsutil_bin = fallback
129 cmd = [sys.executable, gsutil_bin] + args 159 proxy_options = [] # Not available in the fallback gsutil.
160 cmd = [sys.executable, gsutil_bin] + proxy_options + args
130 try: 161 try:
131 call(cmd) 162 call(cmd)
132 except SubprocessError as e: 163 except SubprocessError as e:
133 return e.code 164 return e.code
134 return 0 165 return 0
135 166
136 167
137 def parse_args(): 168 def parse_args():
138 parser = argparse.ArgumentParser() 169 parser = argparse.ArgumentParser()
139 parser.add_argument('--force-version') 170 parser.add_argument('--force-version')
140 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) 171 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL)
141 parser.add_argument('--target', default=DEFAULT_BIN_DIR) 172 parser.add_argument('--target', default=DEFAULT_BIN_DIR)
142 parser.add_argument('args', nargs=argparse.REMAINDER) 173 parser.add_argument('args', nargs=argparse.REMAINDER)
143 174
144 args, extras = parser.parse_known_args() 175 args, extras = parser.parse_known_args()
145 if args.args and args.args[0] == '--': 176 if args.args and args.args[0] == '--':
146 args.args.pop(0) 177 args.args.pop(0)
147 if extras: 178 if extras:
148 args.args = extras + args.args 179 args.args = extras + args.args
149 return args.force_version, args.fallback, args.target, args.args 180 return args.force_version, args.fallback, args.target, args.args
150 181
151 182
152 def main(): 183 def main():
153 force_version, fallback, target, args = parse_args() 184 force_version, fallback, target, args = parse_args()
154 return run_gsutil(force_version, fallback, target, args) 185 return run_gsutil(force_version, fallback, target, args)
155 186
156 if __name__ == '__main__': 187 if __name__ == '__main__':
157 sys.exit(main()) 188 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine