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