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 difflib | 5 import difflib |
6 import hashlib | 6 import hashlib |
7 import itertools | 7 import itertools |
8 import json | 8 import json |
9 import os | 9 import os |
10 import sys | 10 import sys |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 ret = self._GetEntry(path, subpath) | 333 ret = self._GetEntry(path, subpath) |
334 return ret and ret['tag'] | 334 return ret and ret['tag'] |
335 | 335 |
336 def IterPaths(self): | 336 def IterPaths(self): |
337 """Returns a generator for all top-level paths.""" | 337 """Returns a generator for all top-level paths.""" |
338 return (e['path'] for e in self._files) | 338 return (e['path'] for e in self._files) |
339 | 339 |
340 def IterSubpaths(self, path): | 340 def IterSubpaths(self, path): |
341 """Returns a generator for all subpaths in the given zip. | 341 """Returns a generator for all subpaths in the given zip. |
342 | 342 |
343 If the given path is not a zip file, returns an empty generator. | 343 If the given path is not a zip file or doesn't exist, returns an empty |
| 344 iterable. |
344 """ | 345 """ |
345 outer_entry = self._GetEntry(path) | 346 outer_entry = self._GetEntry(path) |
| 347 if not outer_entry: |
| 348 return () |
346 subentries = outer_entry.get('entries', []) | 349 subentries = outer_entry.get('entries', []) |
347 return (entry['path'] for entry in subentries) | 350 return (entry['path'] for entry in subentries) |
348 | 351 |
349 | 352 |
350 def _UpdateMd5ForFile(md5, path, block_size=2**16): | 353 def _UpdateMd5ForFile(md5, path, block_size=2**16): |
351 with open(path, 'rb') as infile: | 354 with open(path, 'rb') as infile: |
352 while True: | 355 while True: |
353 data = infile.read(block_size) | 356 data = infile.read(block_size) |
354 if not data: | 357 if not data: |
355 break | 358 break |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 | 392 |
390 def _ExtractZipEntries(path): | 393 def _ExtractZipEntries(path): |
391 """Returns a list of (path, CRC32) of all files within |path|.""" | 394 """Returns a list of (path, CRC32) of all files within |path|.""" |
392 entries = [] | 395 entries = [] |
393 with zipfile.ZipFile(path) as zip_file: | 396 with zipfile.ZipFile(path) as zip_file: |
394 for zip_info in zip_file.infolist(): | 397 for zip_info in zip_file.infolist(): |
395 # Skip directories and empty files. | 398 # Skip directories and empty files. |
396 if zip_info.CRC: | 399 if zip_info.CRC: |
397 entries.append((zip_info.filename, zip_info.CRC)) | 400 entries.append((zip_info.filename, zip_info.CRC)) |
398 return entries | 401 return entries |
OLD | NEW |