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 |
| 11 import hashlib |
| 12 import json |
| 13 import os |
10 import shutil | 14 import shutil |
| 15 import subprocess |
| 16 import sys |
| 17 import urllib2 |
11 import zipfile | 18 import zipfile |
12 import hashlib | |
13 import base64 | |
14 import os | |
15 import sys | |
16 import json | |
17 import urllib | |
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') |
28 | 28 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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...) Expand 10 before | Expand all | Expand 10 after Loading... |
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()) |
OLD | NEW |