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