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

Side by Side Diff: chrome/common/extensions/docs/server2/patched_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 from copy import deepcopy
6
7 from file_system import FileSystem, StatInfo, FileNotFoundError
8 from future import Future
9
10 class _AsyncFetchFuture(object):
11 def __init__(self,
12 unpatched_files_future,
13 patched_files_future,
14 dirs_value,
15 patched_file_system):
16 self._unpatched_files_future = unpatched_files_future
17 self._patched_files_future = patched_files_future
18 self._dirs_value = dirs_value
19 self._patched_file_system = patched_file_system
20
21 def Get(self):
22 files = self._unpatched_files_future.Get()
23 files.update(self._patched_files_future.Get())
24 files.update({path: self._PatchDirectoryListing(path,
25 self._dirs_value[path])
26 for path in self._dirs_value})
27 return files
28
29 def _PatchDirectoryListing(self, path, original_listing):
30 added, deleted, modified = (
31 self._patched_file_system._GetDirectoryListingFromPatch(path))
32 if original_listing is None:
33 if len(added) == 0:
34 raise FileNotFoundError('Directory %s not found in the patch.' % path)
35 return added
36 return list((set(original_listing) | set(added)) - set(deleted))
37
38 class PatchedFileSystem(FileSystem):
39 ''' Class to fetch resources with a patch applied.
40 '''
41 def __init__(self, host_file_system, patcher):
42 self._host_file_system = host_file_system
43 self._patcher = patcher
44
45 def Read(self, paths, binary=False):
46 patched_files = set()
47 for files in self._patcher.GetPatchedFiles():
48 patched_files |= set(files)
49 dir_paths = {path for path in paths if path.endswith('/')}
50 file_paths = set(paths) - dir_paths
51 patched_paths = file_paths & patched_files
52 unpatched_paths = file_paths - patched_files
53 return Future(delegate=_AsyncFetchFuture(
54 self._host_file_system.Read(unpatched_paths, binary),
55 self._patcher.Apply(patched_paths, self._host_file_system, binary),
56 self._TryReadDirectory(dir_paths, binary),
57 self))
58
59 ''' Given the list of patched files, it's not possible to determine whether
60 a directory to read exists in self._host_file_system. So try reading each one
61 and handle FileNotFoundError.
62 '''
63 def _TryReadDirectory(self, paths, binary):
64 value = {}
65 for path in paths:
66 assert path.endswith('/')
67 try:
68 value[path] = self._host_file_system.ReadSingle(path, binary)
69 except FileNotFoundError:
70 value[path] = None
71 return value
72
73 def _GetDirectoryListingFromPatch(self, path):
74 assert path.endswith('/')
75 def _FindChildrenInPath(files, path):
76 result = []
77 for f in files:
78 if f.startswith(path):
79 child_path = f[len(path):]
80 if '/' in child_path:
81 child_name = child_path[0:child_path.find('/') + 1]
82 else:
83 child_name = child_path
84 result.append(child_name)
85 return result
86
87 added, deleted, modified = (tuple(
88 _FindChildrenInPath(files, path)
89 for files in self._patcher.GetPatchedFiles()))
90
91 # A patch applies to files only. It cannot delete directories.
92 deleted_files = [child for child in deleted if not child.endswith('/')]
93 # However, these directories are actually modified because their children
94 # are patched.
95 modified += [child for child in deleted if child.endswith('/')]
96
97 return (added, deleted_files, modified)
98
99 def _PatchStat(self, stat_info, version, added, deleted, modified):
100 assert len(added) + len(deleted) + len(modified) > 0
101 assert stat_info.child_versions is not None
102
103 # Deep copy before patching to make sure it doesn't interfere with values
104 # cached in memory.
105 stat_info = deepcopy(stat_info)
106
107 stat_info.version = version
108 for child in added + modified:
109 stat_info.child_versions[child] = version
110 for child in deleted:
111 if stat_info.child_versions.get(child):
112 del stat_info.child_versions[child]
113
114 return stat_info
115
116 def Stat(self, path):
117 version = self._patcher.GetVersion()
118 if version is None:
119 return self._host_file_system.Stat(path)
120 version = 'patched_%s' % version
121
122 directory, filename = path.rsplit('/', 1)
123 added, deleted, modified = self._GetDirectoryListingFromPatch(
124 directory + '/')
125
126 if len(added) > 0:
127 # There are new files added. It's possible (if |directory| is new) that
128 # self._host_file_system.Stat will throw an exception.
129 try:
130 stat_info = self._PatchStat(
131 self._host_file_system.Stat(directory + '/'),
132 version,
133 added,
134 deleted,
135 modified)
136 except FileNotFoundError:
137 stat_info = StatInfo(version, {child: version
138 for child in added + modified})
139 elif len(deleted) + len(modified) > 0:
140 # No files were added.
141 stat_info = self._PatchStat(self._host_file_system.Stat(directory + '/'),
142 version,
143 added,
144 deleted,
145 modified)
146 else:
147 # No changes are made in this directory.
148 return self._host_file_system.Stat(path)
149
150 if stat_info.child_versions is not None:
151 if filename:
152 if filename in stat_info.child_versions:
153 stat_info = StatInfo(stat_info.child_versions[filename])
154 else:
155 raise FileNotFoundError('%s was not in child versions' % filename)
156 return stat_info
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698