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

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

Issue 14125010: Docserver: Add support for viewing docs with a codereview patch applied (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 7 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 # Copyright 2013 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 tarfile
7 from StringIO import StringIO
8
9 from file_system import FileSystem, FileNotFoundError, StatInfo, ToUnicode
10 from future import Future
11
12 # Use a special value other than None to represent a deleted file in the patch.
13 _FILE_NOT_FOUND_VALUE = (None,)
14
15 class _AsyncFetchFuture(object):
16 def __init__(self,
17 base_path,
18 paths,
19 cached_files,
20 missing_paths,
21 binary,
22 patch_data,
23 fetcher,
24 object_store):
25 self._base_path = base_path
26 self._paths = paths
27 self._cached_value = cached_files
28 self._missing_paths = missing_paths
29 self._binary = binary
30 self._patch_data = patch_data
31 self._object_store = object_store
32 if missing_paths is not None:
33 self._tarball = fetcher.FetchAsync('tarball/%s/%s' %
34 (patch_data.issue, patch_data.patchset))
35
36 def _GetMissingPaths(self):
37 tarball_result = self._tarball.Get()
38 if tarball_result.status_code != 200:
39 raise FileNotFoundError('Failed to download tarball.')
40
41 try:
42 tar = tarfile.open(fileobj=StringIO(tarball_result.content))
43 except tarfile.TarError as e:
44 raise FileNotFoundError('Invalid tarball.')
45
46 self._uncached_value = {}
47 for path in self._patch_data.files:
48 if self._cached_value.get(path) is not None:
49 continue
50
51 if self._base_path:
52 tar_path = 'b/%s/%s' % (self._base_path, path)
53 else:
54 tar_path = 'b/%s' % path
55 try:
56 patched_file = tar.extractfile(tar_path)
57 data = patched_file.read()
58 patched_file.close()
not at google - send to devlin 2013/04/29 16:56:21 you may be able to do with tar.extractfile(tar_pa
方觉(Fang Jue) 2013/04/30 14:25:08 It ended up with AttributeError: __exit__.
59 except tarfile.TarError as e:
60 self._uncached_value[path] = _FILE_NOT_FOUND_VALUE
61 continue
62
63 # Deleted files still exist in the tarball, but they are empty.
64 if len(data) == 0:
65 self._uncached_value[path] = _FILE_NOT_FOUND_VALUE
66 elif self._binary:
67 self._uncached_value[path] = data
68 else:
69 self._uncached_value[path] = ToUnicode(data)
70
71 self._object_store.SetMulti(self._uncached_value)
72
73 for path in self._missing_paths:
74 if self._uncached_value.get(path) is None:
75 raise FileNotFoundError('File %s was not found in the patch.' % path)
76 self._cached_value[path] = self._uncached_value[path]
77
78 def Get(self):
79 if self._missing_paths is not None:
80 self._GetMissingPaths()
81
82 # Make sure all paths exist before returning.
83 for path in self._paths:
84 if self._cached_value[path] == _FILE_NOT_FOUND_VALUE:
85 raise FileNotFoundError('File %s was deleted in the patch.' % path)
86 return self._cached_value
87
88 class RietveldFileSystem(FileSystem):
89 ''' Class to fetch resources from a patchset in Rietveld.
90 '''
91 def __init__(self,
92 base_path,
93 patch_data,
94 fetcher,
95 object_store_creator_factory):
96 self._base_path = base_path
97 self._patch_data = patch_data
98 self._fetcher = fetcher
99 self._object_store = object_store_creator_factory.Create(
100 RietveldFileSystem).Create()
101
102 def Read(self, paths, binary=False):
103 cached_files = self._object_store.GetMulti(paths).Get()
104 missing_paths = list(set(paths) - set(cached_files.keys()))
105 if len(missing_paths) == 0:
106 missing_paths = None
107 return Future(delegate=_AsyncFetchFuture(
108 self._base_path,
109 paths,
110 cached_files,
111 missing_paths,
112 binary,
113 self._patch_data,
114 self._fetcher,
115 self._object_store))
not at google - send to devlin 2013/04/29 16:38:49 I'm confused what the role of PatchedFileSystem vs
方觉(Fang Jue) 2013/04/30 01:07:11 Perhaps just combine RietveldFileSystem and Patche
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698