1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# found in the LICENSE file.
4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class HostFileSystemIterator(object):
7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  '''Provides methods for iterating through host file systems, in both
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ascending (oldest to newest version) and descending order.
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  '''
10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  def __init__(self, file_system_provider, branch_utility):
124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    self._file_system_provider = file_system_provider
13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    self._branch_utility = branch_utility
14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  def _ForEach(self, channel_info, callback, get_next):
16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    '''Iterates through a sequence of file systems defined by |get_next| until
17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    |callback| returns False, or until the end of the sequence of file systems
18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    is reached. Returns the BranchUtility.ChannelInfo of the last file system
19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    for which |callback| returned True.
20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    '''
21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    last_true = None
22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    while channel_info is not None:
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      if channel_info.branch == 'master':
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        file_system = self._file_system_provider.GetMaster()
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      else:
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        file_system = self._file_system_provider.GetBranch(channel_info.branch)
27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if not callback(file_system, channel_info):
28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        return last_true
29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      last_true = channel_info
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      channel_info = get_next(channel_info)
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return last_true
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  def Ascending(self, channel_info, callback):
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return self._ForEach(channel_info, callback, self._branch_utility.Newer)
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  def Descending(self, channel_info, callback):
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return self._ForEach(channel_info, callback, self._branch_utility.Older)
38