Chromium Code Reviews

Side by Side Diff: gsutil.py

Issue 809053002: gsutil: Use urllib2 instead of urllib. (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Created 6 years 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
M-A Ruel 2014/12/17 16:01:45 Ugh, can you sort the list while at it? Thanks.
10 import shutil 10 import shutil
11 import zipfile 11 import zipfile
12 import hashlib 12 import hashlib
13 import base64 13 import base64
14 import os 14 import os
15 import sys 15 import sys
16 import json 16 import json
17 import urllib 17 import urllib2
18 import subprocess 18 import subprocess
19 19
20 20
21 GSUTIL_URL = 'https://storage.googleapis.com/pub/' 21 GSUTIL_URL = 'https://storage.googleapis.com/pub/'
22 API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/' 22 API_URL = 'https://www.googleapis.com/storage/v1/b/pub/o/'
23 23
24 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) 24 THIS_DIR = os.path.dirname(os.path.abspath(__file__))
25 DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil') 25 DEFAULT_BIN_DIR = os.path.join(THIS_DIR, 'external_bin', 'gsutil')
26 DEFAULT_FALLBACK_GSUTIL = os.path.join( 26 DEFAULT_FALLBACK_GSUTIL = os.path.join(
27 THIS_DIR, 'third_party', 'gsutil', 'gsutil') 27 THIS_DIR, 'third_party', 'gsutil', 'gsutil')
(...skipping 32 matching lines...)
60 md5_calc = hashlib.md5() 60 md5_calc = hashlib.md5()
61 with open(target_filename, 'rb') as f: 61 with open(target_filename, 'rb') as f:
62 while True: 62 while True:
63 buf = f.read(4096) 63 buf = f.read(4096)
64 if not buf: 64 if not buf:
65 break 65 break
66 md5_calc.update(buf) 66 md5_calc.update(buf)
67 local_md5 = md5_calc.hexdigest() 67 local_md5 = md5_calc.hexdigest()
68 68
69 metadata_url = '%s%s' % (API_URL, filename) 69 metadata_url = '%s%s' % (API_URL, filename)
70 metadata = json.load(urllib.urlopen(metadata_url)) 70 metadata = json.load(urllib2.urlopen(metadata_url))
71 remote_md5 = base64.b64decode(metadata['md5Hash']) 71 remote_md5 = base64.b64decode(metadata['md5Hash'])
72 72
73 if local_md5 == remote_md5: 73 if local_md5 == remote_md5:
74 return target_filename 74 return target_filename
75 os.remove(target_filename) 75 os.remove(target_filename)
76 76
77 # Do the download. 77 # Do the download.
78 url = '%s%s' % (GSUTIL_URL, filename) 78 url = '%s%s' % (GSUTIL_URL, filename)
79 u = urllib.urlopen(url) 79 u = urllib2.urlopen(url)
80 with open(target_filename, 'wb') as f: 80 with open(target_filename, 'wb') as f:
81 while True: 81 while True:
82 buf = u.read(4096) 82 buf = u.read(4096)
83 if not buf: 83 if not buf:
84 break 84 break
85 f.write(buf) 85 f.write(buf)
86 return target_filename 86 return target_filename
87 87
88 88
89 def check_gsutil(gsutil_bin): 89 def check_gsutil(gsutil_bin):
(...skipping 48 matching lines...)
138 args = parser.parse_args() 138 args = parser.parse_args()
139 return args.force_version, args.fallback, args.target, args.args 139 return args.force_version, args.fallback, args.target, args.args
140 140
141 141
142 def main(): 142 def main():
143 force_version, fallback, target, args = parse_args() 143 force_version, fallback, target, args = parse_args()
144 run_gsutil(force_version, fallback, target, args) 144 run_gsutil(force_version, fallback, target, args)
145 145
146 if __name__ == '__main__': 146 if __name__ == '__main__':
147 sys.exit(main()) 147 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