| 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 | |
| 6 | |
| 7 | 5 |
| 8 class HostFileSystemIterator(object): | 6 class HostFileSystemIterator(object): |
| 9 '''Provides methods for iterating through host file systems, in both | 7 '''Provides methods for iterating through host file systems, in both |
| 10 ascending (oldest to newest version) and descending order. | 8 ascending (oldest to newest version) and descending order. |
| 11 ''' | 9 ''' |
| 12 | 10 |
| 13 def __init__(self, file_system_creator, host_file_system, branch_utility): | 11 def __init__(self, file_system_provider, branch_utility): |
| 14 self._file_system_creator = file_system_creator | 12 self._file_system_provider = file_system_provider |
| 15 self._host_file_system = host_file_system | |
| 16 self._branch_utility = branch_utility | 13 self._branch_utility = branch_utility |
| 17 | 14 |
| 18 @memoize | |
| 19 def _GetFileSystem(self, branch): | |
| 20 '''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 | |
| 22 instead of creating a file system for 'trunk'. | |
| 23 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. | |
| 25 Use |branch| to memoize the created file systems. | |
| 26 ''' | |
| 27 if branch == 'trunk': | |
| 28 # Don't create a new file system for trunk, since there is a bug with the | |
| 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 | |
| 36 def _ForEach(self, channel_info, callback, get_next): | 15 def _ForEach(self, channel_info, callback, get_next): |
| 37 '''Iterates through a sequence of file systems defined by |get_next| until | 16 '''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 | 17 |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 | 18 is reached. Returns the BranchUtility.ChannelInfo of the last file system |
| 40 for which |callback| returned True. | 19 for which |callback| returned True. |
| 41 ''' | 20 ''' |
| 42 last_true = None | 21 last_true = None |
| 43 while channel_info is not None: | 22 while channel_info is not None: |
| 44 file_system = self._GetFileSystem(channel_info.branch) | 23 if channel_info.branch == 'trunk': |
| 24 file_system = self._file_system_provider.GetTrunk() |
| 25 else: |
| 26 file_system = self._file_system_provider.GetBranch(channel_info.branch) |
| 45 if not callback(file_system, channel_info): | 27 if not callback(file_system, channel_info): |
| 46 return last_true | 28 return last_true |
| 47 last_true = channel_info | 29 last_true = channel_info |
| 48 channel_info = get_next(channel_info) | 30 channel_info = get_next(channel_info) |
| 49 return last_true | 31 return last_true |
| 50 | 32 |
| 51 def Ascending(self, channel_info, callback): | 33 def Ascending(self, channel_info, callback): |
| 52 return self._ForEach(channel_info, callback, self._branch_utility.Newer) | 34 return self._ForEach(channel_info, callback, self._branch_utility.Newer) |
| 53 | 35 |
| 54 def Descending(self, channel_info, callback): | 36 def Descending(self, channel_info, callback): |
| 55 return self._ForEach(channel_info, callback, self._branch_utility.Older) | 37 return self._ForEach(channel_info, callback, self._branch_utility.Older) |
| OLD | NEW |