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 posixpath | 5 import posixpath |
6 import traceback | 6 import traceback |
7 | 7 |
8 from future import Future | 8 from future import Future |
9 from path_util import ( | 9 from path_util import ( |
10 AssertIsDirectory, AssertIsValid, IsDirectory, IsValid, SplitParent, | 10 AssertIsDirectory, AssertIsValid, IsDirectory, IsValid, SplitParent, |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 - Otherwise, and by default, a FileNotFoundError is raised. This is | 79 - Otherwise, and by default, a FileNotFoundError is raised. This is |
80 guaranteed to only happen once the Future has been resolved (Get() | 80 guaranteed to only happen once the Future has been resolved (Get() |
81 called). | 81 called). |
82 | 82 |
83 For any other failure, raises a FileSystemError. | 83 For any other failure, raises a FileSystemError. |
84 ''' | 84 ''' |
85 raise NotImplementedError(self.__class__) | 85 raise NotImplementedError(self.__class__) |
86 | 86 |
87 def ReadSingle(self, path, skip_not_found=False): | 87 def ReadSingle(self, path, skip_not_found=False): |
88 '''Reads a single file from the FileSystem. Returns a Future with the same | 88 '''Reads a single file from the FileSystem. Returns a Future with the same |
89 rules as Read(). If |path| is not found raise a FileNotFoundError on Get(). | 89 rules as Read(). If |path| is not found raise a FileNotFoundError on Get(), |
| 90 or if |skip_not_found| is True then return None. |
90 ''' | 91 ''' |
91 AssertIsValid(path) | 92 AssertIsValid(path) |
92 read_single = self.Read([path], skip_not_found=skip_not_found) | 93 read_single = self.Read([path], skip_not_found=skip_not_found) |
93 return Future(callback=lambda: read_single.Get().get(path, None)) | 94 return Future(callback=lambda: read_single.Get().get(path, None)) |
94 | 95 |
95 def Exists(self, path): | 96 def Exists(self, path): |
96 '''Returns a Future to the existence of |path|; True if |path| exists, | 97 '''Returns a Future to the existence of |path|; True if |path| exists, |
97 False if not. This method will not throw a FileNotFoundError unlike | 98 False if not. This method will not throw a FileNotFoundError unlike |
98 the Read* methods, however it may still throw a FileSystemError. | 99 the Read* methods, however it may still throw a FileSystemError. |
99 | 100 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 148 |
148 def GetIdentity(self): | 149 def GetIdentity(self): |
149 '''The identity of the file system, exposed for caching classes to | 150 '''The identity of the file system, exposed for caching classes to |
150 namespace their caches. this will usually depend on the configuration of | 151 namespace their caches. this will usually depend on the configuration of |
151 that file system - e.g. a LocalFileSystem with a base path of /var is | 152 that file system - e.g. a LocalFileSystem with a base path of /var is |
152 different to that of a SubversionFileSystem with a base path of /bar, is | 153 different to that of a SubversionFileSystem with a base path of /bar, is |
153 different to a LocalFileSystem with a base path of /usr. | 154 different to a LocalFileSystem with a base path of /usr. |
154 ''' | 155 ''' |
155 raise NotImplementedError(self.__class__) | 156 raise NotImplementedError(self.__class__) |
156 | 157 |
157 def Walk(self, root): | 158 def Walk(self, root, depth=-1): |
158 '''Recursively walk the directories in a file system, starting with root. | 159 '''Recursively walk the directories in a file system, starting with root. |
159 | 160 |
160 Behaviour is very similar to os.walk from the standard os module, yielding | 161 Behaviour is very similar to os.walk from the standard os module, yielding |
161 (base, dirs, files) recursively, where |base| is the base path of |files|, | 162 (base, dirs, files) recursively, where |base| is the base path of |files|, |
162 |dirs| relative to |root|, and |files| and |dirs| the list of files/dirs in | 163 |dirs| relative to |root|, and |files| and |dirs| the list of files/dirs in |
163 |base| respectively. | 164 |base| respectively. If |depth| is specified and greater than 0, Walk will |
| 165 only recurse |depth| times. |
164 | 166 |
165 Note that directories will always end with a '/', files never will. | 167 Note that directories will always end with a '/', files never will. |
166 | 168 |
167 If |root| cannot be found, raises a FileNotFoundError. | 169 If |root| cannot be found, raises a FileNotFoundError. |
168 For any other failure, raises a FileSystemError. | 170 For any other failure, raises a FileSystemError. |
169 ''' | 171 ''' |
170 AssertIsDirectory(root) | 172 AssertIsDirectory(root) |
171 basepath = root | 173 basepath = root |
172 | 174 |
173 def walk(root): | 175 def walk(root, depth): |
| 176 if depth == 0: |
| 177 return |
174 AssertIsDirectory(root) | 178 AssertIsDirectory(root) |
175 dirs, files = [], [] | 179 dirs, files = [], [] |
176 | 180 |
177 for f in self.ReadSingle(root).Get(): | 181 for f in self.ReadSingle(root).Get(): |
178 if IsDirectory(f): | 182 if IsDirectory(f): |
179 dirs.append(f) | 183 dirs.append(f) |
180 else: | 184 else: |
181 files.append(f) | 185 files.append(f) |
182 | 186 |
183 yield root[len(basepath):].rstrip('/'), dirs, files | 187 yield root[len(basepath):].rstrip('/'), dirs, files |
184 | 188 |
185 for d in dirs: | 189 for d in dirs: |
186 for walkinfo in walk(root + d): | 190 for walkinfo in walk(root + d, depth - 1): |
187 yield walkinfo | 191 yield walkinfo |
188 | 192 |
189 for walkinfo in walk(root): | 193 for walkinfo in walk(root, depth): |
190 yield walkinfo | 194 yield walkinfo |
191 | 195 |
192 def __eq__(self, other): | 196 def __eq__(self, other): |
193 return (isinstance(other, FileSystem) and | 197 return (isinstance(other, FileSystem) and |
194 self.GetIdentity() == other.GetIdentity()) | 198 self.GetIdentity() == other.GetIdentity()) |
195 | 199 |
196 def __ne__(self, other): | 200 def __ne__(self, other): |
197 return not (self == other) | 201 return not (self == other) |
198 | 202 |
199 def __repr__(self): | 203 def __repr__(self): |
200 return '<%s>' % type(self).__name__ | 204 return '<%s>' % type(self).__name__ |
201 | 205 |
202 def __str__(self): | 206 def __str__(self): |
203 return repr(self) | 207 return repr(self) |
OLD | NEW |