OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import errno | 6 import errno |
7 import os | 7 import os |
8 import re | 8 import re |
9 import StringIO | 9 import StringIO |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 self.written_files = {} | 25 self.written_files = {} |
26 self._sep = '/' | 26 self._sep = '/' |
27 | 27 |
28 @property | 28 @property |
29 def sep(self): | 29 def sep(self): |
30 return self._sep | 30 return self._sep |
31 | 31 |
32 def _split(self, path): | 32 def _split(self, path): |
33 return path.rsplit(self.sep, 1) | 33 return path.rsplit(self.sep, 1) |
34 | 34 |
| 35 def abspath(self, path): |
| 36 if path.endswith(self.sep): |
| 37 return path[:-1] |
| 38 return path |
| 39 |
35 def dirname(self, path): | 40 def dirname(self, path): |
36 if not self.sep in path: | 41 if self.sep not in path: |
37 return '' | 42 return '' |
38 return self._split(path)[0] | 43 return self._split(path)[0] or self.sep |
39 | 44 |
40 def exists(self, path): | 45 def exists(self, path): |
41 return self.isfile(path) or self.isdir(path) | 46 return self.isfile(path) or self.isdir(path) |
42 | 47 |
43 def isfile(self, path): | 48 def isfile(self, path): |
44 return path in self.files and self.files[path] is not None | 49 return path in self.files and self.files[path] is not None |
45 | 50 |
46 def isdir(self, path): | 51 def isdir(self, path): |
47 if path in self.files: | 52 if path in self.files: |
48 return False | 53 return False |
49 if not path.endswith(self.sep): | 54 if not path.endswith(self.sep): |
50 path += self.sep | 55 path += self.sep |
51 | 56 |
52 # We need to use a copy of the keys here in order to avoid switching | 57 # We need to use a copy of the keys here in order to avoid switching |
53 # to a different thread and potentially modifying the dict in | 58 # to a different thread and potentially modifying the dict in |
54 # mid-iteration. | 59 # mid-iteration. |
55 files = self.files.keys()[:] | 60 files = self.files.keys()[:] |
56 return any(f.startswith(path) for f in files) | 61 return any(f.startswith(path) for f in files) |
57 | 62 |
58 def join(self, *comps): | 63 def join(self, *comps): |
59 # FIXME: might want tests for this and/or a better comment about how | 64 # TODO: Might want tests for this and/or a better comment about how |
60 # it works. | 65 # it works. |
61 return re.sub(re.escape(os.path.sep), self.sep, os.path.join(*comps)) | 66 return re.sub(re.escape(os.path.sep), self.sep, os.path.join(*comps)) |
62 | 67 |
63 def open_for_reading(self, path): | 68 def open_for_reading(self, path): |
64 return StringIO.StringIO(self.read_binary_file(path)) | 69 return StringIO.StringIO(self.read_binary_file(path)) |
65 | 70 |
66 def read_binary_file(self, path): | 71 def read_binary_file(self, path): |
67 # Intentionally raises KeyError if we don't recognize the path. | 72 # Intentionally raises KeyError if we don't recognize the path. |
68 if self.files[path] is None: | 73 if self.files[path] is None: |
69 _RaiseNotFound(path) | 74 _RaiseNotFound(path) |
70 return self.files[path] | 75 return self.files[path] |
OLD | NEW |