• 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
7import six
8
9class LocalPathInfo(object):
10
11  def __init__(self, path_priority_groups):
12    """Container for a set of local file paths where a given dependency
13    can be stored.
14
15    Organized as a list of groups, where each group is itself a file path list.
16    See GetLocalPath() to understand how they are used.
17
18    Args:
19      path_priority_groups: Can be either None, or a list of file path
20        strings (corresponding to a list of groups, where each group has
21        a single file path), or a list of a list of file path strings
22        (i.e. a list of groups).
23    """
24    self._path_priority_groups = self._ParseLocalPaths(path_priority_groups)
25
26  def GetLocalPath(self):
27    """Look for a local file, and return its path.
28
29    Looks for the first group which has at least one existing file path. Then
30    returns the most-recent of these files.
31
32    Returns:
33      Local file path, if found, or None otherwise.
34    """
35    for priority_group in self._path_priority_groups:
36      priority_group = [g for g in priority_group if os.path.exists(g)]
37      if not priority_group:
38        continue
39      return max(priority_group, key=lambda path: os.stat(path).st_mtime)
40    return None
41
42  def IsPathInLocalPaths(self, path):
43    """Returns true if |path| is in one of this instance's file path lists."""
44    return any(
45        path in priority_group for priority_group in self._path_priority_groups)
46
47  def Update(self, local_path_info):
48    """Update this object from the content of another LocalPathInfo instance.
49
50    Any file path from |local_path_info| that is not already contained in the
51    current instance will be added into new groups to it.
52
53    Args:
54      local_path_info: Another LocalPathInfo instance, or None.
55    """
56    if not local_path_info:
57      return
58    for priority_group in local_path_info._path_priority_groups:
59      group_list = []
60      for path in priority_group:
61        if not self.IsPathInLocalPaths(path):
62          group_list.append(path)
63      if group_list:
64        self._path_priority_groups.append(group_list)
65
66  @staticmethod
67  def _ParseLocalPaths(local_paths):
68    if not local_paths:
69      return []
70    return [[e] if isinstance(e, six.string_types) else e for e in local_paths]
71