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 json | |
| 6 import os | |
| 7 | |
| 8 import appengine_memcache as memcache | |
| 9 import file_system | |
| 10 from io import BytesIO | |
| 11 from future import Future | |
| 12 from zipfile import ZipFile | |
| 13 | |
| 14 ZIP_KEY = 'zipball' | |
| 15 | |
| 16 class _AsyncFetchFutureZip(object): | |
| 17 def __init__(self, fetcher, blobstore, new_version, old_version): | |
| 18 self._fetch = fetcher.FetchAsync(ZIP_KEY) | |
| 19 self._blobstore = blobstore | |
| 20 self._new_version = new_version | |
| 21 self._old_version = old_version | |
| 22 | |
| 23 def Get(self): | |
| 24 self._blobstore.Delete(ZIP_KEY, self._old_version) | |
|
not at google - send to devlin
2012/08/07 01:35:38
Won't blobstore also need to be namespaced (like A
cduvall
2012/08/08 19:27:25
I think the version functions as a namespace for t
not at google - send to devlin
2012/08/09 07:52:36
sgtm. I don't know so much about the versions thou
| |
| 25 blob = self._fetch.Get().content | |
| 26 self._blobstore.Set(blob, ZIP_KEY, self._new_version) | |
|
not at google - send to devlin
2012/08/07 01:35:38
Why are you versioning the zip files? Just curious
cduvall
2012/08/08 19:27:25
I versioned them so if there's an old zip hanging
not at google - send to devlin
2012/08/09 07:52:36
I made comment in the new version of the code.
| |
| 27 return ZipFile(BytesIO(blob)) | |
| 28 | |
| 29 class GithubFileSystem(file_system.FileSystem): | |
| 30 """FileSystem implementation which fetches resources from github. | |
| 31 """ | |
| 32 def __init__(self, fetcher, memcache, blobstore): | |
| 33 self._fetcher = fetcher | |
| 34 self._memcache = memcache | |
| 35 self._blobstore = blobstore | |
| 36 self._zip_file = None | |
| 37 self._version = 0 | |
| 38 | |
| 39 def _GetZip(self, version): | |
| 40 blob = self._blobstore.Get(ZIP_KEY, version) | |
| 41 if blob is not None: | |
| 42 self._zip_file = Future(value=ZipFile(BytesIO(blob))) | |
| 43 else: | |
| 44 self._zip_file = Future(delegate=_AsyncFetchFutureZip(self._fetcher, | |
| 45 self._blobstore, | |
| 46 version, | |
| 47 self._version)) | |
|
not at google - send to devlin
2012/08/07 01:35:38
Nice, doing this asynchronously is the way to go.
cduvall
2012/08/08 19:27:25
I'm starting the fetch when the GithubFileSystem i
not at google - send to devlin
2012/08/09 07:52:36
Well... I realised there actually isn't much advan
| |
| 48 self._version = version | |
| 49 | |
| 50 def _ReadFile(self, path): | |
| 51 zip_file = self._zip_file.Get() | |
| 52 prefix = zip_file.namelist()[0][:-1] | |
| 53 return zip_file.read(prefix + path) | |
| 54 | |
| 55 def _ListDir(self, path): | |
| 56 filenames = self._zip_file.Get().namelist() | |
| 57 # Take out parent directory name (GoogleChrome-chrome-app-samples-c78a30f) | |
| 58 filenames = [f[len(filenames[0]) - 1:] for f in filenames] | |
| 59 # Remove the path of the directory we're listing from the filenames. | |
| 60 filenames = [f[len(path):] for f in filenames | |
| 61 if f != path and f.startswith(path)] | |
| 62 # Remove all files not directly in this directory. | |
| 63 return [f for f in filenames if f[:-1].count('/') == 0] | |
| 64 | |
| 65 def Read(self, paths, binary=False): | |
| 66 version = self.Stat(ZIP_KEY).version | |
| 67 if True:#self._zip_file is None or version != self._version: | |
|
not at google - send to devlin
2012/08/07 01:35:38
?
cduvall
2012/08/08 19:27:25
oops :)
| |
| 68 self._GetZip(version) | |
| 69 result = {} | |
| 70 for path in paths: | |
| 71 if path.endswith('/'): | |
| 72 result[path] = self._ListDir(path) | |
| 73 else: | |
| 74 result[path] = self._ReadFile(path) | |
| 75 return Future(value=result) | |
| 76 | |
| 77 def Stat(self, path): | |
| 78 version = self._memcache.Get(path, memcache.MEMCACHE_GITHUB_STAT) | |
| 79 if version is not None: | |
| 80 return self.StatInfo(version) | |
| 81 version = json.loads( | |
| 82 self._fetcher.Fetch('commits/HEAD').content)['commit']['tree']['sha'] | |
| 83 self._memcache.Set(path, version, memcache.MEMCACHE_GITHUB_STAT) | |
| 84 return self.StatInfo(version) | |
| OLD | NEW |