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 json | |
6 import os | |
7 | |
8 import file_system | |
9 from io import BytesIO | |
10 from future import Future | |
11 from zipfile import ZipFile | |
12 | |
13 class ZipFileSystem(file_system.FileSystem): | |
not at google - send to devlin
2012/07/30 11:24:05
This is super cool, though it would be nice to re-
cduvall
2012/08/02 01:14:53
So I switched to fetching individual files and dir
not at google - send to devlin
2012/08/02 13:27:48
Ah. I wonder if that's because we're getting rate
| |
14 """FileSystem implementation which fetches resources from a zip file. | |
15 """ | |
16 def __init__(self, fetcher): | |
17 self._fetcher = fetcher | |
18 self._GetZip() | |
19 | |
20 def _GetZip(self): | |
21 self._version = self.Stat('zipball').version | |
22 self._zip_file = ZipFile(BytesIO(self._fetcher.Fetch('zipball'))) | |
23 | |
24 def _ReadFile(self, path): | |
25 prefix = self._zip_file.namelist()[0][:-1] | |
26 return self._zip_file.read(prefix + path) | |
27 | |
28 def _ListDir(self, path): | |
29 filenames = self._zip_file.namelist() | |
30 # Take out parent directory name (GoogleChrome-chrome-app-samples-c78a30f) | |
31 filenames = [f[len(filenames[0]) - 1:] for f in filenames] | |
32 # Remove the path of the directory we're listing from the filenames. | |
33 filenames = [f[len(path):] for f in filenames | |
34 if f != path and f.startswith(path)] | |
35 # Remove all files not directly in this directory. | |
36 return [f for f in filenames if f[:-1].count('/') == 0] | |
37 | |
38 def Read(self, paths, binary=False): | |
39 if self.Stat('zipball').version != self._version: | |
40 self._GetZip() | |
not at google - send to devlin
2012/07/30 11:24:05
You should try to put these expensive operations (
cduvall
2012/08/02 01:14:53
Done.
| |
41 result = {} | |
42 for path in paths: | |
43 if path.endswith('/'): | |
44 result[path] = self._ListDir(path) | |
45 else: | |
46 result[path] = self._ReadFile(path) | |
47 return Future(value=result) | |
48 | |
49 def Stat(self, path): | |
50 return self.StatInfo(json.loads(self._fetcher.Fetch('commits'))[0]['sha']) | |
OLD | NEW |