Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import os | |
| 6 from io import BytesIO | |
| 7 import re | |
| 8 from zipfile import ZipFile | |
| 9 | |
| 10 class ExampleZipper(object): | |
| 11 """This class creates a zip file given a samples directory. | |
| 12 """ | |
| 13 def __init__(self, fetcher, cache_builder, base_path, match_path): | |
| 14 self._base_path = base_path | |
| 15 self._zip_cache = cache_builder.build(self._MakeZipFile) | |
| 16 self._fetcher = fetcher | |
| 17 self._match_path = match_path | |
| 18 | |
| 19 def _MakeZipFile(self, files): | |
| 20 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2] | |
| 21 prefix = zip_path.rsplit('/', 1)[-2] | |
| 22 if zip_path + '/manifest.json' not in files: | |
| 23 return None | |
| 24 zip_bytes = BytesIO() | |
| 25 zip_file = ZipFile(zip_bytes, mode='w') | |
| 26 try: | |
| 27 for filename in files: | |
| 28 zip_file.writestr( | |
| 29 filename[len(prefix):].strip('/'), | |
| 30 self._fetcher.FetchResource(filename).content) | |
| 31 finally: | |
| 32 zip_file.close() | |
| 33 return zip_bytes.getvalue() | |
| 34 | |
| 35 def create(self, path): | |
| 36 """ Creates a new zip file from the recursive contents of |path| | |
| 37 as returned by |_zip_cache|. | |
| 38 Paths within the zip file are given relative to and including |path|. | |
| 39 """ | |
| 40 return self._zip_cache.getFromFileListing(self._base_path + '/' + path, | |
| 41 True) | |
|
not at google - send to devlin
2012/07/12 07:06:29
Be explicit here with recursive=True, makes this c
cduvall
2012/07/12 18:09:02
Done.
| |
| OLD | NEW |