OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import ast | 5 import ast |
6 import contextlib | 6 import contextlib |
7 import fnmatch | 7 import fnmatch |
8 import json | 8 import json |
9 import os | 9 import os |
10 import pipes | 10 import pipes |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 321 |
322 def MatchesGlob(path, filters): | 322 def MatchesGlob(path, filters): |
323 """Returns whether the given path matches any of the given glob patterns.""" | 323 """Returns whether the given path matches any of the given glob patterns.""" |
324 return filters and any(fnmatch.fnmatch(path, f) for f in filters) | 324 return filters and any(fnmatch.fnmatch(path, f) for f in filters) |
325 | 325 |
326 | 326 |
327 def MergeZips(output, inputs, exclude_patterns=None, path_transform=None): | 327 def MergeZips(output, inputs, exclude_patterns=None, path_transform=None): |
328 path_transform = path_transform or (lambda p, z: p) | 328 path_transform = path_transform or (lambda p, z: p) |
329 added_names = set() | 329 added_names = set() |
330 | 330 |
331 with zipfile.ZipFile(output, 'w') as out_zip: | 331 output_is_already_open = not isinstance(output, basestring) |
| 332 if output_is_already_open: |
| 333 assert isinstance(output, zipfile.ZipFile) |
| 334 out_zip = output |
| 335 else: |
| 336 out_zip = zipfile.ZipFile(output, 'w') |
| 337 |
| 338 try: |
332 for in_file in inputs: | 339 for in_file in inputs: |
333 with zipfile.ZipFile(in_file, 'r') as in_zip: | 340 with zipfile.ZipFile(in_file, 'r') as in_zip: |
334 in_zip._expected_crc = None | 341 in_zip._expected_crc = None |
335 for info in in_zip.infolist(): | 342 for info in in_zip.infolist(): |
336 # Ignore directories. | 343 # Ignore directories. |
337 if info.filename[-1] == '/': | 344 if info.filename[-1] == '/': |
338 continue | 345 continue |
339 dst_name = path_transform(info.filename, in_file) | 346 dst_name = path_transform(info.filename, in_file) |
340 already_added = dst_name in added_names | 347 already_added = dst_name in added_names |
341 if not already_added and not MatchesGlob(dst_name, exclude_patterns): | 348 if not already_added and not MatchesGlob(dst_name, exclude_patterns): |
342 AddToZipHermetic(out_zip, dst_name, data=in_zip.read(info)) | 349 AddToZipHermetic(out_zip, dst_name, data=in_zip.read(info), |
| 350 compress=info.compress_type != zipfile.ZIP_STORED) |
343 added_names.add(dst_name) | 351 added_names.add(dst_name) |
| 352 finally: |
| 353 if not output_is_already_open: |
| 354 out_zip.close() |
344 | 355 |
345 | 356 |
346 def PrintWarning(message): | 357 def PrintWarning(message): |
347 print 'WARNING: ' + message | 358 print 'WARNING: ' + message |
348 | 359 |
349 | 360 |
350 def PrintBigWarning(message): | 361 def PrintBigWarning(message): |
351 print '***** ' * 8 | 362 print '***** ' * 8 |
352 PrintWarning(message) | 363 PrintWarning(message) |
353 print '***** ' * 8 | 364 print '***** ' * 8 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 | 547 |
537 md5_check.CallAndRecordIfStale( | 548 md5_check.CallAndRecordIfStale( |
538 on_stale_md5, | 549 on_stale_md5, |
539 record_path=record_path, | 550 record_path=record_path, |
540 input_paths=input_paths, | 551 input_paths=input_paths, |
541 input_strings=input_strings, | 552 input_strings=input_strings, |
542 output_paths=output_paths, | 553 output_paths=output_paths, |
543 force=force, | 554 force=force, |
544 pass_changes=True) | 555 pass_changes=True) |
545 | 556 |
OLD | NEW |