Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

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 v3 with different order Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« 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...) Expand 10 before | Expand all | Expand 10 after
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[s]_proxy' (and variations) environment
129 variable and converts it to the corresponding Boto options that gsutil
130 accepts.
131 """
132 proxy_variable = None
Primiano Tucci (use gerrit) 2015/01/09 17:17:31 I think you can squeeze lines 132-136 in: vars = (
133 for env_var in ('https_proxy', 'HTTPS_PROXY', 'http_proxy', 'HTTP_PROXY'):
134 if env_var in os.environ:
135 proxy_variable = env_var
136 break
137 if proxy_variable is None:
Primiano Tucci (use gerrit) 2015/01/09 17:17:31 I think here you want to check: if not proxy_varia
Raphael Kubo da Costa 2015/01/09 17:54:12 Doing this works, but if I only store os.environ[v
138 return []
139
140 proxy_url = urlparse.urlsplit(os.environ[proxy_variable])
141 if proxy_url.hostname is None or proxy_url.port is None:
142 raise EnvironmentError(
Primiano Tucci (use gerrit) 2015/01/09 17:17:31 Not sure you want to be so strict on the port. Wha
Raphael Kubo da Costa 2015/01/09 17:54:12 As far as I can see, httplib2 (which gsutil uses)
143 'The "%s" environment is set, but has an invalid format. Its '
144 'format must be "http://[user[:password]@]host:port".' % proxy_variable)
145 boto_options = ['-o', 'Boto:proxy=%s' % proxy_url.hostname,
146 '-o', 'Boto:proxy_port=%d' % proxy_url.port]
147 if proxy_url.username:
148 boto_options.extend(['-o', 'Boto:proxy_user=%s' % proxy_url.username])
149 if proxy_url.password:
150 boto_options.extend(['-o', 'Boto:proxy_pass=%s' % proxy_url.password])
151 return boto_options
152
153
124 def run_gsutil(force_version, fallback, target, args): 154 def run_gsutil(force_version, fallback, target, args):
125 if force_version: 155 if force_version:
126 gsutil_bin = ensure_gsutil(force_version, target) 156 gsutil_bin = ensure_gsutil(force_version, target)
157 proxy_options = get_boto_proxy_options_from_environment()
127 else: 158 else:
128 gsutil_bin = fallback 159 gsutil_bin = fallback
129 cmd = [sys.executable, gsutil_bin] + args 160 proxy_options = [] # Not available in the fallback gsutil.
161 cmd = [sys.executable, gsutil_bin] + proxy_options + args
130 try: 162 try:
131 call(cmd) 163 call(cmd)
132 except SubprocessError as e: 164 except SubprocessError as e:
133 return e.code 165 return e.code
134 return 0 166 return 0
135 167
136 168
137 def parse_args(): 169 def parse_args():
138 parser = argparse.ArgumentParser() 170 parser = argparse.ArgumentParser()
139 parser.add_argument('--force-version') 171 parser.add_argument('--force-version')
140 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL) 172 parser.add_argument('--fallback', default=DEFAULT_FALLBACK_GSUTIL)
141 parser.add_argument('--target', default=DEFAULT_BIN_DIR) 173 parser.add_argument('--target', default=DEFAULT_BIN_DIR)
142 parser.add_argument('args', nargs=argparse.REMAINDER) 174 parser.add_argument('args', nargs=argparse.REMAINDER)
143 175
144 args, extras = parser.parse_known_args() 176 args, extras = parser.parse_known_args()
145 if args.args and args.args[0] == '--': 177 if args.args and args.args[0] == '--':
146 args.args.pop(0) 178 args.args.pop(0)
147 if extras: 179 if extras:
148 args.args = extras + args.args 180 args.args = extras + args.args
149 return args.force_version, args.fallback, args.target, args.args 181 return args.force_version, args.fallback, args.target, args.args
150 182
151 183
152 def main(): 184 def main():
153 force_version, fallback, target, args = parse_args() 185 force_version, fallback, target, args = parse_args()
154 return run_gsutil(force_version, fallback, target, args) 186 return run_gsutil(force_version, fallback, target, args)
155 187
156 if __name__ == '__main__': 188 if __name__ == '__main__':
157 sys.exit(main()) 189 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
This is Rietveld 408576698