| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 from third_party.json_schema_compiler.memoize import memoize | 5 from third_party.json_schema_compiler.memoize import memoize |
| 6 | 6 |
| 7 | 7 |
| 8 class HostFileSystemIterator(object): | 8 class HostFileSystemIterator(object): |
| 9 '''Provides methods for iterating through host file systems, in both | 9 '''Provides methods for iterating through host file systems, in both |
| 10 ascending (oldest to newest version) and descending order. | 10 ascending (oldest to newest version) and descending order. |
| 11 ''' | 11 ''' |
| 12 | 12 |
| 13 def __init__(self, file_system_creator, host_file_system, branch_utility): | 13 def __init__(self, file_system_provider, branch_utility): |
| 14 self._file_system_creator = file_system_creator | 14 self._file_system_provider = file_system_provider |
| 15 self._host_file_system = host_file_system | |
| 16 self._branch_utility = branch_utility | 15 self._branch_utility = branch_utility |
| 17 | 16 |
| 18 @memoize | 17 @memoize |
| 19 def _GetFileSystem(self, branch): | 18 def _GetFileSystem(self, branch): |
| 20 '''To avoid overwriting the persistent data store entry for the 'trunk' | 19 '''To avoid overwriting the persistent data store entry for the 'trunk' |
| 21 host file system, hold on to a reference of this file system and return it | 20 host file system, hold on to a reference of this file system and return it |
| 22 instead of creating a file system for 'trunk'. | 21 instead of creating a file system for 'trunk'. |
| 23 Also of note: File systems are going to be iterated over multiple times | 22 Also of note: File systems are going to be iterated over multiple times |
| 24 at each call of ForEach, but the data isn't going to change between calls. | 23 at each call of ForEach, but the data isn't going to change between calls. |
| 25 Use |branch| to memoize the created file systems. | 24 Use |branch| to memoize the created file systems. |
| 26 ''' | 25 ''' |
| 27 if branch == 'trunk': | 26 return (self._file_system_provider.GetTrunk() if branch == 'trunk' else |
| 28 # Don't create a new file system for trunk, since there is a bug with the | 27 self._file_system_provider.GetBranch(branch)) |
| 29 # current architecture and design of HostFileSystemCreator, where | |
| 30 # creating 'trunk' ignores the pinned revision (in fact, it bypasses | |
| 31 # every difference including whether the file system is patched). | |
| 32 # TODO(kalman): Fix HostFileSystemCreator and update this comment. | |
| 33 return self._host_file_system | |
| 34 return self._file_system_creator.Create(branch) | |
| 35 | 28 |
| 36 def _ForEach(self, channel_info, callback, get_next): | 29 def _ForEach(self, channel_info, callback, get_next): |
| 37 '''Iterates through a sequence of file systems defined by |get_next| until | 30 '''Iterates through a sequence of file systems defined by |get_next| until |
| 38 |callback| returns False, or until the end of the sequence of file systems | 31 |callback| returns False, or until the end of the sequence of file systems |
| 39 is reached. Returns the BranchUtility.ChannelInfo of the last file system | 32 is reached. Returns the BranchUtility.ChannelInfo of the last file system |
| 40 for which |callback| returned True. | 33 for which |callback| returned True. |
| 41 ''' | 34 ''' |
| 42 last_true = None | 35 last_true = None |
| 43 while channel_info is not None: | 36 while channel_info is not None: |
| 44 file_system = self._GetFileSystem(channel_info.branch) | 37 file_system = self._GetFileSystem(channel_info.branch) |
| 45 if not callback(file_system, channel_info): | 38 if not callback(file_system, channel_info): |
| 46 return last_true | 39 return last_true |
| 47 last_true = channel_info | 40 last_true = channel_info |
| 48 channel_info = get_next(channel_info) | 41 channel_info = get_next(channel_info) |
| 49 return last_true | 42 return last_true |
| 50 | 43 |
| 51 def Ascending(self, channel_info, callback): | 44 def Ascending(self, channel_info, callback): |
| 52 return self._ForEach(channel_info, callback, self._branch_utility.Newer) | 45 return self._ForEach(channel_info, callback, self._branch_utility.Newer) |
| 53 | 46 |
| 54 def Descending(self, channel_info, callback): | 47 def Descending(self, channel_info, callback): |
| 55 return self._ForEach(channel_info, callback, self._branch_utility.Older) | 48 return self._ForEach(channel_info, callback, self._branch_utility.Older) |
| OLD | NEW |