@@ -1741,7 +1741,7 @@ def process_paths_in_binary(
1741
1741
matched_from_resources = sort_matched_from_resources (matched_from_resources )
1742
1742
winning_from_resource = matched_from_resources [0 ]
1743
1743
1744
- path_length = len (path . strip ( "/" ). split ( "/" ) ) - 1
1744
+ path_length = count_path_segments (path ) - 1
1745
1745
extra_data = {
1746
1746
"path_score" : f"{ matched_path_length } /{ path_length } " ,
1747
1747
map_type : path ,
@@ -1758,15 +1758,21 @@ def process_paths_in_binary(
1758
1758
yield rel_key , relation
1759
1759
1760
1760
1761
+ def count_path_segments (path ):
1762
+ """Return the number of path segments in POSIX ``path`` string"""
1763
+ return len (path .strip ("/" ).split ("/" ))
1764
+
1765
+
1761
1766
def sort_matched_from_resources (matched_from_resources ):
1762
1767
"""
1763
1768
Return the sorted list of ``matched_from_resources``
1764
1769
based on path length and path.
1765
1770
"""
1766
- return sorted (
1767
- matched_from_resources ,
1768
- key = lambda res : (len (res .path .strip ("/" ).split ("/" )), res .path ),
1769
- )
1771
+
1772
+ def sorter (res ):
1773
+ return count_path_segments (res .path ), res .path
1774
+
1775
+ return sorted (matched_from_resources , key = sorter )
1770
1776
1771
1777
1772
1778
def is_invalid_match (match , matched_path_length ):
@@ -1777,45 +1783,6 @@ def is_invalid_match(match, matched_path_length):
1777
1783
return matched_path_length == 1 and len (match .resource_ids ) != 1
1778
1784
1779
1785
1780
- def map_paths (project , file_type , collect_paths_func , map_types , logger = None ):
1781
- """Map paths using similarities of path suffixes."""
1782
- from_resources = project .codebaseresources .files ().from_codebase ()
1783
- to_resources = project .codebaseresources .files ().to_codebase ().has_no_relation ()
1784
- to_resources = getattr (to_resources , file_type )()
1785
- resource_count = 0
1786
- for resource in to_resources :
1787
- try :
1788
- paths = collect_paths_func (resource .location_path )
1789
- resource .update_extra_data (paths )
1790
- resource_count += 1
1791
- except Exception as e :
1792
- logger (f"Can not parse { resource .location_path !r} { e !r} " )
1793
-
1794
- if logger :
1795
- logger (
1796
- f"Mapping { resource_count :,d} to/ resources using paths "
1797
- f"with { from_resources .count ():,d} from/ resources."
1798
- )
1799
-
1800
- from_resources_index = pathmap .build_index (
1801
- from_resources .values_list ("id" , "path" ), with_subpaths = True
1802
- )
1803
-
1804
- if logger :
1805
- logger ("Done building from/ resources index." )
1806
-
1807
- resource_iterator = to_resources .iterator (chunk_size = 2000 )
1808
- progress = LoopProgress (resource_count , logger )
1809
- for to_resource in progress .iter (resource_iterator ):
1810
- map_paths_resource (
1811
- to_resource ,
1812
- from_resources ,
1813
- from_resources_index ,
1814
- map_types = map_types ,
1815
- logger = logger ,
1816
- )
1817
-
1818
-
1819
1786
def map_elfs (project , logger = None ):
1820
1787
"""Map ELF binaries to their sources in ``project``."""
1821
1788
from_resources = project .codebaseresources .files ().from_codebase ()
0 commit comments