| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 collections import defaultdict | 5 from collections import defaultdict |
| 6 | 6 |
| 7 | 7 |
| 8 def IsSameFilePath(path_1, path_2): | 8 def IsSameFilePath(path_1, path_2): |
| 9 """Determines if two paths represent same path. | 9 """Determines if two paths represent same path. |
| 10 | 10 |
| 11 First we split each path into a list of directories (via split('/')), | 11 First we split each path into a list of directories (via split('/')), |
| 12 then we treat those lists as multisets (i.e., ignore the order of | 12 then we treat those lists as multisets (i.e., ignore the order of |
| 13 directories, but keep track of their multiplicities) and take the | 13 directories, but keep track of their multiplicities) and take the |
| 14 multiset intersection. Finally, we return whether the number of elements | 14 multiset intersection. Finally, we return whether the number of elements |
| 15 in the intersection is at least 3 (or, when one of the paths has | 15 in the intersection is at least 3 (or, when one of the paths has |
| 16 fewer than 3 parts, we return whether all those parts are also in the | 16 fewer than 3 parts, we return whether all those parts are also in the |
| 17 other path) | 17 other path) |
| 18 | 18 |
| 19 Args: | 19 Args: |
| 20 path_1 (str): First path. | 20 path_1 (str): First path. |
| 21 path_2 (str): Second path to compare. | 21 path_2 (str): Second path to compare. |
| 22 | 22 |
| 23 Returns: | 23 Returns: |
| 24 Boolean, True if it they are thought to be a same path, False otherwise. | 24 Boolean, True if it they are thought to be a same path, False otherwise. |
| 25 """ | 25 """ |
| 26 if not path_1 and not path_2: |
| 27 return True |
| 28 |
| 29 if not path_1 or not path_2: |
| 30 return False |
| 31 |
| 26 # TODO(katesonia): Think of better way to determine whether 2 paths are the | 32 # TODO(katesonia): Think of better way to determine whether 2 paths are the |
| 27 # same or not. | 33 # same or not. |
| 28 path_parts_1 = path_1.lower().split('/') | 34 path_parts_1 = path_1.lower().split('/') |
| 29 path_parts_2 = path_2.lower().split('/') | 35 path_parts_2 = path_2.lower().split('/') |
| 30 | 36 |
| 31 if path_parts_1[-1] != path_parts_2[-1]: | 37 if path_parts_1[-1] != path_parts_2[-1]: |
| 32 return False | 38 return False |
| 33 | 39 |
| 34 def _GetPathPartsCount(path_parts): | 40 def _GetPathPartsCount(path_parts): |
| 35 path_parts_count = defaultdict(int) | 41 path_parts_count = defaultdict(int) |
| 36 | 42 |
| 37 for path_part in path_parts: | 43 for path_part in path_parts: |
| 38 path_parts_count[path_part] += 1 | 44 path_parts_count[path_part] += 1 |
| 39 | 45 |
| 40 return path_parts_count | 46 return path_parts_count |
| 41 | 47 |
| 42 parts_count_1 = _GetPathPartsCount(path_parts_1) | 48 parts_count_1 = _GetPathPartsCount(path_parts_1) |
| 43 parts_count_2 = _GetPathPartsCount(path_parts_2) | 49 parts_count_2 = _GetPathPartsCount(path_parts_2) |
| 44 | 50 |
| 45 # Get number of same path parts between path_1 and path_2. For example: | 51 # Get number of same path parts between path_1 and path_2. For example: |
| 46 # a/b/b/b/f.cc and a/b/b/c/d/f.cc have 4 path parts the same in total. | 52 # a/b/b/b/f.cc and a/b/b/c/d/f.cc have 4 path parts the same in total. |
| 47 total_same_parts = sum([min(parts_count_1[part], parts_count_2[part]) for | 53 total_same_parts = sum([min(parts_count_1[part], parts_count_2[part]) for |
| 48 part in parts_count_1 if part in path_parts_2]) | 54 part in parts_count_1 if part in path_parts_2]) |
| 49 | 55 |
| 50 return total_same_parts >= (min(3, min(len(path_parts_1), len(path_parts_2)))) | 56 return total_same_parts >= (min(3, min(len(path_parts_1), len(path_parts_2)))) |
| OLD | NEW |