Skip to content

Commit 9093f7f

Browse files
authored
Leverage the parent_path field when available #1691 (#1776)
* Leverage the parent_path field when available #1691 Signed-off-by: tdruez <[email protected]> * Fix failing tests #1691 Signed-off-by: tdruez <[email protected]> --------- Signed-off-by: tdruez <[email protected]>
1 parent 42be421 commit 9093f7f

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

scanpipe/models.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,7 +2864,7 @@ def __str__(self):
28642864

28652865
def save(self, *args, **kwargs):
28662866
if self.path and not self.parent_path:
2867-
self.parent_path = self.parent_directory() or ""
2867+
self.parent_path = self.compute_parent_directory() or ""
28682868
super().save(*args, **kwargs)
28692869

28702870
def get_absolute_url(self):
@@ -2935,17 +2935,21 @@ def get_path_segments_with_subpath(self):
29352935

29362936
return part_and_subpath
29372937

2938-
def parent_directory(self):
2938+
def compute_parent_directory(self):
29392939
"""Return the parent path for this CodebaseResource or None."""
2940-
parent_path = parent_directory(str(self.path), with_trail=False)
2941-
return parent_path or None
2940+
if parent_path := parent_directory(str(self.path), with_trail=False):
2941+
return parent_path
29422942

29432943
def has_parent(self):
29442944
"""
29452945
Return True if this CodebaseResource has a parent CodebaseResource or
29462946
False otherwise.
29472947
"""
2948-
parent_path = self.parent_directory()
2948+
if self.parent_path:
2949+
return True
2950+
2951+
# Support for resources created before the parent_path was stored as a field.
2952+
parent_path = self.compute_parent_directory()
29492953
if not parent_path:
29502954
return False
29512955
if self.project.codebaseresources.filter(path=parent_path).exists():
@@ -2960,7 +2964,13 @@ def parent(self, codebase=None):
29602964
`codebase` is not used in this context but required for compatibility
29612965
with the commoncode.resource.Codebase class API.
29622966
"""
2963-
parent_path = self.parent_directory()
2967+
if self.parent_path:
2968+
parent_path = self.parent_path
2969+
2970+
# Support for resources created before the parent_path was stored as a field.
2971+
else:
2972+
parent_path = self.compute_parent_directory()
2973+
29642974
return parent_path and self.project.codebaseresources.get(path=parent_path)
29652975

29662976
def siblings(self, codebase=None):

scanpipe/pipes/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def make_codebase_resource(project, location, save=True, **extra_fields):
7373

7474
relative_path = Path(location).relative_to(project.codebase_path)
7575
parent_path = str(relative_path.parent)
76-
7776
if parent_path == ".":
7877
parent_path = ""
7978

scanpipe/tests/pipes/test_scancode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def test_scanpipe_pipes_scancode_assemble_package_function(self):
502502
resource = project.codebaseresources.get(name="package.json")
503503

504504
# This assembly should not trigger that many queries.
505-
with self.assertNumQueries(18):
505+
with self.assertNumQueries(17):
506506
scancode.assemble_package(resource, project, processed_paths)
507507

508508
self.assertEqual(1, project.discoveredpackages.count())

scanpipe/tests/test_models.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,25 @@ def test_scanpipe_codebase_resource_model_compliance_alert_update_fields(self):
16381638
resource.refresh_from_db()
16391639
self.assertEqual("ok", resource.compliance_alert)
16401640

1641+
def test_scanpipe_codebase_resource_model_parent_path_set_during_save(self):
1642+
resource = self.project1.codebaseresources.create(path="")
1643+
self.assertEqual("", resource.parent_path)
1644+
1645+
resource = self.project1.codebaseresources.create(path=".")
1646+
self.assertEqual("", resource.parent_path)
1647+
1648+
resource = self.project1.codebaseresources.create(path="file")
1649+
self.assertEqual("", resource.parent_path)
1650+
1651+
resource = self.project1.codebaseresources.create(path="dir/")
1652+
self.assertEqual("", resource.parent_path)
1653+
1654+
resource = self.project1.codebaseresources.create(path="dir1/dir2/")
1655+
self.assertEqual("dir1", resource.parent_path)
1656+
1657+
resource = self.project1.codebaseresources.create(path="dir1/dir2/file")
1658+
self.assertEqual("dir1/dir2", resource.parent_path)
1659+
16411660
@patch.object(scanpipe_app, "policies", new=global_policies)
16421661
def test_scanpipe_can_compute_compliance_alert_for_license_exceptions(self):
16431662
scanpipe_app.license_policies_index = license_policies_index
@@ -1646,16 +1665,6 @@ def test_scanpipe_can_compute_compliance_alert_for_license_exceptions(self):
16461665
resource.update(detected_license_expression=license_expression)
16471666
self.assertEqual("warning", resource.compute_compliance_alert())
16481667

1649-
def test_scanpipe_codebase_root_parent_path(self):
1650-
resource1 = self.project1.codebaseresources.create(path="file")
1651-
1652-
self.assertEqual("", resource1.parent_path)
1653-
1654-
def test_scanpipe_codebase_regular_parent_path(self):
1655-
resource2 = self.project1.codebaseresources.create(path="dir1/dir2/file")
1656-
1657-
self.assertEqual("dir1/dir2", resource2.parent_path)
1658-
16591668
def test_scanpipe_scan_fields_model_mixin_methods(self):
16601669
expected = [
16611670
"detected_license_expression",
@@ -2136,7 +2145,9 @@ def test_scanpipe_codebase_resource_model_walk_method(self):
21362145
path="asgiref-3.3.0.whl-extract/asgiref/compatibility.py"
21372146
)
21382147
expected_parent_path = "asgiref-3.3.0.whl-extract/asgiref"
2139-
self.assertEqual(expected_parent_path, asgiref_resource.parent_directory())
2148+
self.assertEqual(
2149+
expected_parent_path, asgiref_resource.compute_parent_directory()
2150+
)
21402151
self.assertTrue(asgiref_resource.has_parent())
21412152
expected_parent = self.project_asgiref.codebaseresources.get(
21422153
path="asgiref-3.3.0.whl-extract/asgiref"

0 commit comments

Comments
 (0)