From faba7cfe1d208b1b5807f0dc380609bbdae242d2 Mon Sep 17 00:00:00 2001 From: Thomas Druez Date: Tue, 11 Apr 2023 14:28:49 +0400 Subject: [PATCH] Do not run extra matching if existing CodebaseRelation #659 Signed-off-by: Thomas Druez --- scanpipe/models.py | 4 ++++ scanpipe/pipes/d2d.py | 27 ++++++++++++++------------- scanpipe/views.py | 16 ++++++++-------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/scanpipe/models.py b/scanpipe/models.py index 9fd01bd8c..196c868b1 100644 --- a/scanpipe/models.py +++ b/scanpipe/models.py @@ -1410,6 +1410,10 @@ def to_codebase(self): """Resources in to/ directory""" return self.filter(path__startswith="to/") + def has_no_relation(self): + """Resources not part of any CodebaseRelation""" + return self.filter(related_from__isnull=True, related_to__isnull=True) + def missing_in_to(self): """Resources in from/ not found in to/""" return self.from_codebase().filter(related_to__isnull=True) diff --git a/scanpipe/pipes/d2d.py b/scanpipe/pipes/d2d.py index 4bd9e6d71..05d92bece 100644 --- a/scanpipe/pipes/d2d.py +++ b/scanpipe/pipes/d2d.py @@ -31,7 +31,7 @@ def checksum_match(project, checksum_field): """Match using checksum.""" - project_files = project.codebaseresources.files().not_empty() + project_files = project.codebaseresources.files().has_no_relation().not_empty() from_resources = project_files.from_codebase().has_value(checksum_field) to_resources = project_files.to_codebase().has_value(checksum_field) @@ -50,15 +50,16 @@ def checksum_match(project, checksum_field): def java_to_class_match(project): """Match a .java source to its compiled .class""" - extension = ".java" + from_extension = ".java" + to_extension = ".class" - project_files = project.codebaseresources.files() + project_files = project.codebaseresources.files().has_no_relation() from_resources = project_files.from_codebase() to_resources = project_files.to_codebase() - for resource in from_resources.filter(extension=extension): - parts = resource.path[len(FROM) : -len(extension)] - matches = to_resources.filter(path=f"{TO}{parts}.class") + for resource in from_resources.filter(extension=from_extension): + parts = resource.path[len(FROM) : -len(from_extension)] + matches = to_resources.filter(path=f"{TO}{parts}{to_extension}") for match in matches: pipes.make_relationship( from_resource=resource, @@ -70,17 +71,18 @@ def java_to_class_match(project): def java_to_inner_class_match(project): """Match a .java source to compiled $.class""" - extension = ".class" + from_extension = ".java" + to_extension = ".class" - project_files = project.codebaseresources.files() + project_files = project.codebaseresources.files().has_no_relation() from_resources = project_files.from_codebase() to_resources = project_files.to_codebase() - inner_classes = to_resources.filter(name__contains="$", extension=extension) + inner_classes = to_resources.filter(name__contains="$", extension=to_extension) for to_resource in inner_classes: - parts = to_resource.path[len(TO) : -len(extension)] + parts = to_resource.path[len(TO) : -len(to_extension)] source_java = "/".join(parts.split("/")[:-1] + to_resource.name.split("$")[:1]) - matches = from_resources.filter(path=f"{FROM}{source_java}.java") + matches = from_resources.filter(path=f"{FROM}{source_java}{from_extension}") for match in matches: pipes.make_relationship( from_resource=match, @@ -92,8 +94,7 @@ def java_to_inner_class_match(project): def path_match(project): """Match using path similarities.""" - project_files = project.codebaseresources.files() - + project_files = project.codebaseresources.files().has_no_relation() from_resources = project_files.from_codebase() to_resources = project_files.to_codebase() diff --git a/scanpipe/views.py b/scanpipe/views.py index 473fd9a41..fb05cbaff 100644 --- a/scanpipe/views.py +++ b/scanpipe/views.py @@ -1029,14 +1029,14 @@ def codebase_resource_diff_view(request, uuid): project = get_object_or_404(Project, uuid=uuid) project_files = project.codebaseresources.files() - pk_a = request.GET.get("pk_a") - pk_b = request.GET.get("pk_b") - resource_a = get_object_or_404(project_files, path=pk_a) - resource_b = get_object_or_404(project_files, path=pk_b) - - text_a = resource_a.location_path.read_text() - text_b = resource_b.location_path.read_text() - html = difflib.HtmlDiff().make_file(text_a.split("\n"), text_b.split("\n")) + from_pk = request.GET.get("pk_a") + to_pk = request.GET.get("pk_b") + from_resource = get_object_or_404(project_files, path=from_pk) + to_resource = get_object_or_404(project_files, path=to_pk) + + from_lines = from_resource.location_path.read_text().split("\n") + to_lines = to_resource.location_path.read_text().split("\n") + html = difflib.HtmlDiff().make_file(from_lines, to_lines) return HttpResponse(html)