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
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.
'''
......@@ -46,17 +22,31 @@ class PatchedFileSystem(FileSystem):
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 patch_directory_listing(path, original_listing):
added, deleted, modified = (
self._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 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):
return self._base_file_system.Refresh()
......
......@@ -25,51 +25,6 @@ class RietveldPatcherError(Exception):
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 to fetch resources from a patchset in Rietveld.
'''
......@@ -141,10 +96,47 @@ class RietveldPatcher(Patcher):
def Apply(self, paths, file_system, version=None):
if version is None:
version = self.GetVersion()
return Future(callback=_GetAsyncFetchCallback(self._issue,
version,
paths,
self._fetcher))
def apply_(tarball_result):
if tarball_result.status_code != 200:
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):
return self._issue
......@@ -95,19 +95,48 @@ def _CreateStatInfo(html):
return StatInfo(parent_version, child_versions)
def _GetAsyncFetchCallback(paths, fetcher, args=None, skip_not_found=False):
class SubversionFileSystem(FileSystem):
'''Class to fetch resources from src.chromium.org.
'''
@staticmethod
def Create(branch='trunk', revision=None):
if branch == 'trunk':
svn_path = 'trunk/src'
else:
svn_path = 'branches/%s/src' % branch
return SubversionFileSystem(
AppEngineUrlFetcher('%s/%s' % (url_constants.SVN_URL, svn_path)),
AppEngineUrlFetcher('%s/%s' % (url_constants.VIEWVC_URL, svn_path)),
svn_path,
revision=revision)
def __init__(self, file_fetcher, stat_fetcher, svn_path, revision=None):
self._file_fetcher = file_fetcher
self._stat_fetcher = stat_fetcher
self._svn_path = svn_path
self._revision = revision
def Read(self, paths, skip_not_found=False):
args = None
if self._revision is not None:
# |fetcher| gets from svn.chromium.org which uses p= for version.
args = 'p=%s' % self._revision
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')]
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]
fetches = [(path, self._file_fetcher.FetchAsync(apply_args(path)))
for path in paths]
def resolve():
value = {}
......@@ -116,12 +145,14 @@ def _GetAsyncFetchCallback(paths, fetcher, args=None, skip_not_found=False):
result = future.Get()
except Exception as e:
if skip_not_found and IsDownloadError(e): continue
exc_type = FileNotFoundError if IsDownloadError(e) else FileSystemError
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' %
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' %
......@@ -131,40 +162,7 @@ def _GetAsyncFetchCallback(paths, fetcher, args=None, skip_not_found=False):
else:
value[path] = result.content
return value
return resolve
class SubversionFileSystem(FileSystem):
'''Class to fetch resources from src.chromium.org.
'''
@staticmethod
def Create(branch='trunk', revision=None):
if branch == 'trunk':
svn_path = 'trunk/src'
else:
svn_path = 'branches/%s/src' % branch
return SubversionFileSystem(
AppEngineUrlFetcher('%s/%s' % (url_constants.SVN_URL, svn_path)),
AppEngineUrlFetcher('%s/%s' % (url_constants.VIEWVC_URL, svn_path)),
svn_path,
revision=revision)
def __init__(self, file_fetcher, stat_fetcher, svn_path, revision=None):
self._file_fetcher = file_fetcher
self._stat_fetcher = stat_fetcher
self._svn_path = svn_path
self._revision = revision
def Read(self, paths, skip_not_found=False):
args = None
if self._revision is not None:
# |fetcher| gets from svn.chromium.org which uses p= for version.
args = 'p=%s' % self._revision
return Future(callback=_GetAsyncFetchCallback(
paths,
self._file_fetcher,
args=args,
skip_not_found=skip_not_found))
return Future(callback=resolve)
def Refresh(self):
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