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 file_system_cache as fs_cache |
| 11 |
10 class ExampleZipper(object): | 12 class ExampleZipper(object): |
11 """This class creates a zip file given a samples directory. | 13 """This class creates a zip file given a samples directory. |
12 """ | 14 """ |
13 def __init__(self, file_system, cache_builder, base_path, match_path): | 15 def __init__(self, file_system, cache_builder, base_path, match_path): |
14 self._base_path = base_path | 16 self._base_path = base_path |
15 self._zip_cache = cache_builder.build(self._MakeZipFile) | 17 self._zip_cache = cache_builder.build(self._MakeZipFile, |
| 18 fs_cache.ZIP) |
16 self._file_system = file_system | 19 self._file_system = file_system |
17 self._match_path = match_path | 20 self._match_path = match_path |
18 | 21 |
19 def _MakeZipFile(self, files): | 22 def _MakeZipFile(self, files): |
20 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2] | 23 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2] |
21 prefix = zip_path.rsplit('/', 1)[-2] | 24 prefix = zip_path.rsplit('/', 1)[-2] |
22 if zip_path + '/manifest.json' not in files: | 25 if zip_path + '/manifest.json' not in files: |
23 return None | 26 return None |
24 zip_bytes = BytesIO() | 27 zip_bytes = BytesIO() |
25 zip_file = ZipFile(zip_bytes, mode='w') | 28 zip_file = ZipFile(zip_bytes, mode='w') |
26 try: | 29 try: |
27 for name, file_contents in ( | 30 for name, file_contents in ( |
28 self._file_system.Read(files, binary=True).Get().iteritems()): | 31 self._file_system.Read(files, binary=True).Get().iteritems()): |
29 zip_file.writestr(name[len(prefix):].strip('/'), file_contents) | 32 zip_file.writestr(name[len(prefix):].strip('/'), file_contents) |
30 finally: | 33 finally: |
31 zip_file.close() | 34 zip_file.close() |
32 return zip_bytes.getvalue() | 35 return zip_bytes.getvalue() |
33 | 36 |
34 def Create(self, path): | 37 def Create(self, path): |
35 """ Creates a new zip file from the recursive contents of |path| | 38 """ Creates a new zip file from the recursive contents of |path| |
36 as returned by |_zip_cache|. | 39 as returned by |_zip_cache|. |
37 Paths within the zip file are given relative to and including |path|. | 40 Paths within the zip file are given relative to and including |path|. |
38 """ | 41 """ |
39 return self._zip_cache.GetFromFileListing( | 42 return self._zip_cache.GetFromFileListing( |
40 self._base_path + '/' + path) | 43 self._base_path + '/' + path) |
OLD | NEW |