| OLD | NEW |
| 1 # Copyright (C) 2009 Google Inc. All rights reserved. | 1 # Copyright (C) 2009 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 def __init__(self, files=None, dirs=None, cwd='/'): | 42 def __init__(self, files=None, dirs=None, cwd='/'): |
| 43 """Initializes a "mock" filesystem that can be used to completely | 43 """Initializes a "mock" filesystem that can be used to completely |
| 44 stub out a filesystem. | 44 stub out a filesystem. |
| 45 | 45 |
| 46 Args: | 46 Args: |
| 47 files: a dict of filenames -> file contents. A file contents | 47 files: a dict of filenames -> file contents. A file contents |
| 48 value of None is used to indicate that the file should | 48 value of None is used to indicate that the file should |
| 49 not exist. | 49 not exist. |
| 50 """ | 50 """ |
| 51 self.files = files or {} | 51 self.files = files or {} |
| 52 self.executable_files = set() |
| 52 self.written_files = {} | 53 self.written_files = {} |
| 53 self.last_tmpdir = None | 54 self.last_tmpdir = None |
| 54 self.current_tmpno = 0 | 55 self.current_tmpno = 0 |
| 55 self.cwd = cwd | 56 self.cwd = cwd |
| 56 self.dirs = set(dirs or []) | 57 self.dirs = set(dirs or []) |
| 57 self.dirs.add(cwd) | 58 self.dirs.add(cwd) |
| 58 for f in self.files: | 59 for f in self.files: |
| 59 d = self.dirname(f) | 60 d = self.dirname(f) |
| 60 while not d in self.dirs: | 61 while not d in self.dirs: |
| 61 self.dirs.add(d) | 62 self.dirs.add(d) |
| 62 d = self.dirname(d) | 63 d = self.dirname(d) |
| 63 | |
| 64 def clear_written_files(self): | 64 def clear_written_files(self): |
| 65 # This function can be used to track what is written between steps in a
test. | 65 # This function can be used to track what is written between steps in a
test. |
| 66 self.written_files = {} | 66 self.written_files = {} |
| 67 | 67 |
| 68 def _raise_not_found(self, path): | 68 def _raise_not_found(self, path): |
| 69 raise IOError(errno.ENOENT, path, os.strerror(errno.ENOENT)) | 69 raise IOError(errno.ENOENT, path, os.strerror(errno.ENOENT)) |
| 70 | 70 |
| 71 def _split(self, path): | 71 def _split(self, path): |
| 72 # This is not quite a full implementation of os.path.split | 72 # This is not quite a full implementation of os.path.split |
| 73 # http://docs.python.org/library/os.path.html#os.path.split | 73 # http://docs.python.org/library/os.path.html#os.path.split |
| 74 if self.sep in path: | 74 if self.sep in path: |
| 75 return path.rsplit(self.sep, 1) | 75 return path.rsplit(self.sep, 1) |
| 76 return ('', path) | 76 return ('', path) |
| 77 | 77 |
| 78 def is_executable(self, file_path): |
| 79 return file_path in self.executable_files |
| 80 |
| 81 def make_executable(self, orig_path, new_path): |
| 82 if self.is_executable(orig_path): |
| 83 self.executable_files.add(new_path) |
| 84 |
| 78 def abspath(self, path): | 85 def abspath(self, path): |
| 79 if os.path.isabs(path): | 86 if os.path.isabs(path): |
| 80 return self.normpath(path) | 87 return self.normpath(path) |
| 81 return self.abspath(self.join(self.cwd, path)) | 88 return self.abspath(self.join(self.cwd, path)) |
| 82 | 89 |
| 83 def realpath(self, path): | 90 def realpath(self, path): |
| 84 return self.abspath(path) | 91 return self.abspath(path) |
| 85 | 92 |
| 86 def basename(self, path): | 93 def basename(self, path): |
| 87 return self._split(path)[1] | 94 return self._split(path)[1] |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 return self.data.readline(length) | 497 return self.data.readline(length) |
| 491 | 498 |
| 492 def __iter__(self): | 499 def __iter__(self): |
| 493 return self.data.__iter__() | 500 return self.data.__iter__() |
| 494 | 501 |
| 495 def next(self): | 502 def next(self): |
| 496 return self.data.next() | 503 return self.data.next() |
| 497 | 504 |
| 498 def seek(self, offset, whence=os.SEEK_SET): | 505 def seek(self, offset, whence=os.SEEK_SET): |
| 499 self.data.seek(offset, whence) | 506 self.data.seek(offset, whence) |
| OLD | NEW |