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