Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Download files from Google Storage based on SHA1 sums.""" | 6 """Download files from Google Storage based on SHA1 sums.""" |
| 7 | 7 |
| 8 | 8 |
| 9 import hashlib | 9 import hashlib |
| 10 import optparse | 10 import optparse |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 return False | 213 return False |
| 214 return True | 214 return True |
| 215 return all(map(_validate, tar.getmembers())) | 215 return all(map(_validate, tar.getmembers())) |
| 216 | 216 |
| 217 def _downloader_worker_thread(thread_num, q, force, base_url, | 217 def _downloader_worker_thread(thread_num, q, force, base_url, |
| 218 gsutil, out_q, ret_codes, verbose, extract, | 218 gsutil, out_q, ret_codes, verbose, extract, |
| 219 delete=True): | 219 delete=True): |
| 220 while True: | 220 while True: |
| 221 input_sha1_sum, output_filename = q.get() | 221 input_sha1_sum, output_filename = q.get() |
| 222 if input_sha1_sum is None: | 222 if input_sha1_sum is None: |
| 223 return | 223 return |
|
Ryan Tseng
2016/04/19 21:07:52
extract_dir = None
pauljensen
2016/04/20 11:37:00
Done.
| |
| 224 if extract: | |
| 225 if not output_filename.endswith('.tar.gz'): | |
| 226 out_q.put('%d> Error: %s is not a tar.gz archive.' % ( | |
| 227 thread_num, output_filename)) | |
| 228 ret_codes.put((1, '%s is not a tar.gz archive.' % (output_filename))) | |
| 229 continue | |
| 230 extract_dir = output_filename[0:len(output_filename)-7] | |
| 224 if os.path.exists(output_filename) and not force: | 231 if os.path.exists(output_filename) and not force: |
| 225 if get_sha1(output_filename) == input_sha1_sum: | 232 if not extract or os.path.exists(extract_dir): |
| 226 if verbose: | 233 if get_sha1(output_filename) == input_sha1_sum: |
| 227 out_q.put( | 234 if verbose: |
| 228 '%d> File %s exists and SHA1 matches. Skipping.' % ( | 235 out_q.put( |
| 229 thread_num, output_filename)) | 236 '%d> File %s exists and SHA1 matches. Skipping.' % ( |
| 230 continue | 237 thread_num, output_filename)) |
| 238 continue | |
| 231 # Check if file exists. | 239 # Check if file exists. |
| 232 file_url = '%s/%s' % (base_url, input_sha1_sum) | 240 file_url = '%s/%s' % (base_url, input_sha1_sum) |
| 233 (code, _, err) = gsutil.check_call('ls', file_url) | 241 (code, _, err) = gsutil.check_call('ls', file_url) |
| 234 if code != 0: | 242 if code != 0: |
| 235 if code == 404: | 243 if code == 404: |
| 236 out_q.put('%d> File %s for %s does not exist, skipping.' % ( | 244 out_q.put('%d> File %s for %s does not exist, skipping.' % ( |
| 237 thread_num, file_url, output_filename)) | 245 thread_num, file_url, output_filename)) |
| 238 ret_codes.put((1, 'File %s for %s does not exist.' % ( | 246 ret_codes.put((1, 'File %s for %s does not exist.' % ( |
| 239 file_url, output_filename))) | 247 file_url, output_filename))) |
| 240 else: | 248 else: |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 261 | 269 |
| 262 remote_sha1 = get_sha1(output_filename) | 270 remote_sha1 = get_sha1(output_filename) |
| 263 if remote_sha1 != input_sha1_sum: | 271 if remote_sha1 != input_sha1_sum: |
| 264 msg = ('%d> ERROR remote sha1 (%s) does not match expected sha1 (%s).' % | 272 msg = ('%d> ERROR remote sha1 (%s) does not match expected sha1 (%s).' % |
| 265 (thread_num, remote_sha1, input_sha1_sum)) | 273 (thread_num, remote_sha1, input_sha1_sum)) |
| 266 out_q.put(msg) | 274 out_q.put(msg) |
| 267 ret_codes.put((20, msg)) | 275 ret_codes.put((20, msg)) |
| 268 continue | 276 continue |
| 269 | 277 |
| 270 if extract: | 278 if extract: |
| 271 if (not tarfile.is_tarfile(output_filename) | 279 if not tarfile.is_tarfile(output_filename): |
| 272 or not output_filename.endswith('.tar.gz')): | |
| 273 out_q.put('%d> Error: %s is not a tar.gz archive.' % ( | 280 out_q.put('%d> Error: %s is not a tar.gz archive.' % ( |
| 274 thread_num, output_filename)) | 281 thread_num, output_filename)) |
| 275 ret_codes.put((1, '%s is not a tar.gz archive.' % (output_filename))) | 282 ret_codes.put((1, '%s is not a tar.gz archive.' % (output_filename))) |
| 276 continue | 283 continue |
| 277 with tarfile.open(output_filename, 'r:gz') as tar: | 284 with tarfile.open(output_filename, 'r:gz') as tar: |
| 278 dirname = os.path.dirname(os.path.abspath(output_filename)) | 285 dirname = os.path.dirname(os.path.abspath(output_filename)) |
| 279 extract_dir = output_filename[0:len(output_filename)-7] | |
| 280 if not _validate_tar_file(tar, os.path.basename(extract_dir)): | 286 if not _validate_tar_file(tar, os.path.basename(extract_dir)): |
| 281 out_q.put('%d> Error: %s contains files outside %s.' % ( | 287 out_q.put('%d> Error: %s contains files outside %s.' % ( |
| 282 thread_num, output_filename, extract_dir)) | 288 thread_num, output_filename, extract_dir)) |
| 283 ret_codes.put((1, '%s contains invalid entries.' % (output_filename))) | 289 ret_codes.put((1, '%s contains invalid entries.' % (output_filename))) |
| 284 continue | 290 continue |
| 285 if os.path.exists(extract_dir): | 291 if os.path.exists(extract_dir): |
| 286 try: | 292 try: |
| 287 shutil.rmtree(extract_dir) | 293 shutil.rmtree(extract_dir) |
| 288 out_q.put('%d> Removed %s...' % (thread_num, extract_dir)) | 294 out_q.put('%d> Removed %s...' % (thread_num, extract_dir)) |
| 289 except OSError: | 295 except OSError: |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 522 | 528 |
| 523 return download_from_google_storage( | 529 return download_from_google_storage( |
| 524 input_filename, base_url, gsutil, options.num_threads, options.directory, | 530 input_filename, base_url, gsutil, options.num_threads, options.directory, |
| 525 options.recursive, options.force, options.output, options.ignore_errors, | 531 options.recursive, options.force, options.output, options.ignore_errors, |
| 526 options.sha1_file, options.verbose, options.auto_platform, | 532 options.sha1_file, options.verbose, options.auto_platform, |
| 527 options.extract) | 533 options.extract) |
| 528 | 534 |
| 529 | 535 |
| 530 if __name__ == '__main__': | 536 if __name__ == '__main__': |
| 531 sys.exit(main(sys.argv)) | 537 sys.exit(main(sys.argv)) |
| OLD | NEW |