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 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
| 224 extract_dir = None |
| 225 if extract: |
| 226 if not output_filename.endswith('.tar.gz'): |
| 227 out_q.put('%d> Error: %s is not a tar.gz archive.' % ( |
| 228 thread_num, output_filename)) |
| 229 ret_codes.put((1, '%s is not a tar.gz archive.' % (output_filename))) |
| 230 continue |
| 231 extract_dir = output_filename[0:len(output_filename)-7] |
224 if os.path.exists(output_filename) and not force: | 232 if os.path.exists(output_filename) and not force: |
225 if get_sha1(output_filename) == input_sha1_sum: | 233 if not extract or os.path.exists(extract_dir): |
226 if verbose: | 234 if get_sha1(output_filename) == input_sha1_sum: |
227 out_q.put( | 235 if verbose: |
228 '%d> File %s exists and SHA1 matches. Skipping.' % ( | 236 out_q.put( |
229 thread_num, output_filename)) | 237 '%d> File %s exists and SHA1 matches. Skipping.' % ( |
230 continue | 238 thread_num, output_filename)) |
| 239 continue |
231 # Check if file exists. | 240 # Check if file exists. |
232 file_url = '%s/%s' % (base_url, input_sha1_sum) | 241 file_url = '%s/%s' % (base_url, input_sha1_sum) |
233 (code, _, err) = gsutil.check_call('ls', file_url) | 242 (code, _, err) = gsutil.check_call('ls', file_url) |
234 if code != 0: | 243 if code != 0: |
235 if code == 404: | 244 if code == 404: |
236 out_q.put('%d> File %s for %s does not exist, skipping.' % ( | 245 out_q.put('%d> File %s for %s does not exist, skipping.' % ( |
237 thread_num, file_url, output_filename)) | 246 thread_num, file_url, output_filename)) |
238 ret_codes.put((1, 'File %s for %s does not exist.' % ( | 247 ret_codes.put((1, 'File %s for %s does not exist.' % ( |
239 file_url, output_filename))) | 248 file_url, output_filename))) |
240 else: | 249 else: |
(...skipping 20 matching lines...) Expand all Loading... |
261 | 270 |
262 remote_sha1 = get_sha1(output_filename) | 271 remote_sha1 = get_sha1(output_filename) |
263 if remote_sha1 != input_sha1_sum: | 272 if remote_sha1 != input_sha1_sum: |
264 msg = ('%d> ERROR remote sha1 (%s) does not match expected sha1 (%s).' % | 273 msg = ('%d> ERROR remote sha1 (%s) does not match expected sha1 (%s).' % |
265 (thread_num, remote_sha1, input_sha1_sum)) | 274 (thread_num, remote_sha1, input_sha1_sum)) |
266 out_q.put(msg) | 275 out_q.put(msg) |
267 ret_codes.put((20, msg)) | 276 ret_codes.put((20, msg)) |
268 continue | 277 continue |
269 | 278 |
270 if extract: | 279 if extract: |
271 if (not tarfile.is_tarfile(output_filename) | 280 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.' % ( | 281 out_q.put('%d> Error: %s is not a tar.gz archive.' % ( |
274 thread_num, output_filename)) | 282 thread_num, output_filename)) |
275 ret_codes.put((1, '%s is not a tar.gz archive.' % (output_filename))) | 283 ret_codes.put((1, '%s is not a tar.gz archive.' % (output_filename))) |
276 continue | 284 continue |
277 with tarfile.open(output_filename, 'r:gz') as tar: | 285 with tarfile.open(output_filename, 'r:gz') as tar: |
278 dirname = os.path.dirname(os.path.abspath(output_filename)) | 286 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)): | 287 if not _validate_tar_file(tar, os.path.basename(extract_dir)): |
281 out_q.put('%d> Error: %s contains files outside %s.' % ( | 288 out_q.put('%d> Error: %s contains files outside %s.' % ( |
282 thread_num, output_filename, extract_dir)) | 289 thread_num, output_filename, extract_dir)) |
283 ret_codes.put((1, '%s contains invalid entries.' % (output_filename))) | 290 ret_codes.put((1, '%s contains invalid entries.' % (output_filename))) |
284 continue | 291 continue |
285 if os.path.exists(extract_dir): | 292 if os.path.exists(extract_dir): |
286 try: | 293 try: |
287 shutil.rmtree(extract_dir) | 294 shutil.rmtree(extract_dir) |
288 out_q.put('%d> Removed %s...' % (thread_num, extract_dir)) | 295 out_q.put('%d> Removed %s...' % (thread_num, extract_dir)) |
289 except OSError: | 296 except OSError: |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 | 529 |
523 return download_from_google_storage( | 530 return download_from_google_storage( |
524 input_filename, base_url, gsutil, options.num_threads, options.directory, | 531 input_filename, base_url, gsutil, options.num_threads, options.directory, |
525 options.recursive, options.force, options.output, options.ignore_errors, | 532 options.recursive, options.force, options.output, options.ignore_errors, |
526 options.sha1_file, options.verbose, options.auto_platform, | 533 options.sha1_file, options.verbose, options.auto_platform, |
527 options.extract) | 534 options.extract) |
528 | 535 |
529 | 536 |
530 if __name__ == '__main__': | 537 if __name__ == '__main__': |
531 sys.exit(main(sys.argv)) | 538 sys.exit(main(sys.argv)) |
OLD | NEW |