Skip to content

Commit 506cf1d

Browse files
committed
Compute line_by_pos for resources with matches
Signed-off-by: Jono Yang <[email protected]>
1 parent b7d7d21 commit 506cf1d

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

scanpipe/pipes/matchcode.py

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import requests
2929
from matchcode_toolkit.fingerprinting import compute_codebase_directory_fingerprints
3030
from matchcode_toolkit.fingerprinting import get_file_fingerprint_hashes
31+
from matchcode_toolkit.fingerprinting import get_line_by_pos
3132
from scancode import Scanner
3233

3334
from scanpipe.pipes import codebase
@@ -367,4 +368,9 @@ def create_packages_from_match_results(project, match_results):
367368
match_resource_extra_data = match_resource["extra_data"]
368369
if match_resource_extra_data:
369370
resource = project.codebaseresources.get(path=match_resource["path"])
371+
# compute line_by_pos for displaying matches in CodebaseResource detail view
372+
with open(resource.location) as f:
373+
content = f.read()
374+
line_by_pos = get_line_by_pos(content)
375+
match_resource_extra_data["line_by_pos"] = line_by_pos
370376
resource.update_extra_data(match_resource_extra_data)

scanpipe/views.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@
6161
import saneyaml
6262
import xlsxwriter
6363
from django_filters.views import FilterView
64-
from packageurl.contrib.django.models import PACKAGE_URL_FIELDS
6564
from licensedcode.spans import Span
66-
from matchcode_toolkit.fingerprinting import get_line_by_pos
65+
from packageurl.contrib.django.models import PACKAGE_URL_FIELDS
6766

6867
from scancodeio.auth import ConditionalLoginRequired
6968
from scancodeio.auth import conditional_login_required
@@ -1937,24 +1936,21 @@ def get_context_data(self, **kwargs):
19371936
matched_snippet_annotations = []
19381937
matched_snippets = resource.extra_data.get("matched_snippets")
19391938
if matched_snippets:
1940-
# tokenize file content and map tokens to line numbers
1941-
line_by_pos = get_line_by_pos(resource.file_content)
1942-
last_pos = max(line_by_pos, key=line_by_pos.get)
1939+
line_by_pos = resource.extra_data.get("line_by_pos")
19431940
for matched_snippet in matched_snippets:
19441941
qspan = Span(matched_snippet["qspan"])
1945-
matched_snippet_annotations.append
19461942
matched_snippet["qspan"] = qspan
19471943
for span in qspan.subspans():
1948-
# Convert qstart and qends to start_line and end_lines
1949-
end = min(span.end, last_pos)
1944+
# line_by_pos is stored as JSON and keys in JSON are always
1945+
# strings
19501946
matched_snippet_annotations.append(
19511947
{
1952-
"start_line": line_by_pos[span.start],
1953-
"end_line": line_by_pos[end],
1948+
"start_line": line_by_pos[str(span.start)],
1949+
"end_line": line_by_pos[str(span.end)],
19541950
}
19551951
)
1956-
19571952
context["detected_values"]["matched_snippets"] = matched_snippet_annotations
1953+
19581954
return context
19591955

19601956

0 commit comments

Comments
 (0)