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 | 6 |
7 import file_system | 7 from file_system import FileSystem, FileNotFoundError, StatInfo, ToUnicode |
8 from future import Future | 8 from future import Future |
9 | 9 |
10 class LocalFileSystem(file_system.FileSystem): | 10 class LocalFileSystem(FileSystem): |
11 """FileSystem implementation which fetches resources from the local | 11 """FileSystem implementation which fetches resources from the local |
12 filesystem. | 12 filesystem. |
13 """ | 13 """ |
14 def __init__(self, base_path): | 14 def __init__(self, base_path): |
15 self._base_path = self._ConvertToFilepath(base_path) | 15 self._base_path = self._ConvertToFilepath(base_path) |
16 | 16 |
17 def _ConvertToFilepath(self, path): | 17 def _ConvertToFilepath(self, path): |
18 return path.replace('/', os.sep) | 18 return path.replace('/', os.sep) |
19 | 19 |
20 def _ReadFile(self, filename, binary): | 20 def _ReadFile(self, filename, binary): |
21 try: | 21 try: |
22 mode = 'rb' if binary else 'r' | 22 mode = 'rb' if binary else 'r' |
23 with open(os.path.join(self._base_path, filename), mode) as f: | 23 with open(os.path.join(self._base_path, filename), mode) as f: |
24 contents = f.read() | 24 contents = f.read() |
25 if binary: | 25 if binary: |
26 return contents | 26 return contents |
27 return file_system.ToUnicode(contents) | 27 return ToUnicode(contents) |
28 except IOError: | 28 except IOError as e: |
29 raise file_system.FileNotFoundError(filename) | 29 raise FileNotFoundError('Read failed for %s: %s' % (filename, e)) |
30 | 30 |
31 def _ListDir(self, dir_name): | 31 def _ListDir(self, dir_name): |
32 all_files = [] | 32 all_files = [] |
33 full_path = os.path.join(self._base_path, dir_name) | 33 full_path = os.path.join(self._base_path, dir_name) |
34 try: | 34 try: |
35 files = os.listdir(full_path) | 35 files = os.listdir(full_path) |
36 except OSError: | 36 except OSError as e: |
37 raise file_system.FileNotFoundError(dir_name) | 37 raise FileNotFoundError('os.listdir failed for %s: %s' % (dir_name, e)) |
38 for path in files: | 38 for path in files: |
39 if path.startswith('.'): | 39 if path.startswith('.'): |
40 continue | 40 continue |
41 if os.path.isdir(os.path.join(full_path, path)): | 41 if os.path.isdir(os.path.join(full_path, path)): |
42 all_files.append(path + '/') | 42 all_files.append(path + '/') |
43 else: | 43 else: |
44 all_files.append(path) | 44 all_files.append(path) |
45 return all_files | 45 return all_files |
46 | 46 |
47 def Read(self, paths, binary=False): | 47 def Read(self, paths, binary=False): |
48 result = {} | 48 result = {} |
49 for path in paths: | 49 for path in paths: |
50 if path.endswith('/'): | 50 if path.endswith('/'): |
51 result[path] = self._ListDir(self._ConvertToFilepath(path)) | 51 result[path] = self._ListDir(self._ConvertToFilepath(path)) |
52 else: | 52 else: |
53 result[path] = self._ReadFile(self._ConvertToFilepath(path), binary) | 53 result[path] = self._ReadFile(self._ConvertToFilepath(path), binary) |
54 return Future(value=result) | 54 return Future(value=result) |
55 | 55 |
56 def _CreateStatInfo(self, path): | 56 def _CreateStatInfo(self, path): |
57 if path.endswith('/'): | 57 if path.endswith('/'): |
58 versions = dict((filename, os.stat(os.path.join(path, filename)).st_mtime) | 58 versions = dict((filename, os.stat(os.path.join(path, filename)).st_mtime) |
59 for filename in os.listdir(path)) | 59 for filename in os.listdir(path)) |
60 else: | 60 else: |
61 versions = None | 61 versions = None |
62 return file_system.StatInfo(os.stat(path).st_mtime, versions) | 62 try: |
| 63 return StatInfo(os.stat(path).st_mtime, versions) |
| 64 except OSError as e: |
| 65 raise FileNotFoundError('os.stat failed for %s: %s' % (path, e)) |
63 | 66 |
64 def Stat(self, path): | 67 def Stat(self, path): |
65 try: | 68 return self._CreateStatInfo(os.path.join(self._base_path, path)) |
66 return self._CreateStatInfo(os.path.join(self._base_path, path)) | |
67 except OSError: | |
68 raise file_system.FileNotFoundError(path) | |
OLD | NEW |