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 os | 5 import os |
6 | 6 |
7 class FileNotFoundError(Exception): | 7 class FileNotFoundError(Exception): |
8 def __init__(self, filename): | 8 def __init__(self, filename): |
9 Exception.__init__(self, filename) | 9 Exception.__init__(self, filename) |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 ''' | 44 ''' |
45 def Read(self, paths, binary=False): | 45 def Read(self, paths, binary=False): |
46 '''Reads each file in paths and returns a dictionary mapping the path to the | 46 '''Reads each file in paths and returns a dictionary mapping the path to the |
47 contents. If a path in paths ends with a '/', it is assumed to be a | 47 contents. If a path in paths ends with a '/', it is assumed to be a |
48 directory, and a list of files in the directory is mapped to the path. | 48 directory, and a list of files in the directory is mapped to the path. |
49 | 49 |
50 If binary=False, the contents of each file will be unicode parsed as utf-8, | 50 If binary=False, the contents of each file will be unicode parsed as utf-8, |
51 and failing that as latin-1 (some extension docs use latin-1). If | 51 and failing that as latin-1 (some extension docs use latin-1). If |
52 binary=True then the contents will be a str. | 52 binary=True then the contents will be a str. |
53 ''' | 53 ''' |
54 raise NotImplementedError() | 54 raise NotImplementedError(self.__class__) |
55 | 55 |
56 def ReadSingle(self, path, binary=False): | 56 def ReadSingle(self, path, binary=False): |
57 '''Reads a single file from the FileSystem. | 57 '''Reads a single file from the FileSystem. |
58 ''' | 58 ''' |
59 return self.Read([path], binary=binary).Get()[path] | 59 return self.Read([path], binary=binary).Get()[path] |
60 | 60 |
61 # TODO(cduvall): Allow Stat to take a list of paths like Read. | 61 # TODO(cduvall): Allow Stat to take a list of paths like Read. |
62 def Stat(self, path): | 62 def Stat(self, path): |
63 '''Returns a |StatInfo| object containing the version of |path|. If |path| | 63 '''Returns a |StatInfo| object containing the version of |path|. If |path| |
64 is a directory, |StatInfo| will have the versions of all the children of | 64 is a directory, |StatInfo| will have the versions of all the children of |
65 the directory in |StatInfo.child_versions|. | 65 the directory in |StatInfo.child_versions|. |
66 ''' | 66 ''' |
67 raise NotImplementedError() | 67 raise NotImplementedError(self.__class__) |
68 | 68 |
69 def GetIdentity(self): | 69 def GetIdentity(self): |
70 '''The identity of the file system, exposed for caching classes to | 70 '''The identity of the file system, exposed for caching classes to |
71 namespace their caches. this will usually depend on the configuration of | 71 namespace their caches. this will usually depend on the configuration of |
72 that file system - e.g. a LocalFileSystem with a base path of /var is | 72 that file system - e.g. a LocalFileSystem with a base path of /var is |
73 different to that of a SubversionFileSystem with a base path of /bar, is | 73 different to that of a SubversionFileSystem with a base path of /bar, is |
74 different to a LocalFileSystem with a base path of /usr. | 74 different to a LocalFileSystem with a base path of /usr. |
75 ''' | 75 ''' |
76 raise NotImplementedError() | 76 raise NotImplementedError(self.__class__) |
77 | 77 |
78 def Walk(self, root): | 78 def Walk(self, root): |
79 '''Recursively walk the directories in a file system, starting with root. | 79 '''Recursively walk the directories in a file system, starting with root. |
80 Emulates os.walk from the standard os module. | 80 Emulates os.walk from the standard os module. |
81 ''' | 81 ''' |
82 if not root.endswith('/'): | 82 if not root.endswith('/'): |
83 root += '/' | 83 root += '/' |
84 | 84 |
85 dirs, files = [], [] | 85 dirs, files = [], [] |
86 | 86 |
87 for f in self.ReadSingle(root): | 87 for f in self.ReadSingle(root): |
88 if f.endswith('/'): | 88 if f.endswith('/'): |
89 dirs.append(f) | 89 dirs.append(f) |
90 else: | 90 else: |
91 files.append(f) | 91 files.append(f) |
92 | 92 |
93 yield (root.rstrip('/'), dirs, files) | 93 yield (root.rstrip('/'), dirs, files) |
94 | 94 |
95 for d in dirs: | 95 for d in dirs: |
96 for walkinfo in self.Walk(root + d): | 96 for walkinfo in self.Walk(root + d): |
97 yield walkinfo | 97 yield walkinfo |
OLD | NEW |