|
48 | 48 | from packagedcode.npm import NpmPackageJsonHandler |
49 | 49 | from rust_inspector.binary import collect_and_parse_rust_symbols |
50 | 50 | from summarycode.classify import LEGAL_STARTS_ENDS |
| 51 | +from source_inspector.symbols_tree_sitter import get_tree |
51 | 52 |
|
52 | 53 | from aboutcode.pipeline import LoopProgress |
53 | 54 | from scanpipe import pipes |
@@ -1945,7 +1946,7 @@ def map_elfs_binaries_with_symbols(project, logger=None): |
1945 | 1946 | ) |
1946 | 1947 |
|
1947 | 1948 | # Collect source symbols from elf related source files |
1948 | | - elf_from_resources = from_resources.filter(extension__in=[".c", ".cpp", ".h"]) |
| 1949 | + elf_from_resources = from_resources.filter(extension__in=[".c", ".cpp", ".h", ".pyx", ".pxd"]) |
1949 | 1950 |
|
1950 | 1951 | map_binaries_with_symbols( |
1951 | 1952 | project=project, |
@@ -2146,3 +2147,39 @@ def _map_javascript_symbols(to_resource, javascript_from_resources, logger): |
2146 | 2147 | to_resource.update(status=flag.MAPPED) |
2147 | 2148 | return 1 |
2148 | 2149 | return 0 |
| 2150 | + |
| 2151 | + |
| 2152 | +def map_python_pyx_to_binaries(project, logger=None): |
| 2153 | + """Map ELF binaries to their sources in ``project``.""" |
| 2154 | + from_resources = project.codebaseresources.files().from_codebase().filter(extension__endswith=".pyx") |
| 2155 | + to_resources = ( |
| 2156 | + project.codebaseresources.files().to_codebase().has_no_relation().elfs() |
| 2157 | + ) |
| 2158 | + |
| 2159 | + # Collect binary symbols from binaries |
| 2160 | + for resource in to_resources: |
| 2161 | + try: |
| 2162 | + binary_symbols = collect_and_parse_elf_symbols(resource.location) |
| 2163 | + resource.update_extra_data(binary_symbols) |
| 2164 | + except Exception as e: |
| 2165 | + logger(f"Error parsing binary symbols at: {resource.location_path!r} {e!r}") |
| 2166 | + |
| 2167 | + for resource in from_resources: |
| 2168 | + tree, _ = get_tree(resource.location) |
| 2169 | + function_definitions = [node for node in tree.root_node.children if node.type == "function_definition"] |
| 2170 | + identifiers = [] |
| 2171 | + for node in function_definitions: |
| 2172 | + for child in node.children: |
| 2173 | + if child.type == "identifier": |
| 2174 | + identifiers.append(child.text.decode()) |
| 2175 | + |
| 2176 | + identifiers_qs = Q() |
| 2177 | + for identifier in identifiers: |
| 2178 | + identifiers_qs |= Q(extra_data__icontains=identifier) |
| 2179 | + matching_elfs = to_resources.filter(identifiers_qs) |
| 2180 | + for matching_elf in matching_elfs: |
| 2181 | + pipes.make_relation( |
| 2182 | + from_resource=resource, |
| 2183 | + to_resource=matching_elf, |
| 2184 | + map_type="python_pyx_match", |
| 2185 | + ) |
0 commit comments