Chromium Code Reviews| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 | 138 |
| 139 def GetIdentity(self): | 139 def GetIdentity(self): |
| 140 '''The identity of the file system, exposed for caching classes to | 140 '''The identity of the file system, exposed for caching classes to |
| 141 namespace their caches. this will usually depend on the configuration of | 141 namespace their caches. this will usually depend on the configuration of |
| 142 that file system - e.g. a LocalFileSystem with a base path of /var is | 142 that file system - e.g. a LocalFileSystem with a base path of /var is |
| 143 different to that of a SubversionFileSystem with a base path of /bar, is | 143 different to that of a SubversionFileSystem with a base path of /bar, is |
| 144 different to a LocalFileSystem with a base path of /usr. | 144 different to a LocalFileSystem with a base path of /usr. |
| 145 ''' | 145 ''' |
| 146 raise NotImplementedError(self.__class__) | 146 raise NotImplementedError(self.__class__) |
| 147 | 147 |
| 148 def Walk(self, root): | 148 def Walk(self, root, depth=-1): |
| 149 '''Recursively walk the directories in a file system, starting with root. | 149 '''Recursively walk the directories in a file system, starting with root. |
| 150 | 150 |
| 151 Behaviour is very similar to os.walk from the standard os module, yielding | 151 Behaviour is very similar to os.walk from the standard os module, yielding |
| 152 (base, dirs, files) recursively, where |base| is the base path of |files|, | 152 (base, dirs, files) recursively, where |base| is the base path of |files|, |
| 153 |dirs| relative to |root|, and |files| and |dirs| the list of files/dirs in | 153 |dirs| relative to |root|, and |files| and |dirs| the list of files/dirs in |
| 154 |base| respectively. | 154 |base| respectively. If |depth| is specified and greater than 0, Walk will |
|
not at google - send to devlin
2014/08/15 15:45:50
Quick test for specifying depth=0, 1, or 2 plz.
| |
| 155 only recurse |depth| times. | |
| 155 | 156 |
| 156 Note that directories will always end with a '/', files never will. | 157 Note that directories will always end with a '/', files never will. |
| 157 | 158 |
| 158 If |root| cannot be found, raises a FileNotFoundError. | 159 If |root| cannot be found, raises a FileNotFoundError. |
| 159 For any other failure, raises a FileSystemError. | 160 For any other failure, raises a FileSystemError. |
| 160 ''' | 161 ''' |
| 161 AssertIsDirectory(root) | 162 AssertIsDirectory(root) |
| 162 basepath = root | 163 basepath = root |
| 163 | 164 |
| 164 def walk(root): | 165 def walk(root, depth): |
| 166 if depth == 0: | |
| 167 return | |
| 165 AssertIsDirectory(root) | 168 AssertIsDirectory(root) |
| 166 dirs, files = [], [] | 169 dirs, files = [], [] |
| 167 | 170 |
| 168 for f in self.ReadSingle(root).Get(): | 171 for f in self.ReadSingle(root).Get(): |
| 169 if IsDirectory(f): | 172 if IsDirectory(f): |
| 170 dirs.append(f) | 173 dirs.append(f) |
| 171 else: | 174 else: |
| 172 files.append(f) | 175 files.append(f) |
| 173 | 176 |
| 174 yield root[len(basepath):].rstrip('/'), dirs, files | 177 yield root[len(basepath):].rstrip('/'), dirs, files |
| 175 | 178 |
| 176 for d in dirs: | 179 for d in dirs: |
| 177 for walkinfo in walk(root + d): | 180 for walkinfo in walk(root + d, depth - 1): |
| 178 yield walkinfo | 181 yield walkinfo |
| 179 | 182 |
| 180 for walkinfo in walk(root): | 183 for walkinfo in walk(root, depth): |
| 181 yield walkinfo | 184 yield walkinfo |
| 182 | 185 |
| 183 def __eq__(self, other): | 186 def __eq__(self, other): |
| 184 return (isinstance(other, FileSystem) and | 187 return (isinstance(other, FileSystem) and |
| 185 self.GetIdentity() == other.GetIdentity()) | 188 self.GetIdentity() == other.GetIdentity()) |
| 186 | 189 |
| 187 def __ne__(self, other): | 190 def __ne__(self, other): |
| 188 return not (self == other) | 191 return not (self == other) |
| 189 | 192 |
| 190 def __repr__(self): | 193 def __repr__(self): |
| 191 return '<%s>' % type(self).__name__ | 194 return '<%s>' % type(self).__name__ |
| 192 | 195 |
| 193 def __str__(self): | 196 def __str__(self): |
| 194 return repr(self) | 197 return repr(self) |
| OLD | NEW |