Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/rietveld_file_system.py |
| =================================================================== |
| --- chrome/common/extensions/docs/server2/rietveld_file_system.py (revision 0) |
| +++ chrome/common/extensions/docs/server2/rietveld_file_system.py (revision 0) |
| @@ -0,0 +1,115 @@ |
| +# 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. |
| + |
| +import logging |
| +import tarfile |
| +from StringIO import StringIO |
| + |
| +from file_system import FileSystem, FileNotFoundError, StatInfo, ToUnicode |
| +from future import Future |
| + |
| +# Use a special value other than None to represent a deleted file in the patch. |
| +_FILE_NOT_FOUND_VALUE = (None,) |
| + |
| +class _AsyncFetchFuture(object): |
| + def __init__(self, |
| + base_path, |
| + paths, |
| + cached_files, |
| + missing_paths, |
| + binary, |
| + patch_data, |
| + fetcher, |
| + object_store): |
| + self._base_path = base_path |
| + self._paths = paths |
| + self._cached_value = cached_files |
| + self._missing_paths = missing_paths |
| + self._binary = binary |
| + self._patch_data = patch_data |
| + self._object_store = object_store |
| + if missing_paths is not None: |
| + self._tarball = fetcher.FetchAsync('tarball/%s/%s' % |
| + (patch_data.issue, patch_data.patchset)) |
| + |
| + def _GetMissingPaths(self): |
| + tarball_result = self._tarball.Get() |
| + if tarball_result.status_code != 200: |
| + raise FileNotFoundError('Failed to download tarball.') |
| + |
| + try: |
| + tar = tarfile.open(fileobj=StringIO(tarball_result.content)) |
| + except tarfile.TarError as e: |
| + raise FileNotFoundError('Invalid tarball.') |
| + |
| + self._uncached_value = {} |
| + for path in self._patch_data.files: |
| + if self._cached_value.get(path) is not None: |
| + continue |
| + |
| + if self._base_path: |
| + tar_path = 'b/%s/%s' % (self._base_path, path) |
| + else: |
| + tar_path = 'b/%s' % path |
| + try: |
| + patched_file = tar.extractfile(tar_path) |
| + data = patched_file.read() |
| + 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__.
|
| + except tarfile.TarError as e: |
| + self._uncached_value[path] = _FILE_NOT_FOUND_VALUE |
| + continue |
| + |
| + # Deleted files still exist in the tarball, but they are empty. |
| + if len(data) == 0: |
| + self._uncached_value[path] = _FILE_NOT_FOUND_VALUE |
| + elif self._binary: |
| + self._uncached_value[path] = data |
| + else: |
| + self._uncached_value[path] = ToUnicode(data) |
| + |
| + self._object_store.SetMulti(self._uncached_value) |
| + |
| + for path in self._missing_paths: |
| + if self._uncached_value.get(path) is None: |
| + raise FileNotFoundError('File %s was not found in the patch.' % path) |
| + self._cached_value[path] = self._uncached_value[path] |
| + |
| + def Get(self): |
| + if self._missing_paths is not None: |
| + self._GetMissingPaths() |
| + |
| + # Make sure all paths exist before returning. |
| + for path in self._paths: |
| + if self._cached_value[path] == _FILE_NOT_FOUND_VALUE: |
| + raise FileNotFoundError('File %s was deleted in the patch.' % path) |
| + return self._cached_value |
| + |
| +class RietveldFileSystem(FileSystem): |
| + ''' Class to fetch resources from a patchset in Rietveld. |
| + ''' |
| + def __init__(self, |
| + base_path, |
| + patch_data, |
| + fetcher, |
| + object_store_creator_factory): |
| + self._base_path = base_path |
| + self._patch_data = patch_data |
| + self._fetcher = fetcher |
| + self._object_store = object_store_creator_factory.Create( |
| + RietveldFileSystem).Create() |
| + |
| + def Read(self, paths, binary=False): |
| + cached_files = self._object_store.GetMulti(paths).Get() |
| + missing_paths = list(set(paths) - set(cached_files.keys())) |
| + if len(missing_paths) == 0: |
| + missing_paths = None |
| + return Future(delegate=_AsyncFetchFuture( |
| + self._base_path, |
| + paths, |
| + cached_files, |
| + missing_paths, |
| + binary, |
| + self._patch_data, |
| + self._fetcher, |
| + 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
|
| Property changes on: chrome/common/extensions/docs/server2/rietveld_file_system.py |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |