maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 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 | """Archives a set of files to a server.""" |
| 7 | |
| 8 | import binascii |
| 9 | import hashlib |
| 10 | import logging |
| 11 | import optparse |
| 12 | import os |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 13 | import cStringIO |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 14 | import sys |
| 15 | import time |
| 16 | import urllib2 |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 17 | import zlib |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 18 | |
| 19 | import run_isolated |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 20 | import run_test_cases |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | # The maximum number of upload attempts to try when uploading a single file. |
| 24 | MAX_UPLOAD_ATTEMPTS = 5 |
| 25 | |
| 26 | # The minimum size of files to upload directly to the blobstore. |
maruel@chromium.org | aef29f8 | 2012-12-12 15:00:42 +0000 | [diff] [blame] | 27 | MIN_SIZE_FOR_DIRECT_BLOBSTORE = 20 * 1024 |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 28 | |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 29 | # The number of files to check the isolate server for each query. |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 30 | ITEMS_PER_CONTAINS_QUERY = 500 |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 31 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 32 | # A list of already compressed extension types that should not receive any |
| 33 | # compression before being uploaded. |
| 34 | ALREADY_COMPRESSED_TYPES = [ |
| 35 | '7z', 'avi', 'cur', 'gif', 'h264', 'jar', 'jpeg', 'jpg', 'pdf', 'png', |
| 36 | 'wav', 'zip' |
| 37 | ] |
| 38 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 39 | |
| 40 | def encode_multipart_formdata(fields, files, |
| 41 | mime_mapper=lambda _: 'application/octet-stream'): |
| 42 | """Encodes a Multipart form data object. |
| 43 | |
| 44 | Args: |
| 45 | fields: a sequence (name, value) elements for |
| 46 | regular form fields. |
| 47 | files: a sequence of (name, filename, value) elements for data to be |
| 48 | uploaded as files. |
| 49 | mime_mapper: function to return the mime type from the filename. |
| 50 | Returns: |
| 51 | content_type: for httplib.HTTP instance |
| 52 | body: for httplib.HTTP instance |
| 53 | """ |
| 54 | boundary = hashlib.md5(str(time.time())).hexdigest() |
| 55 | body_list = [] |
| 56 | for (key, value) in fields: |
| 57 | if isinstance(key, unicode): |
| 58 | value = key.encode('utf-8') |
| 59 | if isinstance(value, unicode): |
| 60 | value = value.encode('utf-8') |
| 61 | body_list.append('--' + boundary) |
| 62 | body_list.append('Content-Disposition: form-data; name="%s"' % key) |
| 63 | body_list.append('') |
| 64 | body_list.append(value) |
| 65 | body_list.append('--' + boundary) |
| 66 | body_list.append('') |
| 67 | for (key, filename, value) in files: |
| 68 | if isinstance(key, unicode): |
| 69 | value = key.encode('utf-8') |
| 70 | if isinstance(filename, unicode): |
| 71 | value = filename.encode('utf-8') |
| 72 | if isinstance(value, unicode): |
| 73 | value = value.encode('utf-8') |
| 74 | body_list.append('--' + boundary) |
| 75 | body_list.append('Content-Disposition: form-data; name="%s"; ' |
| 76 | 'filename="%s"' % (key, filename)) |
| 77 | body_list.append('Content-Type: %s' % mime_mapper(filename)) |
| 78 | body_list.append('') |
| 79 | body_list.append(value) |
| 80 | body_list.append('--' + boundary) |
| 81 | body_list.append('') |
| 82 | if body_list: |
| 83 | body_list[-2] += '--' |
| 84 | body = '\r\n'.join(body_list) |
| 85 | content_type = 'multipart/form-data; boundary=%s' % boundary |
| 86 | return content_type, body |
| 87 | |
| 88 | |
| 89 | def gen_url_request(url, payload, content_type='application/octet-stream'): |
| 90 | """Returns a POST request.""" |
| 91 | request = urllib2.Request(url, data=payload) |
| 92 | if payload is not None: |
| 93 | request.add_header('Content-Type', content_type) |
| 94 | request.add_header('Content-Length', len(payload)) |
| 95 | return request |
| 96 | |
| 97 | |
maruel@chromium.org | 037758d | 2012-12-10 17:59:46 +0000 | [diff] [blame] | 98 | def sha1_file(filepath): |
| 99 | """Calculates the SHA-1 of a file without reading it all in memory at once.""" |
| 100 | digest = hashlib.sha1() |
| 101 | with open(filepath, 'rb') as f: |
| 102 | while True: |
| 103 | # Read in 1mb chunks. |
| 104 | chunk = f.read(1024*1024) |
| 105 | if not chunk: |
| 106 | break |
| 107 | digest.update(chunk) |
| 108 | return digest.hexdigest() |
| 109 | |
| 110 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 111 | def url_open(url, data, content_type='application/octet-stream'): |
| 112 | """Opens the given url with the given data, repeating up to |
| 113 | MAX_UPLOAD_ATTEMPTS times if it encounters an error. |
| 114 | |
| 115 | Arguments: |
| 116 | url: The url to open. |
| 117 | data: The data to send to the url. |
| 118 | |
| 119 | Returns: |
| 120 | The response from the url, or it raises an exception it it failed to get |
| 121 | a response. |
| 122 | """ |
| 123 | request = gen_url_request(url, data, content_type) |
maruel@chromium.org | 3dc6abd | 2012-11-15 17:01:53 +0000 | [diff] [blame] | 124 | last_error = None |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 125 | for i in range(MAX_UPLOAD_ATTEMPTS): |
| 126 | try: |
| 127 | return urllib2.urlopen(request) |
| 128 | except urllib2.URLError as e: |
maruel@chromium.org | 3dc6abd | 2012-11-15 17:01:53 +0000 | [diff] [blame] | 129 | last_error = e |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 130 | logging.warning('Unable to connect to %s, error msg: %s', url, e) |
| 131 | time.sleep(0.5 + i) |
| 132 | |
| 133 | # If we get no response from the server after max_retries, assume it |
| 134 | # is down and raise an exception |
| 135 | raise run_isolated.MappingError( |
maruel@chromium.org | 3dc6abd | 2012-11-15 17:01:53 +0000 | [diff] [blame] | 136 | 'Unable to connect to server, %s, to see which files are presents: %s' % |
| 137 | (url, last_error)) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 138 | |
| 139 | |
maruel@chromium.org | 00a7d6c | 2012-11-22 14:11:01 +0000 | [diff] [blame] | 140 | def upload_hash_content_to_blobstore(generate_upload_url, hash_key, content): |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 141 | """Uploads the given hash contents directly to the blobsotre via a generated |
| 142 | url. |
| 143 | |
| 144 | Arguments: |
| 145 | generate_upload_url: The url to get the new upload url from. |
| 146 | hash_contents: The contents to upload. |
| 147 | """ |
| 148 | logging.debug('Generating url to directly upload file to blobstore') |
maruel@chromium.org | 92a3d2e | 2012-12-20 16:22:29 +0000 | [diff] [blame] | 149 | assert isinstance(hash_key, str), hash_key |
| 150 | assert isinstance(content, str), (hash_key, content) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 151 | upload_url = url_open(generate_upload_url, None).read() |
| 152 | |
| 153 | if not upload_url: |
| 154 | logging.error('Unable to generate upload url') |
| 155 | return |
| 156 | |
| 157 | content_type, body = encode_multipart_formdata( |
maruel@chromium.org | 00a7d6c | 2012-11-22 14:11:01 +0000 | [diff] [blame] | 158 | [], [('hash_contents', hash_key, content)]) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 159 | url_open(upload_url, body, content_type) |
| 160 | |
| 161 | |
| 162 | class UploadRemote(run_isolated.Remote): |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 163 | def __init__(self, namespace, *args, **kwargs): |
| 164 | super(UploadRemote, self).__init__(*args, **kwargs) |
maruel@chromium.org | 21243ce | 2012-12-20 17:43:00 +0000 | [diff] [blame] | 165 | self.namespace = str(namespace) |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 166 | |
| 167 | def get_file_handler(self, base_url): |
maruel@chromium.org | 21243ce | 2012-12-20 17:43:00 +0000 | [diff] [blame] | 168 | base_url = str(base_url) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 169 | def upload_file(content, hash_key): |
maruel@chromium.org | 21243ce | 2012-12-20 17:43:00 +0000 | [diff] [blame] | 170 | hash_key = str(hash_key) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 171 | content_url = base_url.rstrip('/') + '/content/' |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 172 | if len(content) > MIN_SIZE_FOR_DIRECT_BLOBSTORE: |
| 173 | upload_hash_content_to_blobstore( |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 174 | content_url + 'generate_blobstore_url/' + self.namespace + '/' + |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 175 | hash_key, |
maruel@chromium.org | 00a7d6c | 2012-11-22 14:11:01 +0000 | [diff] [blame] | 176 | hash_key, |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 177 | content) |
| 178 | else: |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 179 | url_open(content_url + 'store/' + self.namespace + '/' + hash_key, |
| 180 | content) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 181 | return upload_file |
| 182 | |
| 183 | |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 184 | def update_files_to_upload(query_url, queries, upload): |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 185 | """Queries the server to see which files from this batch already exist there. |
| 186 | |
| 187 | Arguments: |
| 188 | queries: The hash files to potential upload to the server. |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 189 | upload: Any new files that need to be upload are sent to this function. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 190 | """ |
| 191 | body = ''.join( |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 192 | (binascii.unhexlify(meta_data['h']) for (_, meta_data) in queries)) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 193 | assert (len(body) % 20) == 0, repr(body) |
| 194 | |
| 195 | response = url_open(query_url, body).read() |
| 196 | if len(queries) != len(response): |
| 197 | raise run_isolated.MappingError( |
| 198 | 'Got an incorrect number of responses from the server. Expected %d, ' |
| 199 | 'but got %d' % (len(queries), len(response))) |
| 200 | |
| 201 | hit = 0 |
| 202 | for i in range(len(response)): |
| 203 | if response[i] == chr(0): |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 204 | upload(queries[i]) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 205 | else: |
| 206 | hit += 1 |
| 207 | logging.info('Queried %d files, %d cache hit', len(queries), hit) |
| 208 | |
| 209 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 210 | def compression_level(filename): |
| 211 | """Given a filename calculates the ideal compression level to use.""" |
| 212 | file_ext = os.path.splitext(filename)[1].lower() |
| 213 | # TODO(csharp): Profile to find what compression level works best. |
| 214 | return 0 if file_ext in ALREADY_COMPRESSED_TYPES else 7 |
| 215 | |
| 216 | |
| 217 | def zip_and_trigger_upload(infile, metadata, upload_function): |
| 218 | compressor = zlib.compressobj(compression_level(infile)) |
| 219 | hash_data = cStringIO.StringIO() |
| 220 | with open(infile, 'rb') as f: |
| 221 | # TODO(csharp): Fix crbug.com/150823 and enable the touched logic again. |
| 222 | while True: # and not metadata['T']: |
| 223 | chunk = f.read(run_isolated.ZIPPED_FILE_CHUNK) |
| 224 | if not chunk: |
| 225 | break |
| 226 | hash_data.write(compressor.compress(chunk)) |
| 227 | |
| 228 | hash_data.write(compressor.flush(zlib.Z_FINISH)) |
| 229 | priority = ( |
| 230 | run_isolated.Remote.HIGH if metadata.get('priority', '1') == '0' |
| 231 | else run_isolated.Remote.MED) |
| 232 | upload_function(priority, hash_data.getvalue(), metadata['h'], |
| 233 | None) |
| 234 | hash_data.close() |
| 235 | |
| 236 | |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 237 | def process_items(contains_hash_url, infiles, zip_and_upload): |
| 238 | """Generates the list of files that need to be uploaded and send them to |
| 239 | zip_and_upload. |
| 240 | |
| 241 | Some may already be on the server. |
| 242 | """ |
| 243 | next_queries = [] |
csharp@chromium.org | 90c4581 | 2013-01-23 14:27:21 +0000 | [diff] [blame^] | 244 | items = ((k, v) for k, v in infiles.iteritems() if 's' in v) |
| 245 | for relfile, metadata in sorted(items, key=lambda x: -x[1]['s']): |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 246 | next_queries.append((relfile, metadata)) |
| 247 | if len(next_queries) == ITEMS_PER_CONTAINS_QUERY: |
| 248 | update_files_to_upload(contains_hash_url, next_queries, zip_and_upload) |
| 249 | next_queries = [] |
| 250 | if next_queries: |
| 251 | update_files_to_upload(contains_hash_url, next_queries, zip_and_upload) |
| 252 | |
| 253 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 254 | def upload_sha1_tree(base_url, indir, infiles, namespace): |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 255 | """Uploads the given tree to the given url. |
| 256 | |
| 257 | Arguments: |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 258 | base_url: The base url, it is assume that |base_url|/has/ can be used to |
| 259 | query if an element was already uploaded, and |base_url|/store/ |
| 260 | can be used to upload a new element. |
| 261 | indir: Root directory the infiles are based in. |
| 262 | infiles: dict of files to map from |indir| to |outdir|. |
| 263 | namespace: The namespace to use on the server. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 264 | """ |
| 265 | logging.info('upload tree(base_url=%s, indir=%s, files=%d)' % |
| 266 | (base_url, indir, len(infiles))) |
| 267 | |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 268 | # Create a pool of workers to zip and upload any files missing from |
| 269 | # the server. |
maruel@chromium.org | 6b0c9ec | 2013-01-18 00:34:31 +0000 | [diff] [blame] | 270 | num_threads = run_test_cases.num_processors() |
| 271 | zipping_pool = run_isolated.ThreadPool(num_threads, num_threads, 0) |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 272 | remote_uploader = UploadRemote(namespace, base_url) |
| 273 | |
| 274 | # Starts the zip and upload process for a given query. The query is assumed |
| 275 | # to be in the format (relfile, metadata). |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 276 | uploaded = [] |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 277 | def zip_and_upload(query): |
| 278 | relfile, metadata = query |
| 279 | infile = os.path.join(indir, relfile) |
maruel@chromium.org | 831958f | 2013-01-22 15:01:46 +0000 | [diff] [blame] | 280 | zipping_pool.add_task(0, zip_and_trigger_upload, infile, metadata, |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 281 | remote_uploader.add_item) |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 282 | uploaded.append(query) |
csharp@chromium.org | 07fa759 | 2013-01-11 18:19:30 +0000 | [diff] [blame] | 283 | |
maruel@chromium.org | 35fc0c8 | 2013-01-17 15:14:14 +0000 | [diff] [blame] | 284 | contains_hash_url = '%s/content/contains/%s' % ( |
| 285 | base_url.rstrip('/'), namespace) |
| 286 | process_items(contains_hash_url, infiles, zip_and_upload) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 287 | |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 288 | logging.info('Waiting for all files to finish zipping') |
| 289 | zipping_pool.join() |
| 290 | logging.info('All files zipped.') |
| 291 | |
| 292 | logging.info('Waiting for all files to finish uploading') |
maruel@chromium.org | 13eca0b | 2013-01-22 16:42:21 +0000 | [diff] [blame] | 293 | # Will raise if any exception occurred. |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 294 | remote_uploader.join() |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 295 | logging.info('All files are uploaded') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 296 | |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 297 | total = len(infiles) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 298 | total_size = sum(metadata.get('s', 0) for metadata in infiles.itervalues()) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 299 | logging.info( |
| 300 | 'Total: %6d, %9.1fkb', |
| 301 | total, |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 302 | sum(m.get('s', 0) for m in infiles.itervalues()) / 1024.) |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 303 | cache_hit = set(infiles.iterkeys()) - set(x[0] for x in uploaded) |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 304 | cache_hit_size = sum(infiles[i].get('s', 0) for i in cache_hit) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 305 | logging.info( |
| 306 | 'cache hit: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size', |
| 307 | len(cache_hit), |
| 308 | cache_hit_size / 1024., |
| 309 | len(cache_hit) * 100. / total, |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 310 | cache_hit_size * 100. / total_size if total_size else 0) |
csharp@chromium.org | 20a888c | 2013-01-15 15:06:55 +0000 | [diff] [blame] | 311 | cache_miss = uploaded |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 312 | cache_miss_size = sum(infiles[i[0]].get('s', 0) for i in cache_miss) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 313 | logging.info( |
| 314 | 'cache miss: %6d, %9.1fkb, %6.2f%% files, %6.2f%% size', |
| 315 | len(cache_miss), |
| 316 | cache_miss_size / 1024., |
| 317 | len(cache_miss) * 100. / total, |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 318 | cache_miss_size * 100. / total_size if total_size else 0) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 319 | |
| 320 | |
| 321 | def main(): |
| 322 | parser = optparse.OptionParser( |
| 323 | usage='%prog [options] <file1..fileN> or - to read from stdin', |
| 324 | description=sys.modules[__name__].__doc__) |
| 325 | # TODO(maruel): Support both NFS and isolateserver. |
| 326 | parser.add_option('-o', '--outdir', help='Remote server to archive to') |
| 327 | parser.add_option( |
| 328 | '-v', '--verbose', |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 329 | action='count', default=0, |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 330 | help='Use multiple times to increase verbosity') |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 331 | parser.add_option('--namespace', default='default-gzip', |
| 332 | help='The namespace to use on the server.') |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 333 | |
| 334 | options, files = parser.parse_args() |
| 335 | |
| 336 | levels = [logging.ERROR, logging.INFO, logging.DEBUG] |
| 337 | logging.basicConfig( |
| 338 | level=levels[min(len(levels)-1, options.verbose)], |
| 339 | format='%(levelname)5s %(module)15s(%(lineno)3d): %(message)s') |
| 340 | if files == ['-']: |
| 341 | files = sys.stdin.readlines() |
| 342 | |
| 343 | if not files: |
| 344 | parser.error('Nothing to upload') |
| 345 | if not options.outdir: |
| 346 | parser.error('Nowhere to send. Please specify --outdir') |
| 347 | |
| 348 | # Load the necessary metadata. This is going to be rewritten eventually to be |
| 349 | # more efficient. |
| 350 | infiles = dict( |
| 351 | ( |
| 352 | f, |
| 353 | { |
maruel@chromium.org | e5c1713 | 2012-11-21 18:18:46 +0000 | [diff] [blame] | 354 | 's': os.stat(f).st_size, |
maruel@chromium.org | 037758d | 2012-12-10 17:59:46 +0000 | [diff] [blame] | 355 | 'h': sha1_file(f), |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 356 | } |
| 357 | ) |
| 358 | for f in files) |
| 359 | |
| 360 | with run_isolated.Profiler('Archive'): |
| 361 | upload_sha1_tree( |
| 362 | base_url=options.outdir, |
| 363 | indir=os.getcwd(), |
csharp@chromium.org | 59c7bcf | 2012-11-21 21:13:18 +0000 | [diff] [blame] | 364 | infiles=infiles, |
| 365 | namespace=options.namespace) |
maruel@chromium.org | c6f9006 | 2012-11-07 18:32:22 +0000 | [diff] [blame] | 366 | return 0 |
| 367 | |
| 368 | |
| 369 | if __name__ == '__main__': |
| 370 | sys.exit(main()) |