Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import logging | |
| 6 import os | |
| 7 from io import BytesIO | |
| 8 from zipfile import ZipFile | |
| 9 | |
| 10 from subversion_data_source import SubversionDataSource | |
| 11 | |
| 12 class ZipDataSource(SubversionDataSource): | |
|
not at google - send to devlin
2012/07/11 00:35:09
Why does this need to expose a data source interfa
cduvall
2012/07/11 20:56:30
Done.
| |
| 13 """This class creates a zip file given a samples directory. | |
| 14 """ | |
| 15 def __init__(self, cache_builder, base_path, ignore_path): | |
| 16 super(ZipDataSource, self).__init__(cache_builder, base_path, ignore_path) | |
| 17 self._zip_cache = cache_builder.build(self._MakeZipFile) | |
| 18 | |
| 19 def _MakeZipFile(self, directory): | |
| 20 file_list = self._ListFilesInDirectory(directory, True) | |
| 21 common_prefix = os.path.commonprefix(file_list) | |
| 22 prefix = common_prefix.rsplit('/', 2)[0] | |
| 23 zip_bytes = BytesIO() | |
| 24 zip_file = ZipFile(zip_bytes, mode='w') | |
| 25 for filename in file_list: | |
| 26 try: | |
| 27 zip_file.writestr( | |
| 28 filename.replace(prefix, ''), | |
| 29 self._file_cache.get(self._base_path + '/' + filename)) | |
| 30 except Exception as e: | |
| 31 logging.info(e) | |
| 32 zip_file.close() | |
| 33 return zip_bytes.getvalue() | |
| 34 | |
| 35 def __getitem__(self, key): | |
| 36 return self.get(key) | |
| 37 | |
| 38 def get(self, key): | |
| 39 base, ext = os.path.splitext(key) | |
| 40 try: | |
| 41 return self._zip_cache.get(self._base_path + '/' + base) | |
| 42 except: | |
| 43 return None | |
| OLD | NEW |