| 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 import re |
| 9 from zipfile import ZipFile |
| 10 |
| 11 class ExampleZipper(object): |
| 12 """This class creates a zip file given a samples directory. |
| 13 """ |
| 14 def __init__(self, cache_builder, base_path, match_path): |
| 15 self._base_path = base_path |
| 16 self._zip_cache = cache_builder.build(self._MakeZipFile) |
| 17 self._svn_cache = cache_builder.build(lambda x: x) |
| 18 self._match_path = match_path |
| 19 |
| 20 def _MakeZipFile(self, files): |
| 21 zip_bytes = BytesIO() |
| 22 zip_file = ZipFile(zip_bytes, mode='w') |
| 23 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2] |
| 24 prefix = zip_path.rsplit('/', 1)[-2] |
| 25 if zip_path + '/manifest.json' not in files: |
| 26 return None |
| 27 for filename in files: |
| 28 try: |
| 29 zip_file.writestr( |
| 30 filename.replace(prefix, ''), |
| 31 self._svn_cache.get(filename)) |
| 32 except Exception as e: |
| 33 logging.info(e) |
| 34 zip_file.close() |
| 35 return zip_bytes.getvalue() |
| 36 |
| 37 def __getitem__(self, key): |
| 38 return self.get(key) |
| 39 |
| 40 def get(self, key): |
| 41 if not re.match(self._match_path + '/.*\.zip$', key): |
| 42 return None |
| 43 base, ext = os.path.splitext(key) |
| 44 try: |
| 45 return self._zip_cache.get(self._base_path + '/' + base, True) |
| 46 except: |
| 47 return None |
| OLD | NEW |