|
46 | 46 | from packagedcode.npm import NpmPackageJsonHandler |
47 | 47 | from rust_inspector.binary import collect_and_parse_rust_symbols |
48 | 48 | from summarycode.classify import LEGAL_STARTS_ENDS |
| 49 | +from source_inspector.symbols_tree_sitter import get_tree |
49 | 50 |
|
50 | 51 | from aboutcode.pipeline import LoopProgress |
51 | 52 | from scanpipe import pipes |
@@ -1943,7 +1944,7 @@ def map_elfs_binaries_with_symbols(project, logger=None): |
1943 | 1944 | ) |
1944 | 1945 |
|
1945 | 1946 | # Collect source symbols from rust source files |
1946 | | - elf_from_resources = from_resources.filter(extension__in=[".c", ".cpp", ".h"]) |
| 1947 | + elf_from_resources = from_resources.filter(extension__in=[".c", ".cpp", ".h", ".pyx", ".pxd"]) |
1947 | 1948 |
|
1948 | 1949 | map_binaries_with_symbols( |
1949 | 1950 | project=project, |
@@ -2097,3 +2098,39 @@ def _map_javascript_symbols(to_resource, javascript_from_resources, logger): |
2097 | 2098 | to_resource.update(status=flag.MAPPED) |
2098 | 2099 | return 1 |
2099 | 2100 | return 0 |
| 2101 | + |
| 2102 | + |
| 2103 | +def map_python_pyx_to_binaries(project, logger=None): |
| 2104 | + """Map ELF binaries to their sources in ``project``.""" |
| 2105 | + from_resources = project.codebaseresources.files().from_codebase().filter(extension__endswith=".pyx") |
| 2106 | + to_resources = ( |
| 2107 | + project.codebaseresources.files().to_codebase().has_no_relation().elfs() |
| 2108 | + ) |
| 2109 | + |
| 2110 | + # Collect binary symbols from binaries |
| 2111 | + for resource in to_resources: |
| 2112 | + try: |
| 2113 | + binary_symbols = collect_and_parse_elf_symbols(resource.location) |
| 2114 | + resource.update_extra_data(binary_symbols) |
| 2115 | + except Exception as e: |
| 2116 | + logger(f"Error parsing binary symbols at: {resource.location_path!r} {e!r}") |
| 2117 | + |
| 2118 | + for resource in from_resources: |
| 2119 | + tree, _ = get_tree(resource.location) |
| 2120 | + function_definitions = [node for node in tree.root_node.children if node.type == "function_definition"] |
| 2121 | + identifiers = [] |
| 2122 | + for node in function_definitions: |
| 2123 | + for child in node.children: |
| 2124 | + if child.type == "identifier": |
| 2125 | + identifiers.append(child.text.decode()) |
| 2126 | + |
| 2127 | + identifiers_qs = Q() |
| 2128 | + for identifier in identifiers: |
| 2129 | + identifiers_qs |= Q(extra_data__icontains=identifier) |
| 2130 | + matching_elfs = to_resources.filter(identifiers_qs) |
| 2131 | + for matching_elf in matching_elfs: |
| 2132 | + pipes.make_relation( |
| 2133 | + from_resource=resource, |
| 2134 | + to_resource=matching_elf, |
| 2135 | + map_type="python_pyx_match", |
| 2136 | + ) |
0 commit comments