Index: chrome/common/extensions/docs/server2/local_file_system.py |
diff --git a/chrome/common/extensions/docs/server2/local_file_system.py b/chrome/common/extensions/docs/server2/local_file_system.py |
index 8a6aae41125265ccc439b95959fd9f0e5823d621..2e9667ecc3c36217cc706de11ebf50a2d6637153 100644 |
--- a/chrome/common/extensions/docs/server2/local_file_system.py |
+++ b/chrome/common/extensions/docs/server2/local_file_system.py |
@@ -12,6 +12,54 @@ from future import Future |
def _ConvertToFilepath(path): |
return path.replace('/', os.sep) |
+def _ConvertFromFilepath(path): |
+ return path.replace(os.sep, '/') |
+ |
+def _ReadFile(filename, binary): |
+ try: |
+ mode = 'rb' if binary else 'r' |
+ with open(filename, mode) as f: |
+ contents = f.read() |
+ if binary: |
+ return contents |
+ return ToUnicode(contents) |
+ except IOError as e: |
+ raise FileNotFoundError('Read failed for %s: %s' % (filename, e)) |
+ |
+def _ListDir(dir_name): |
+ all_files = [] |
+ try: |
+ files = os.listdir(dir_name) |
+ except OSError as e: |
+ raise FileNotFoundError('os.listdir failed for %s: %s' % (dir_name, e)) |
+ for os_path in files: |
+ posix_path = _ConvertFromFilepath(os_path) |
+ if os_path.startswith('.'): |
+ continue |
+ if os.path.isdir(os.path.join(dir_name, os_path)): |
+ all_files.append(posix_path + '/') |
+ else: |
+ all_files.append(posix_path) |
+ return all_files |
+ |
+def _CreateStatInfo(path): |
+ try: |
+ path_mtime = os.stat(path).st_mtime |
+ if path.endswith('/'): |
+ child_versions = dict((_ConvertFromFilepath(filename), |
+ os.stat(os.path.join(path, filename)).st_mtime) |
+ for filename in os.listdir(path)) |
+ # This file system stat mimics subversion, where the stat of directories |
+ # is max(file stats). That means we need to recursively check the whole |
+ # file system tree :\ so approximate that by just checking this dir. |
+ version = max([path_mtime] + child_versions.values()) |
+ else: |
+ child_versions = None |
+ version = path_mtime |
+ return StatInfo(version, child_versions) |
+ except OSError as e: |
+ raise FileNotFoundError('os.stat failed for %s: %s' % (path, e)) |
+ |
class LocalFileSystem(FileSystem): |
'''FileSystem implementation which fetches resources from the local |
filesystem. |
@@ -23,62 +71,21 @@ class LocalFileSystem(FileSystem): |
def Create(): |
return LocalFileSystem(os.path.join(sys.path[0], os.pardir, os.pardir)) |
- def _ReadFile(self, filename, binary): |
- try: |
- mode = 'rb' if binary else 'r' |
- with open(os.path.join(self._base_path, filename), mode) as f: |
- contents = f.read() |
- if binary: |
- return contents |
- return ToUnicode(contents) |
- except IOError as e: |
- raise FileNotFoundError('Read failed for %s: %s' % (filename, e)) |
- |
- def _ListDir(self, dir_name): |
- all_files = [] |
- full_path = os.path.join(self._base_path, dir_name) |
- try: |
- files = os.listdir(full_path) |
- except OSError as e: |
- raise FileNotFoundError('os.listdir failed for %s: %s' % (dir_name, e)) |
- for path in files: |
- if path.startswith('.'): |
- continue |
- if os.path.isdir(os.path.join(full_path, path)): |
- all_files.append(path + '/') |
- else: |
- all_files.append(path) |
- return all_files |
- |
def Read(self, paths, binary=False): |
result = {} |
for path in paths: |
+ full_path = os.path.join(self._base_path, |
+ _ConvertToFilepath(path).lstrip(os.sep)) |
if path.endswith('/'): |
- result[path] = self._ListDir(_ConvertToFilepath(path)) |
+ result[path] = _ListDir(full_path) |
else: |
- result[path] = self._ReadFile(_ConvertToFilepath(path), binary) |
+ result[path] = _ReadFile(full_path, binary) |
return Future(value=result) |
- def _CreateStatInfo(self, path): |
- try: |
- path_mtime = os.stat(path).st_mtime |
- if path.endswith('/'): |
- child_versions = dict( |
- (filename, os.stat(os.path.join(path, filename)).st_mtime) |
- for filename in os.listdir(path)) |
- # This file system stat mimics subversion, where the stat of directories |
- # is max(file stats). That means we need to recursively check the whole |
- # file system tree :\ so approximate that by just checking this dir. |
- version = max([path_mtime] + child_versions.values()) |
- else: |
- child_versions = None |
- version = path_mtime |
- return StatInfo(version, child_versions) |
- except OSError as e: |
- raise FileNotFoundError('os.stat failed for %s: %s' % (path, e)) |
- |
def Stat(self, path): |
- return self._CreateStatInfo(os.path.join(self._base_path, path)) |
+ full_path = os.path.join(self._base_path, |
+ _ConvertToFilepath(path).lstrip(os.sep)) |
+ return _CreateStatInfo(full_path) |
def GetIdentity(self): |
return '@'.join((self.__class__.__name__, StringIdentity(self._base_path))) |