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 json | 5 import json |
6 import os | 6 import os |
7 | 7 |
8 import appengine_blobstore as blobstore | 8 import appengine_blobstore as blobstore |
9 import appengine_memcache as memcache | 9 import appengine_memcache as memcache |
10 import file_system | 10 from file_system import FileSystem, StatInfo |
11 from io import BytesIO | 11 from io import BytesIO |
12 from future import Future | 12 from future import Future |
13 from zipfile import ZipFile | 13 from zipfile import ZipFile |
14 | 14 |
15 ZIP_KEY = 'zipball' | 15 ZIP_KEY = 'zipball' |
16 | 16 |
17 def _MakeKey(version): | 17 def _MakeKey(version): |
18 return ZIP_KEY + '.' + str(version) | 18 return ZIP_KEY + '.' + str(version) |
19 | 19 |
20 class _AsyncFetchFutureZip(object): | 20 class _AsyncFetchFutureZip(object): |
21 def __init__(self, fetcher, blobstore, new_version, old_version): | 21 def __init__(self, fetcher, blobstore, new_version, old_version): |
22 self._fetch = fetcher.FetchAsync(ZIP_KEY) | 22 self._fetch = fetcher.FetchAsync(ZIP_KEY) |
23 self._blobstore = blobstore | 23 self._blobstore = blobstore |
24 self._new_version = new_version | 24 self._new_version = new_version |
25 self._old_version = old_version | 25 self._old_version = old_version |
26 | 26 |
27 def Get(self): | 27 def Get(self): |
28 blob = self._fetch.Get().content | 28 blob = self._fetch.Get().content |
29 self._blobstore.Set(_MakeKey(self._new_version), | 29 self._blobstore.Set(_MakeKey(self._new_version), |
30 blob, | 30 blob, |
31 blobstore.BLOBSTORE_GITHUB) | 31 blobstore.BLOBSTORE_GITHUB) |
32 self._blobstore.Delete(_MakeKey(self._old_version), | 32 self._blobstore.Delete(_MakeKey(self._old_version), |
33 blobstore.BLOBSTORE_GITHUB) | 33 blobstore.BLOBSTORE_GITHUB) |
34 return ZipFile(BytesIO(blob)) | 34 return ZipFile(BytesIO(blob)) |
35 | 35 |
36 class GithubFileSystem(file_system.FileSystem): | 36 class GithubFileSystem(FileSystem): |
37 """FileSystem implementation which fetches resources from github. | 37 """FileSystem implementation which fetches resources from github. |
38 """ | 38 """ |
39 def __init__(self, fetcher, memcache, blobstore): | 39 def __init__(self, fetcher, memcache, blobstore): |
40 self._fetcher = fetcher | 40 self._fetcher = fetcher |
41 self._memcache = memcache | 41 self._memcache = memcache |
42 self._blobstore = blobstore | 42 self._blobstore = blobstore |
43 self._version = self.Stat(ZIP_KEY).version | 43 self._version = self.Stat(ZIP_KEY).version |
44 self._GetZip(self._version) | 44 self._GetZip(self._version) |
45 | 45 |
46 def _GetZip(self, version): | 46 def _GetZip(self, version): |
(...skipping 30 matching lines...) Expand all Loading... |
77 for path in paths: | 77 for path in paths: |
78 if path.endswith('/'): | 78 if path.endswith('/'): |
79 result[path] = self._ListDir(path) | 79 result[path] = self._ListDir(path) |
80 else: | 80 else: |
81 result[path] = self._ReadFile(path) | 81 result[path] = self._ReadFile(path) |
82 return Future(value=result) | 82 return Future(value=result) |
83 | 83 |
84 def Stat(self, path): | 84 def Stat(self, path): |
85 version = self._memcache.Get(path, memcache.MEMCACHE_GITHUB_STAT) | 85 version = self._memcache.Get(path, memcache.MEMCACHE_GITHUB_STAT) |
86 if version is not None: | 86 if version is not None: |
87 return self.StatInfo(version) | 87 return StatInfo(version) |
88 version = json.loads( | 88 version = json.loads( |
89 self._fetcher.Fetch('commits/HEAD').content)['commit']['tree']['sha'] | 89 self._fetcher.Fetch('commits/HEAD').content)['commit']['tree']['sha'] |
90 self._memcache.Set(path, version, memcache.MEMCACHE_GITHUB_STAT) | 90 self._memcache.Set(path, version, memcache.MEMCACHE_GITHUB_STAT) |
91 return self.StatInfo(version) | 91 return StatInfo(version) |
OLD | NEW |