| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 from io import BytesIO | 5 from io import BytesIO |
| 6 from zipfile import ZipFile | 6 from zipfile import ZipFile |
| 7 | 7 |
| 8 class ExampleZipper(object): | 8 class ExampleZipper(object): |
| 9 '''This class creates a zip file given a samples directory. | 9 '''This class creates a zip file given a samples directory. |
| 10 ''' | 10 ''' |
| 11 def __init__(self, compiled_fs_factory, file_system, base_path): | 11 def __init__(self, compiled_fs_factory, file_system, base_path): |
| 12 self._base_path = base_path.rstrip('/') | 12 self._base_path = base_path.rstrip('/') |
| 13 self._file_system = file_system | 13 self._file_system = file_system |
| 14 self._zip_cache = compiled_fs_factory.Create(self._MakeZipFile, | 14 self._zip_cache = compiled_fs_factory.Create(file_system, |
| 15 self._MakeZipFile, |
| 15 ExampleZipper) | 16 ExampleZipper) |
| 16 | 17 |
| 17 def _MakeZipFile(self, base_dir, files): | 18 def _MakeZipFile(self, base_dir, files): |
| 18 if 'manifest.json' not in files: | 19 if 'manifest.json' not in files: |
| 19 return None | 20 return None |
| 20 zip_bytes = BytesIO() | 21 zip_bytes = BytesIO() |
| 21 zip_file = ZipFile(zip_bytes, mode='w') | 22 zip_file = ZipFile(zip_bytes, mode='w') |
| 22 try: | 23 try: |
| 23 for file_name in files: | 24 for file_name in files: |
| 24 file_path = '%s%s' % (base_dir, file_name) | 25 file_path = '%s%s' % (base_dir, file_name) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 36 zip_file.close() | 37 zip_file.close() |
| 37 return zip_bytes.getvalue() | 38 return zip_bytes.getvalue() |
| 38 | 39 |
| 39 def Create(self, path): | 40 def Create(self, path): |
| 40 ''' Creates a new zip file from the recursive contents of |path| | 41 ''' Creates a new zip file from the recursive contents of |path| |
| 41 as returned by |_zip_cache|. | 42 as returned by |_zip_cache|. |
| 42 Paths within the zip file are given relative to and including |path|. | 43 Paths within the zip file are given relative to and including |path|. |
| 43 ''' | 44 ''' |
| 44 return self._zip_cache.GetFromFileListing( | 45 return self._zip_cache.GetFromFileListing( |
| 45 '%s/%s' % (self._base_path, path.strip('/'))).Get() | 46 '%s/%s' % (self._base_path, path.strip('/'))).Get() |
| OLD | NEW |