Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 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 | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Download files from Google Storage based on SHA1 sums.""" | |
| 7 | |
| 8 | |
| 9 import optparse | |
| 10 import os | |
| 11 import Queue | |
| 12 import re | |
| 13 import sys | |
| 14 import threading | |
| 15 import time | |
| 16 | |
| 17 import gstools | |
| 18 | |
| 19 | |
| 20 GSUTIL_DEFAULT_PATH = os.path.join( | |
| 21 os.path.dirname(os.path.abspath(__file__)), | |
| 22 'third_party', 'gsutil', 'gsutil') | |
| 23 | |
| 24 | |
| 25 def enumerate_work_queue(input_filename, work_queue, options): | |
| 26 work_queue_size = 0 | |
| 27 if options.directory: | |
| 28 for root, dirs, files in os.walk(input_filename): | |
| 29 if not options.recursive: | |
| 30 for item in dirs[:]: | |
| 31 dirs.remove(item) | |
| 32 else: | |
| 33 for exclude in ['.svn', '.git']: | |
| 34 if exclude in dirs: | |
| 35 dirs.remove(exclude) | |
| 36 for filename in files: | |
| 37 full_path = os.path.join(root, filename) | |
| 38 if full_path.endswith('.sha1'): | |
| 39 with open(full_path, 'rb') as f: | |
| 40 sha1_match = re.search('^([A-Za-z0-9]{40})$', f.read(1024)) | |
|
M-A Ruel
2013/02/27 21:52:07
s/search/match/ ?
You may want to .rstrip() what y
Ryan Tseng
2013/02/27 23:34:41
Done.
| |
| 41 if sha1_match: | |
| 42 work_queue.put( | |
| 43 (sha1_match.groups(1)[0], full_path.replace('.sha1', ''))) | |
| 44 work_queue_size += 1 | |
| 45 else: | |
| 46 print >> sys.stderr, 'No sha1 sum found in %s.' % filename | |
| 47 else: | |
| 48 work_queue.put((input_filename, options.output)) | |
| 49 work_queue_size += 1 | |
| 50 return work_queue_size | |
| 51 | |
| 52 | |
| 53 def _downloader_worker_thread(thread_num, q, options, base_url, gsutil, out_q): | |
| 54 while True: | |
| 55 input_sha1_sum, output_filename = q.get() | |
| 56 if input_sha1_sum is None: | |
| 57 out_q.put('Thread %d is done' % thread_num) | |
| 58 return | |
| 59 if os.path.exists(output_filename) and not options.force: | |
| 60 if gstools.GetSHA1(output_filename) == input_sha1_sum: | |
| 61 out_q.put('File %s exists and SHA1 sum (%s) matches. Skipping.' % ( | |
| 62 output_filename , input_sha1_sum)) | |
| 63 continue | |
| 64 # Check if file exists. | |
| 65 file_url = '%s/%s' % (base_url, input_sha1_sum) | |
| 66 if gsutil.check_call('ls', file_url)[0] != 0: | |
| 67 out_q.put('File %s for %s does not exist, skipping.' % ( | |
| 68 file_url, output_filename)) | |
| 69 continue | |
| 70 # Fetch the file. | |
| 71 out_q.put('Downloading %s to %s...' % (file_url, output_filename)) | |
| 72 code = gsutil.call('cp', '-q', file_url, output_filename) | |
| 73 if code != 0: | |
| 74 out_q.put(gsutil.stderr) | |
| 75 return code | |
| 76 | |
| 77 | |
| 78 def download_from_google_storage(input_filename, base_url, gsutil, options): | |
| 79 # Start up all the worker threads. | |
| 80 all_threads = [] | |
| 81 download_timer = time.time() | |
| 82 stdout_queue = Queue.Queue() | |
| 83 work_queue = Queue.Queue() | |
| 84 for thread_num in range(options.num_threads): | |
| 85 t = threading.Thread( | |
| 86 target=_downloader_worker_thread, | |
| 87 args=[thread_num, work_queue, options, base_url, | |
| 88 gsutil.clone(), stdout_queue]) | |
| 89 t.daemon = True | |
| 90 t.start() | |
| 91 all_threads.append(t) | |
| 92 work_queue.put((None, None)) # Used to tell worker threads to stop. | |
|
M-A Ruel
2013/02/27 21:52:07
You have to do this after enqueuing the items. I d
Ryan Tseng
2013/02/27 23:34:41
Doh, moved down. Added another test.
| |
| 93 | |
| 94 # Enumerate our work queue. | |
| 95 work_queue_size = enumerate_work_queue(input_filename, work_queue, options) | |
| 96 | |
| 97 # Wait for all downloads to finish. | |
| 98 while True: | |
| 99 num_alive_threads = 0 | |
| 100 for t in all_threads: | |
|
M-A Ruel
2013/02/27 21:52:07
Use t.join() Do not busy loop.
Ryan Tseng
2013/02/27 23:34:41
It shouldn't' busy loop. It should block on the g
| |
| 101 if t.is_alive(): | |
| 102 num_alive_threads += 1 | |
| 103 if num_alive_threads == 0 and stdout_queue.empty(): | |
| 104 break | |
| 105 line = stdout_queue.get() | |
| 106 print line | |
| 107 | |
| 108 | |
| 109 print 'Success.' | |
| 110 print 'Downloading %d files took %1f second(s)' % ( | |
| 111 work_queue_size, time.time() - download_timer) | |
| 112 return 0 | |
| 113 | |
| 114 | |
| 115 def main(args): | |
| 116 usage = ('usage: %prog [options] target\nTarget must be:\n' | |
| 117 '(default) a sha1 sum ([A-Za-z0-9]{40}).\n(-s or --sha1_file) a ' | |
| 118 '.sha1 file, containing a sha1 sum on the first line. (-d or ' | |
| 119 '--directory) A directory to scan for .sha1 files. ') | |
| 120 parser = optparse.OptionParser(usage) | |
| 121 parser.add_option('-o', '--output', | |
| 122 help='Specify the output file name. Defaults to:\n' | |
| 123 '(a) Given a SHA1 hash, the name is the SHA1 hash.\n' | |
| 124 '(b) Given a .sha1 file or directory, the name will ' | |
| 125 'match (.*).sha1.') | |
| 126 parser.add_option('-b', '--bucket', | |
| 127 help='Google Storage bucket to fetch from.') | |
| 128 parser.add_option('-e', '--boto', | |
| 129 help='Specify a custom boto file.') | |
| 130 parser.add_option('-c', '--no_resume', action='store_true', | |
| 131 help='Resume download if file is partially downloaded.') | |
| 132 parser.add_option('-f', '--force', action='store_true', | |
| 133 help='Force download even if local file exists.') | |
| 134 parser.add_option('-r', '--recursive', action='store_true', | |
| 135 help='Scan folders recursively for .sha1 files. ' | |
| 136 'Must be used with -d/--directory') | |
| 137 parser.add_option('-t', '--num_threads', default=1, type='int', | |
| 138 help='Number of downloader threads to run.') | |
| 139 parser.add_option('-d', '--directory', action='store_true', | |
| 140 help='The target is a directory. ' | |
| 141 'Cannot be used with -s/--sha1_file.') | |
| 142 parser.add_option('-s', '--sha1_file', action='store_true', | |
| 143 help='The target is a file containing a sha1 sum. ' | |
| 144 'Cannot be used with -d/--directory.') | |
| 145 | |
| 146 (options, args) = parser.parse_args() | |
| 147 if not args: | |
| 148 parser.error('Missing target.') | |
| 149 if len(args) > 1: | |
| 150 parser.error('Too many targets.') | |
| 151 if not options.bucket: | |
| 152 parser.error('Missing bucket. Specify bucket with --bucket.') | |
| 153 if options.sha1_file and options.directory: | |
| 154 parser.error('Both --directory and --sha1_file are specified, ' | |
| 155 'can only specify one.') | |
| 156 elif options.recursive and not options.directory: | |
| 157 parser.error('--recursive specified but --directory not specified.') | |
| 158 elif options.output and options.directory: | |
| 159 parser.error('--directory is specified, so --output has no effect.') | |
| 160 else: | |
| 161 input_filename = args[0] | |
| 162 | |
| 163 # Set output filename if not specified. | |
| 164 if not options.output and not options.directory: | |
| 165 if not options.sha1_file: | |
| 166 # Target is a sha1 sum, so output filename would also be the sha1 sum. | |
| 167 options.output = input_filename | |
| 168 elif options.sha1_file: | |
| 169 # Target is a .sha1 file. | |
| 170 if not input_filename.endswith('.sha1'): | |
| 171 parser.error('--sha1_file is specified, but the input filename ' | |
| 172 'does not end with .sha1, and no --output is specified. ' | |
| 173 'Either make sure the input filename has a .sha1 ' | |
| 174 'extension, or specify --output.') | |
| 175 options.output = input_filename[:-5] | |
| 176 else: | |
| 177 raise NotImplementedError('Unreachable state.') | |
| 178 | |
| 179 # Check if output file already exists. | |
| 180 if not options.directory and not options.force and not options.no_resume: | |
| 181 if os.path.exists(options.output): | |
| 182 parser.error('Output file %s exists and --no_resume is specified.' | |
| 183 % options.output) | |
| 184 | |
| 185 # Make sure we can find a working instance of gsutil. | |
| 186 if os.path.exists(GSUTIL_DEFAULT_PATH): | |
| 187 gsutil = gstools.Gsutil(GSUTIL_DEFAULT_PATH) | |
| 188 else: | |
| 189 print >> sys.stderr, ('gsutil not found in %s, bad depot_tools checkout?' % | |
| 190 GSUTIL_DEFAULT_PATH) | |
| 191 return 1 | |
| 192 | |
| 193 # Check we have a valid bucket with valid permissions. | |
| 194 base_url, code = gstools.CheckBucketPermissions(options.bucket, gsutil) | |
| 195 if code: | |
| 196 return code | |
| 197 | |
| 198 return download_from_google_storage(input_filename, base_url, gsutil, options) | |
| 199 | |
| 200 | |
| 201 if __name__ == '__main__': | |
| 202 sys.exit(main(sys.argv)) | |
| OLD | NEW |