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.perms = {} | |
qyearsley
2016/06/14 21:47:02
Although it's nice that `perms` is the same length
| |
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: |
60 self.perms[f] = '-rwxrwxrwx' | |
61 for f in self.files: | |
59 d = self.dirname(f) | 62 d = self.dirname(f) |
60 while not d in self.dirs: | 63 while not d in self.dirs: |
61 self.dirs.add(d) | 64 self.dirs.add(d) |
62 d = self.dirname(d) | 65 d = self.dirname(d) |
63 | |
64 def clear_written_files(self): | 66 def clear_written_files(self): |
65 # This function can be used to track what is written between steps in a test. | 67 # This function can be used to track what is written between steps in a test. |
66 self.written_files = {} | 68 self.written_files = {} |
67 | 69 |
68 def _raise_not_found(self, path): | 70 def _raise_not_found(self, path): |
69 raise IOError(errno.ENOENT, path, os.strerror(errno.ENOENT)) | 71 raise IOError(errno.ENOENT, path, os.strerror(errno.ENOENT)) |
70 | 72 |
71 def _split(self, path): | 73 def _split(self, path): |
72 # This is not quite a full implementation of os.path.split | 74 # This is not quite a full implementation of os.path.split |
73 # http://docs.python.org/library/os.path.html#os.path.split | 75 # http://docs.python.org/library/os.path.html#os.path.split |
74 if self.sep in path: | 76 if self.sep in path: |
75 return path.rsplit(self.sep, 1) | 77 return path.rsplit(self.sep, 1) |
76 return ('', path) | 78 return ('', path) |
77 | 79 |
80 def chmod(self, file_path, mode): | |
81 # Incomplete implementation of os.chmod | |
82 # only supports number modes | |
83 mode_list = [] | |
84 for i in range(0, 3): | |
85 temp = mode % 10 | |
86 mode_list.append(temp) | |
87 mode /= 10 | |
88 temp = i | |
89 temp_perms = '-' | |
90 temp_perms += self.chmod_options(mode_list[0]) | |
91 temp_perms += self.chmod_options(mode_list[1]) | |
92 temp_perms += self.chmod_options(mode_list[2]) | |
93 self.perms[file_path] = temp_perms | |
qyearsley
2016/06/14 21:47:02
List comprehensions (and related expressions) are
| |
94 | |
95 def chmod_options(self, arg): | |
qyearsley
2016/06/14 21:47:02
1. The variable name `arg` could perhaps be improv
| |
96 switcher = { | |
97 0: '---', | |
98 1: '--x', | |
99 2: '-w-', | |
100 3: '-wx', | |
101 4: 'r--', | |
102 5: 'r-x', | |
103 6: 'rw-', | |
104 7: 'rwx', | |
105 } | |
106 return switcher.get(arg, None) | |
qyearsley
2016/06/14 21:47:02
None is the default default for dict.get, so this
| |
107 | |
78 def abspath(self, path): | 108 def abspath(self, path): |
79 if os.path.isabs(path): | 109 if os.path.isabs(path): |
80 return self.normpath(path) | 110 return self.normpath(path) |
81 return self.abspath(self.join(self.cwd, path)) | 111 return self.abspath(self.join(self.cwd, path)) |
82 | 112 |
83 def realpath(self, path): | 113 def realpath(self, path): |
84 return self.abspath(path) | 114 return self.abspath(path) |
85 | 115 |
86 def basename(self, path): | 116 def basename(self, path): |
87 return self._split(path)[1] | 117 return self._split(path)[1] |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
490 return self.data.readline(length) | 520 return self.data.readline(length) |
491 | 521 |
492 def __iter__(self): | 522 def __iter__(self): |
493 return self.data.__iter__() | 523 return self.data.__iter__() |
494 | 524 |
495 def next(self): | 525 def next(self): |
496 return self.data.next() | 526 return self.data.next() |
497 | 527 |
498 def seek(self, offset, whence=os.SEEK_SET): | 528 def seek(self, offset, whence=os.SEEK_SET): |
499 self.data.seek(offset, whence) | 529 self.data.seek(offset, whence) |
OLD | NEW |