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 logging | |
| 6 import os | |
| 7 from io import BytesIO | |
| 8 import re | |
| 9 from zipfile import ZipFile | |
| 10 | |
| 11 class ExampleZipper(object): | |
| 12 """This class creates a zip file given a samples directory. | |
| 13 """ | |
| 14 def __init__(self, cache_builder, base_path, match_path): | |
| 15 self._base_path = base_path | |
| 16 self._zip_cache = cache_builder.build(self._MakeZipFile) | |
| 17 self._svn_cache = cache_builder.build(lambda x: x) | |
|
not at google - send to devlin
2012/07/12 00:22:48
Is it actually worth having a cache for the svn st
cduvall
2012/07/12 01:51:23
Done.
| |
| 18 self._match_path = match_path | |
| 19 | |
| 20 def _MakeZipFile(self, files): | |
| 21 zip_bytes = BytesIO() | |
| 22 zip_file = ZipFile(zip_bytes, mode='w') | |
| 23 zip_path = os.path.commonprefix(files).rsplit('/', 1)[-2] | |
| 24 prefix = zip_path.rsplit('/', 1)[-2] | |
| 25 if zip_path + '/manifest.json' not in files: | |
| 26 return None | |
|
not at google - send to devlin
2012/07/12 00:22:48
This leaks zip_file, but see comment below.
cduvall
2012/07/12 01:51:23
Done.
| |
| 27 for filename in files: | |
| 28 try: | |
| 29 zip_file.writestr( | |
| 30 filename.replace(prefix, ''), | |
|
not at google - send to devlin
2012/07/12 00:22:48
filename[len(prefix):] ?
This actually leaves a l
cduvall
2012/07/12 01:51:23
Done.
| |
| 31 self._svn_cache.get(filename)) | |
| 32 except Exception as e: | |
| 33 logging.info(e) | |
|
not at google - send to devlin
2012/07/12 00:22:48
Should we be serving partial zip files on exceptio
cduvall
2012/07/12 01:51:23
Done.
| |
| 34 zip_file.close() | |
| 35 return zip_bytes.getvalue() | |
| 36 | |
| 37 def __getitem__(self, key): | |
|
not at google - send to devlin
2012/07/12 00:22:48
Remove this method?
cduvall
2012/07/12 01:51:23
Done.
| |
| 38 return self.get(key) | |
| 39 | |
| 40 def get(self, key): | |
|
not at google - send to devlin
2012/07/12 00:22:48
A more sensible interface for this class, I think,
cduvall
2012/07/12 01:51:23
Done.
| |
| 41 if not re.match(self._match_path + '/.*\.zip$', key): | |
|
not at google - send to devlin
2012/07/12 00:22:48
See comment just above; none of this checking shou
cduvall
2012/07/12 01:51:23
Done.
| |
| 42 return None | |
| 43 base, ext = os.path.splitext(key) | |
| 44 try: | |
| 45 return self._zip_cache.get(self._base_path + '/' + base, True) | |
|
not at google - send to devlin
2012/07/12 00:22:48
This really threw me. Can we put two methods on th
cduvall
2012/07/12 01:51:23
Done.
| |
| 46 except: | |
| 47 return None | |
|
not at google - send to devlin
2012/07/12 00:22:48
We should let the top-level handler deal with erro
cduvall
2012/07/12 01:51:23
Done.
| |
| OLD | NEW |