| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import os | 5 import os |
| 6 import sys |
| 6 | 7 |
| 8 from docs_server_utils import StringIdentity |
| 7 from file_system import FileSystem, FileNotFoundError, StatInfo, ToUnicode | 9 from file_system import FileSystem, FileNotFoundError, StatInfo, ToUnicode |
| 8 from future import Future | 10 from future import Future |
| 9 | 11 |
| 12 def _ConvertToFilepath(path): |
| 13 return path.replace('/', os.sep) |
| 14 |
| 10 class LocalFileSystem(FileSystem): | 15 class LocalFileSystem(FileSystem): |
| 11 """FileSystem implementation which fetches resources from the local | 16 '''FileSystem implementation which fetches resources from the local |
| 12 filesystem. | 17 filesystem. |
| 13 """ | 18 ''' |
| 14 def __init__(self, base_path): | 19 def __init__(self, base_path): |
| 15 self._base_path = self._ConvertToFilepath(base_path) | 20 self._base_path = _ConvertToFilepath(base_path) |
| 16 | 21 |
| 17 def _ConvertToFilepath(self, path): | 22 @staticmethod |
| 18 return path.replace('/', os.sep) | 23 def Create(): |
| 24 return LocalFileSystem(os.path.join(sys.path[0], os.pardir, os.pardir)) |
| 19 | 25 |
| 20 def _ReadFile(self, filename, binary): | 26 def _ReadFile(self, filename, binary): |
| 21 try: | 27 try: |
| 22 mode = 'rb' if binary else 'r' | 28 mode = 'rb' if binary else 'r' |
| 23 with open(os.path.join(self._base_path, filename), mode) as f: | 29 with open(os.path.join(self._base_path, filename), mode) as f: |
| 24 contents = f.read() | 30 contents = f.read() |
| 25 if binary: | 31 if binary: |
| 26 return contents | 32 return contents |
| 27 return ToUnicode(contents) | 33 return ToUnicode(contents) |
| 28 except IOError as e: | 34 except IOError as e: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 41 if os.path.isdir(os.path.join(full_path, path)): | 47 if os.path.isdir(os.path.join(full_path, path)): |
| 42 all_files.append(path + '/') | 48 all_files.append(path + '/') |
| 43 else: | 49 else: |
| 44 all_files.append(path) | 50 all_files.append(path) |
| 45 return all_files | 51 return all_files |
| 46 | 52 |
| 47 def Read(self, paths, binary=False): | 53 def Read(self, paths, binary=False): |
| 48 result = {} | 54 result = {} |
| 49 for path in paths: | 55 for path in paths: |
| 50 if path.endswith('/'): | 56 if path.endswith('/'): |
| 51 result[path] = self._ListDir(self._ConvertToFilepath(path)) | 57 result[path] = self._ListDir(_ConvertToFilepath(path)) |
| 52 else: | 58 else: |
| 53 result[path] = self._ReadFile(self._ConvertToFilepath(path), binary) | 59 result[path] = self._ReadFile(_ConvertToFilepath(path), binary) |
| 54 return Future(value=result) | 60 return Future(value=result) |
| 55 | 61 |
| 56 def _CreateStatInfo(self, path): | 62 def _CreateStatInfo(self, path): |
| 57 if path.endswith('/'): | |
| 58 versions = dict((filename, os.stat(os.path.join(path, filename)).st_mtime) | |
| 59 for filename in os.listdir(path)) | |
| 60 else: | |
| 61 versions = None | |
| 62 try: | 63 try: |
| 63 return StatInfo(os.stat(path).st_mtime, versions) | 64 path_mtime = os.stat(path).st_mtime |
| 65 if path.endswith('/'): |
| 66 child_versions = dict( |
| 67 (filename, os.stat(os.path.join(path, filename)).st_mtime) |
| 68 for filename in os.listdir(path)) |
| 69 # This file system stat mimics subversion, where the stat of directories |
| 70 # is max(file stats). That means we need to recursively check the whole |
| 71 # file system tree :\ so approximate that by just checking this dir. |
| 72 version = max([path_mtime] + child_versions.values()) |
| 73 else: |
| 74 child_versions = None |
| 75 version = path_mtime |
| 76 return StatInfo(version, child_versions) |
| 64 except OSError as e: | 77 except OSError as e: |
| 65 raise FileNotFoundError('os.stat failed for %s: %s' % (path, e)) | 78 raise FileNotFoundError('os.stat failed for %s: %s' % (path, e)) |
| 66 | 79 |
| 67 def Stat(self, path): | 80 def Stat(self, path): |
| 68 return self._CreateStatInfo(os.path.join(self._base_path, path)) | 81 return self._CreateStatInfo(os.path.join(self._base_path, path)) |
| 82 |
| 83 def GetIdentity(self): |
| 84 return '@'.join((self.__class__.__name__, StringIdentity(self._base_path))) |
| OLD | NEW |