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