Skip to content

Commit

Permalink
Do not run extra matching if existing CodebaseRelation #659
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <[email protected]>
  • Loading branch information
tdruez committed Apr 11, 2023
1 parent 8ce108b commit faba7cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
4 changes: 4 additions & 0 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 14 additions & 13 deletions scanpipe/pipes/d2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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()

Expand Down
16 changes: 8 additions & 8 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit faba7cf

Please sign in to comment.