Skip to content

Commit dbb0fec

Browse files
committed
Use named function for clarity
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent fbabd00 commit dbb0fec

File tree

1 file changed

+11
-44
lines changed

1 file changed

+11
-44
lines changed

scanpipe/pipes/d2d.py

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ def process_paths_in_binary(
17411741
matched_from_resources = sort_matched_from_resources(matched_from_resources)
17421742
winning_from_resource = matched_from_resources[0]
17431743

1744-
path_length = len(path.strip("/").split("/")) - 1
1744+
path_length = count_path_segments(path) - 1
17451745
extra_data = {
17461746
"path_score": f"{matched_path_length}/{path_length}",
17471747
map_type: path,
@@ -1758,15 +1758,21 @@ def process_paths_in_binary(
17581758
yield rel_key, relation
17591759

17601760

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+
17611766
def sort_matched_from_resources(matched_from_resources):
17621767
"""
17631768
Return the sorted list of ``matched_from_resources``
17641769
based on path length and path.
17651770
"""
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)
17701776

17711777

17721778
def is_invalid_match(match, matched_path_length):
@@ -1777,45 +1783,6 @@ def is_invalid_match(match, matched_path_length):
17771783
return matched_path_length == 1 and len(match.resource_ids) != 1
17781784

17791785

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-
18191786
def map_elfs(project, logger=None):
18201787
"""Map ELF binaries to their sources in ``project``."""
18211788
from_resources = project.codebaseresources.files().from_codebase()

0 commit comments

Comments
 (0)