Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: chrome/common/extensions/docs/server2/github_file_system.py

Issue 10825067: Extensions Docs Server: Apps samples page (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Samples on API pages Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
not at google - send to devlin 2012/08/09 07:52:36 This is something you can do in a follow-up, but..
cduvall 2012/08/09 19:20:08 Cool, made a bug: http://crbug.com/141674
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)
25 blob = self._fetch.Get().content
26 self._blobstore.Set(ZIP_KEY, blob, self._new_version)
not at google - send to devlin 2012/08/09 07:52:36 Yeah, so as I said, I don't see the difference bet
cduvall 2012/08/09 19:20:08 Ah, I just remembered why I versioned the zips. Fo
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._version = self.Stat(ZIP_KEY).version
37 self._GetZip(self._version)
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))
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 version != self._version:
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)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698