Docserver: Remove instances of _GetAsyncFetchCallback()

NOTRY=True

Review URL: https://codereview.chromium.org/448823002

Cr-Commit-Position: refs/heads/master@{#288287}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288287 0039d316-1c4b-4281-b951-d872f2087c98
parent 8c016b61
...@@ -8,30 +8,6 @@ from file_system import FileSystem, StatInfo, FileNotFoundError ...@@ -8,30 +8,6 @@ from file_system import FileSystem, StatInfo, FileNotFoundError
from future import Future 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 PatchedFileSystem(FileSystem):
''' Class to fetch resources with a patch applied. ''' Class to fetch resources with a patch applied.
''' '''
...@@ -46,17 +22,31 @@ class PatchedFileSystem(FileSystem): ...@@ -46,17 +22,31 @@ class PatchedFileSystem(FileSystem):
def raise_file_not_found(): def raise_file_not_found():
raise FileNotFoundError('Files are removed from the patch.') raise FileNotFoundError('Files are removed from the patch.')
return Future(callback=raise_file_not_found) return Future(callback=raise_file_not_found)
patched_files |= (set(added) | set(modified)) patched_files |= (set(added) | set(modified))
dir_paths = set(path for path in paths if path.endswith('/')) dir_paths = set(path for path in paths if path.endswith('/'))
file_paths = set(paths) - dir_paths file_paths = set(paths) - dir_paths
patched_paths = file_paths & patched_files patched_paths = file_paths & patched_files
unpatched_paths = file_paths - patched_files unpatched_paths = file_paths - patched_files
return Future(callback=_GetAsyncFetchCallback(
self._base_file_system.Read(unpatched_paths, def patch_directory_listing(path, original_listing):
skip_not_found=skip_not_found), added, deleted, modified = (
self._patcher.Apply(patched_paths, self._base_file_system), self._GetDirectoryListingFromPatch(path))
self._TryReadDirectory(dir_paths), if original_listing is None:
self)) 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 next(files):
dirs_value = self._TryReadDirectory(dir_paths)
files.update(self._patcher.Apply(patched_paths,
self._base_file_system).Get())
files.update(dict((path, patch_directory_listing(path, dirs_value[path]))
for path in dirs_value))
return files
return self._base_file_system.Read(unpatched_paths,
skip_not_found=skip_not_found).Then(next)
def Refresh(self): def Refresh(self):
return self._base_file_system.Refresh() return self._base_file_system.Refresh()
......
...@@ -25,51 +25,6 @@ class RietveldPatcherError(Exception): ...@@ -25,51 +25,6 @@ class RietveldPatcherError(Exception):
self.message = message self.message = message
def _GetAsyncFetchCallback(issue, patchset, files, fetcher):
tarball = fetcher.FetchAsync('tarball/%s/%s' % (issue, patchset))
def resolve():
tarball_result = tarball.Get()
if tarball_result.status_code != 200:
raise RietveldPatcherError(
'Failed to download tarball for issue %s patchset %s. Status: %s' %
(issue, patchset, tarball_result.status_code))
try:
tar = tarfile.open(fileobj=StringIO(tarball_result.content))
except tarfile.TarError as e:
raise RietveldPatcherError(
'Error loading tarball for issue %s patchset %s.' % (issue, patchset))
value = {}
for path in files:
tar_path = 'b/%s' % path
patched_file = None
try:
patched_file = tar.extractfile(tar_path)
data = patched_file.read()
except tarfile.TarError as e:
# Show appropriate error message in the unlikely case that the tarball
# is corrupted.
raise RietveldPatcherError(
'Error extracting tarball for issue %s patchset %s file %s.' %
(issue, patchset, tar_path))
except KeyError as e:
raise FileNotFoundError(
'File %s not found in the tarball for issue %s patchset %s' %
(tar_path, issue, patchset))
finally:
if patched_file:
patched_file.close()
value[path] = data
return value
return resolve
class RietveldPatcher(Patcher): class RietveldPatcher(Patcher):
''' Class to fetch resources from a patchset in Rietveld. ''' Class to fetch resources from a patchset in Rietveld.
''' '''
...@@ -141,10 +96,47 @@ class RietveldPatcher(Patcher): ...@@ -141,10 +96,47 @@ class RietveldPatcher(Patcher):
def Apply(self, paths, file_system, version=None): def Apply(self, paths, file_system, version=None):
if version is None: if version is None:
version = self.GetVersion() version = self.GetVersion()
return Future(callback=_GetAsyncFetchCallback(self._issue,
version, def apply_(tarball_result):
paths, if tarball_result.status_code != 200:
self._fetcher)) raise RietveldPatcherError(
'Failed to download tarball for issue %s patchset %s. Status: %s' %
(self._issue, version, tarball_result.status_code))
try:
tar = tarfile.open(fileobj=StringIO(tarball_result.content))
except tarfile.TarError as e:
raise RietveldPatcherError(
'Error loading tarball for issue %s patchset %s.' % (self._issue,
version))
value = {}
for path in paths:
tar_path = 'b/%s' % path
patched_file = None
try:
patched_file = tar.extractfile(tar_path)
data = patched_file.read()
except tarfile.TarError as e:
# Show appropriate error message in the unlikely case that the tarball
# is corrupted.
raise RietveldPatcherError(
'Error extracting tarball for issue %s patchset %s file %s.' %
(self._issue, version, tar_path))
except KeyError as e:
raise FileNotFoundError(
'File %s not found in the tarball for issue %s patchset %s' %
(tar_path, self._issue, version))
finally:
if patched_file:
patched_file.close()
value[path] = data
return value
return self._fetcher.FetchAsync('tarball/%s/%s' % (self._issue,
version)).Then(apply_)
def GetIdentity(self): def GetIdentity(self):
return self._issue return self._issue
...@@ -95,44 +95,6 @@ def _CreateStatInfo(html): ...@@ -95,44 +95,6 @@ def _CreateStatInfo(html):
return StatInfo(parent_version, child_versions) return StatInfo(parent_version, child_versions)
def _GetAsyncFetchCallback(paths, fetcher, args=None, skip_not_found=False):
def apply_args(path):
return path if args is None else '%s?%s' % (path, args)
def list_dir(directory):
dom = xml.parseString(directory)
files = [elem.childNodes[0].data for elem in dom.getElementsByTagName('a')]
if '..' in files:
files.remove('..')
return files
# A list of tuples of the form (path, Future).
fetches = [(path, fetcher.FetchAsync(apply_args(path))) for path in paths]
def resolve():
value = {}
for path, future in fetches:
try:
result = future.Get()
except Exception as e:
if skip_not_found and IsDownloadError(e): continue
exc_type = FileNotFoundError if IsDownloadError(e) else FileSystemError
raise exc_type('%s fetching %s for Get: %s' %
(type(e).__name__, path, traceback.format_exc()))
if result.status_code == 404:
if skip_not_found: continue
raise FileNotFoundError('Got 404 when fetching %s for Get, content %s' %
(path, result.content))
if result.status_code != 200:
raise FileSystemError('Got %s when fetching %s for Get, content %s' %
(result.status_code, path, result.content))
if path.endswith('/'):
value[path] = list_dir(result.content)
else:
value[path] = result.content
return value
return resolve
class SubversionFileSystem(FileSystem): class SubversionFileSystem(FileSystem):
'''Class to fetch resources from src.chromium.org. '''Class to fetch resources from src.chromium.org.
...@@ -160,11 +122,47 @@ class SubversionFileSystem(FileSystem): ...@@ -160,11 +122,47 @@ class SubversionFileSystem(FileSystem):
if self._revision is not None: if self._revision is not None:
# |fetcher| gets from svn.chromium.org which uses p= for version. # |fetcher| gets from svn.chromium.org which uses p= for version.
args = 'p=%s' % self._revision args = 'p=%s' % self._revision
return Future(callback=_GetAsyncFetchCallback(
paths, def apply_args(path):
self._file_fetcher, return path if args is None else '%s?%s' % (path, args)
args=args,
skip_not_found=skip_not_found)) def list_dir(directory):
dom = xml.parseString(directory)
files = [elem.childNodes[0].data
for elem in dom.getElementsByTagName('a')]
if '..' in files:
files.remove('..')
return files
# A list of tuples of the form (path, Future).
fetches = [(path, self._file_fetcher.FetchAsync(apply_args(path)))
for path in paths]
def resolve():
value = {}
for path, future in fetches:
try:
result = future.Get()
except Exception as e:
if skip_not_found and IsDownloadError(e): continue
exc_type = (FileNotFoundError if IsDownloadError(e)
else FileSystemError)
raise exc_type('%s fetching %s for Get: %s' %
(type(e).__name__, path, traceback.format_exc()))
if result.status_code == 404:
if skip_not_found: continue
raise FileNotFoundError(
'Got 404 when fetching %s for Get, content %s' %
(path, result.content))
if result.status_code != 200:
raise FileSystemError('Got %s when fetching %s for Get, content %s' %
(result.status_code, path, result.content))
if path.endswith('/'):
value[path] = list_dir(result.content)
else:
value[path] = result.content
return value
return Future(callback=resolve)
def Refresh(self): def Refresh(self):
return Future(value=()) return Future(value=())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment