Skip to content

Commit 8813b4f

Browse files
Add extracted_to and extracted_from methods
Adds methods in CodebaseResource objects to navigate from an archive to their corresponding extracted directory and vice versa. This is added to match the functions in commoncode.resource.Resource so these functions work in the scancode.io context similarly to how they work in the scancode-toolkit context. Reference: #1585 Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 7e66f2c commit 8813b4f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

scanpipe/models.py

+12
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,18 @@ def walk(self, topdown=True):
28892889
if not topdown:
28902890
yield child
28912891

2892+
def extracted_to(self, codebase=None):
2893+
"""Return the path this Resource archive was extracted to or None."""
2894+
extract_path = f"{self.path}-extract"
2895+
return self.project.get_resource(extract_path)
2896+
2897+
def extracted_from(self, codebase=None):
2898+
"""Return the path to an archive this Resource was extracted from or None."""
2899+
path = self.path
2900+
if "-extract" in path:
2901+
archive_path, _, _ = self.path.rpartition("-extract")
2902+
return self.project.get_resource(archive_path)
2903+
28922904
def get_absolute_url(self):
28932905
return reverse("resource_detail", args=[self.project.slug, self.path])
28942906

scanpipe/tests/test_models.py

+13
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,19 @@ def test_scanpipe_codebase_resource_model_file_content_for_map(self):
14671467

14681468
self.assertEqual(expected, result)
14691469

1470+
def test_scanpipe_codebase_resource_model_commoncode_methods_extracted_to_from(
1471+
self,
1472+
):
1473+
archive_resource = CodebaseResource.objects.create(
1474+
project=self.project1, path="sample-archive.jar"
1475+
)
1476+
extracted_dir_resource = CodebaseResource.objects.create(
1477+
project=self.project1, path="sample-archive.jar-extract"
1478+
)
1479+
1480+
self.assertEqual(extracted_dir_resource, archive_resource.extracted_to())
1481+
self.assertEqual(archive_resource, extracted_dir_resource.extracted_from())
1482+
14701483
def test_scanpipe_codebase_resource_model_compliance_alert(self):
14711484
scanpipe_app.license_policies_index = license_policies_index
14721485
resource = CodebaseResource.objects.create(project=self.project1, path="file")

0 commit comments

Comments
 (0)