| 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 import os | 5 import os |
| 6 from io import BytesIO | 6 from io import BytesIO |
| 7 import re | 7 import re |
| 8 from zipfile import ZipFile | 8 from zipfile import ZipFile |
| 9 | 9 |
| 10 import compiled_file_system as compiled_fs | 10 import compiled_file_system as compiled_fs |
| 11 | 11 |
| 12 # Increment this if the data model changes for ExampleZipper. | |
| 13 _VERSION = 1 | |
| 14 | |
| 15 class ExampleZipper(object): | 12 class ExampleZipper(object): |
| 16 """This class creates a zip file given a samples directory. | 13 """This class creates a zip file given a samples directory. |
| 17 """ | 14 """ |
| 18 def __init__(self, compiled_fs_factory, base_path): | 15 def __init__(self, compiled_fs_factory, base_path): |
| 19 self._base_path = base_path.rstrip('/') | 16 self._base_path = base_path.rstrip('/') |
| 20 # Use an IdentityFileSystem here so that it shares a cache with the samples | 17 # Use an IdentityFileSystem here so that it shares a cache with the samples |
| 21 # data source. Otherwise we'd need to fetch the zip files from the cron job. | 18 # data source. Otherwise we'd need to fetch the zip files from the cron job. |
| 22 self._file_cache = compiled_fs_factory.GetOrCreateIdentity() | 19 self._file_cache = compiled_fs_factory.GetOrCreateIdentity() |
| 23 self._zip_cache = compiled_fs_factory.Create(self._MakeZipFile, | 20 self._zip_cache = compiled_fs_factory.Create(self._MakeZipFile, |
| 24 ExampleZipper, | 21 ExampleZipper) |
| 25 version=_VERSION) | |
| 26 | 22 |
| 27 def _MakeZipFile(self, base_dir, files): | 23 def _MakeZipFile(self, base_dir, files): |
| 28 if 'manifest.json' not in files: | 24 if 'manifest.json' not in files: |
| 29 return None | 25 return None |
| 30 zip_bytes = BytesIO() | 26 zip_bytes = BytesIO() |
| 31 zip_file = ZipFile(zip_bytes, mode='w') | 27 zip_file = ZipFile(zip_bytes, mode='w') |
| 32 try: | 28 try: |
| 33 for file_name in files: | 29 for file_name in files: |
| 34 file_path = '%s%s' % (base_dir, file_name) | 30 file_path = '%s%s' % (base_dir, file_name) |
| 35 file_contents = self._file_cache.GetFromFile(file_path, binary=True) | 31 file_contents = self._file_cache.GetFromFile(file_path, binary=True) |
| 36 if isinstance(file_contents, unicode): | 32 if isinstance(file_contents, unicode): |
| 37 # Data is sometimes already cached as unicode. | 33 # Data is sometimes already cached as unicode. |
| 38 file_contents = file_contents.encode('utf8') | 34 file_contents = file_contents.encode('utf8') |
| 39 # We want e.g. basic.zip to expand to basic/manifest.json etc, not | 35 # We want e.g. basic.zip to expand to basic/manifest.json etc, not |
| 40 # chrome/common/extensions/.../basic/manifest.json, so only use the | 36 # chrome/common/extensions/.../basic/manifest.json, so only use the |
| 41 # end of the path component when writing into the zip file. | 37 # end of the path component when writing into the zip file. |
| 42 redundant_prefix = '%s/' % base_dir.rstrip('/').rsplit('/', 1)[0] | 38 redundant_prefix = '%s/' % base_dir.rstrip('/').rsplit('/', 1)[0] |
| 43 zip_file.writestr(file_path[len(redundant_prefix):], file_contents) | 39 zip_file.writestr(file_path[len(redundant_prefix):], file_contents) |
| 44 finally: | 40 finally: |
| 45 zip_file.close() | 41 zip_file.close() |
| 46 return zip_bytes.getvalue() | 42 return zip_bytes.getvalue() |
| 47 | 43 |
| 48 def Create(self, path): | 44 def Create(self, path): |
| 49 """ Creates a new zip file from the recursive contents of |path| | 45 """ Creates a new zip file from the recursive contents of |path| |
| 50 as returned by |_zip_cache|. | 46 as returned by |_zip_cache|. |
| 51 Paths within the zip file are given relative to and including |path|. | 47 Paths within the zip file are given relative to and including |path|. |
| 52 """ | 48 """ |
| 53 return self._zip_cache.GetFromFileListing( | 49 return self._zip_cache.GetFromFileListing( |
| 54 '%s/%s' % (self._base_path, path.strip('/'))) | 50 '%s/%s' % (self._base_path, path.strip('/'))) |
| OLD | NEW |