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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 148 |
149 def GetIdentity(self): | 149 def GetIdentity(self): |
150 '''The identity of the file system, exposed for caching classes to | 150 '''The identity of the file system, exposed for caching classes to |
151 namespace their caches. this will usually depend on the configuration of | 151 namespace their caches. this will usually depend on the configuration of |
152 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 |
153 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 |
154 different to a LocalFileSystem with a base path of /usr. | 154 different to a LocalFileSystem with a base path of /usr. |
155 ''' | 155 ''' |
156 raise NotImplementedError(self.__class__) | 156 raise NotImplementedError(self.__class__) |
157 | 157 |
158 def Walk(self, root, depth=-1): | 158 def Walk(self, root, depth=-1, file_lister=None): |
159 '''Recursively walk the directories in a file system, starting with root. | 159 '''Recursively walk the directories in a file system, starting with root. |
160 | 160 |
161 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 |
162 (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|, |
163 |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 |
164 |base| respectively. If |depth| is specified and greater than 0, Walk will | 164 |base| respectively. If |depth| is specified and greater than 0, Walk will |
165 only recurse |depth| times. | 165 only recurse |depth| times. |
166 | 166 |
| 167 |file_lister|, if specified, should be a callback of signature |
| 168 |
| 169 def my_file_lister(root):, |
| 170 |
| 171 which returns a tuple (dirs, files), where |dirs| is a list of directory |
| 172 names under |root|, and |files| is a list of file names under |root|. Note |
| 173 that the listing of files and directories should be for a *single* level |
| 174 only, i.e. it should not recursively list anything. |
| 175 |
167 Note that directories will always end with a '/', files never will. | 176 Note that directories will always end with a '/', files never will. |
168 | 177 |
169 If |root| cannot be found, raises a FileNotFoundError. | 178 If |root| cannot be found, raises a FileNotFoundError. |
170 For any other failure, raises a FileSystemError. | 179 For any other failure, raises a FileSystemError. |
171 ''' | 180 ''' |
172 AssertIsDirectory(root) | 181 AssertIsDirectory(root) |
173 basepath = root | 182 basepath = root |
174 | 183 |
175 def walk(root, depth): | 184 def walk(root, depth): |
176 if depth == 0: | 185 if depth == 0: |
177 return | 186 return |
178 AssertIsDirectory(root) | 187 AssertIsDirectory(root) |
179 dirs, files = [], [] | |
180 | 188 |
181 for f in self.ReadSingle(root).Get(): | 189 if file_lister: |
182 if IsDirectory(f): | 190 dirs, files = file_lister(root) |
183 dirs.append(f) | 191 else: |
184 else: | 192 dirs, files = [], [] |
185 files.append(f) | 193 for f in self.ReadSingle(root).Get(): |
| 194 if IsDirectory(f): |
| 195 dirs.append(f) |
| 196 else: |
| 197 files.append(f) |
186 | 198 |
187 yield root[len(basepath):].rstrip('/'), dirs, files | 199 yield root[len(basepath):].rstrip('/'), dirs, files |
188 | 200 |
189 for d in dirs: | 201 for d in dirs: |
190 for walkinfo in walk(root + d, depth - 1): | 202 for walkinfo in walk(root + d, depth - 1): |
191 yield walkinfo | 203 yield walkinfo |
192 | 204 |
193 for walkinfo in walk(root, depth): | 205 for walkinfo in walk(root, depth): |
194 yield walkinfo | 206 yield walkinfo |
195 | 207 |
196 def __eq__(self, other): | 208 def __eq__(self, other): |
197 return (isinstance(other, FileSystem) and | 209 return (isinstance(other, FileSystem) and |
198 self.GetIdentity() == other.GetIdentity()) | 210 self.GetIdentity() == other.GetIdentity()) |
199 | 211 |
200 def __ne__(self, other): | 212 def __ne__(self, other): |
201 return not (self == other) | 213 return not (self == other) |
202 | 214 |
203 def __repr__(self): | 215 def __repr__(self): |
204 return '<%s>' % type(self).__name__ | 216 return '<%s>' % type(self).__name__ |
205 | 217 |
206 def __str__(self): | 218 def __str__(self): |
207 return repr(self) | 219 return repr(self) |
OLD | NEW |