| 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 FileSystemCache(object): | 7 class FileSystemCache(object): |
| 8 """This class caches FileSystem data that has been processed. | 8 """This class caches FileSystem data that has been processed. |
| 9 """ | 9 """ |
| 10 class Builder(object): | 10 class Builder(object): |
| 11 """A class to build a FileSystemCache. | 11 """A class to build a FileSystemCache. |
| 12 """ | 12 """ |
| 13 def __init__(self, file_system): | 13 def __init__(self, file_system): |
| 14 self._file_system = file_system | 14 self._file_system = file_system |
| 15 | 15 |
| 16 def build(self, populate_function): | 16 def build(self, populate_function): |
| 17 return FileSystemCache(self._file_system, populate_function) | 17 return FileSystemCache(self._file_system, populate_function) |
| 18 | 18 |
| 19 class _CacheEntry(object): | 19 class _CacheEntry(object): |
| 20 def __init__(self, cache_data, version): | 20 def __init__(self, cache_data, version): |
| 21 self._cache_data = cache_data | 21 self._cache_data = cache_data |
| 22 self.version = version | 22 self.version = version |
| 23 | 23 |
| 24 def __init__(self, file_system, populate_function): | 24 def __init__(self, file_system, populate_function): |
| 25 self._file_system = file_system | 25 self._file_system = file_system |
| 26 self._populate_function = populate_function | 26 self._populate_function = populate_function |
| 27 self._cache = {} | 27 self._cache = {} |
| 28 | 28 |
| 29 def _FetchFile(self, filename): | |
| 30 return self._file_system.Read([filename]).Get()[filename] | |
| 31 | |
| 32 def _RecursiveList(self, files): | 29 def _RecursiveList(self, files): |
| 33 all_files = files[:] | 30 all_files = files[:] |
| 34 dirs = {} | 31 dirs = {} |
| 35 for filename in files: | 32 for filename in files: |
| 36 if filename.endswith('/'): | 33 if filename.endswith('/'): |
| 37 all_files.remove(filename) | 34 all_files.remove(filename) |
| 38 dirs.update(self._file_system.Read([filename]).Get()) | 35 dirs.update(self._file_system.Read([filename]).Get()) |
| 39 for dir_, files in dirs.iteritems(): | 36 for dir_, files in dirs.iteritems(): |
| 40 all_files.extend(self._RecursiveList([dir_ + f for f in files])) | 37 all_files.extend(self._RecursiveList([dir_ + f for f in files])) |
| 41 return all_files | 38 return all_files |
| 42 | 39 |
| 43 def GetFromFile(self, path): | 40 def GetFromFile(self, path): |
| 44 """Calls |populate_function| on the contents of the file at |path|. | 41 """Calls |populate_function| on the contents of the file at |path|. |
| 45 """ | 42 """ |
| 46 version = self._file_system.Stat(path).version | 43 version = self._file_system.Stat(path).version |
| 47 if path in self._cache: | 44 if path in self._cache: |
| 48 if version > self._cache[path].version: | 45 if version > self._cache[path].version: |
| 49 self._cache.pop(path) | 46 self._cache.pop(path) |
| 50 else: | 47 else: |
| 51 return self._cache[path]._cache_data | 48 return self._cache[path]._cache_data |
| 52 cache_data = self._FetchFile(path) | 49 cache_data = self._file_system.ReadSingle(path) |
| 53 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 50 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), |
| 54 version) | 51 version) |
| 55 return self._cache[path]._cache_data | 52 return self._cache[path]._cache_data |
| 56 | 53 |
| 57 def GetFromFileListing(self, path): | 54 def GetFromFileListing(self, path): |
| 58 """Calls |populate_function| on the listing of the files at |path|. | 55 """Calls |populate_function| on the listing of the files at |path|. |
| 59 Assumes that the path given is to a directory. | 56 Assumes that the path given is to a directory. |
| 60 """ | 57 """ |
| 61 if not path.endswith('/'): | 58 if not path.endswith('/'): |
| 62 path += '/' | 59 path += '/' |
| 63 version = self._file_system.Stat(path).version | 60 version = self._file_system.Stat(path).version |
| 64 if path in self._cache: | 61 if path in self._cache: |
| 65 if version > self._cache[path].version: | 62 if version > self._cache[path].version: |
| 66 self._cache.pop(path) | 63 self._cache.pop(path) |
| 67 else: | 64 else: |
| 68 return self._cache[path]._cache_data | 65 return self._cache[path]._cache_data |
| 69 cache_data = self._RecursiveList([path + f for f in self._FetchFile(path)]) | 66 cache_data = self._RecursiveList( |
| 67 [path + f for f in self._file_system.ReadSingle(path)]) |
| 70 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 68 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), |
| 71 version) | 69 version) |
| 72 return self._cache[path]._cache_data | 70 return self._cache[path]._cache_data |
| OLD | NEW |