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