• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6
7
8class LocalPathInfo(object):
9
10  def __init__(self, path_priority_groups):
11    self._path_priority_groups = self._ParseLocalPaths(path_priority_groups)
12
13  def GetLocalPath(self):
14    for priority_group in self._path_priority_groups:
15      priority_group = filter(os.path.exists, priority_group)
16      if not priority_group:
17        continue
18      return max(priority_group, key=lambda path: os.stat(path).st_mtime)
19    return None
20
21  def IsPathInLocalPaths(self, path):
22    return any(
23        path in priority_group for priority_group in self._path_priority_groups)
24
25  def Update(self, local_path_info):
26    if not local_path_info:
27      return
28    for priority_group in local_path_info._path_priority_groups:
29      group_list = []
30      for path in priority_group:
31        if not self.IsPathInLocalPaths(path):
32          group_list.append(path)
33      if group_list:
34        self._path_priority_groups.append(group_list)
35
36  @staticmethod
37  def _ParseLocalPaths(local_paths):
38    if not local_paths:
39      return []
40    return [[e] if isinstance(e, basestring) else e for e in local_paths]
41