Skip to content

Commit 074a53a

Browse files
Support regex in ABOUT resource paths
Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent cf62173 commit 074a53a

5 files changed

Lines changed: 30 additions & 20 deletions

File tree

scanpipe/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,15 @@ def path_pattern(self, pattern):
17791779
"""Resources with a path that match the provided ``pattern``."""
17801780
return self.filter(path__regex=posix_regex_to_django_regex_lookup(pattern))
17811781

1782+
def path_patterns(self, patterns):
1783+
"""Resources with a path that match the provided ``pattern``."""
1784+
lookups = Q()
1785+
for resource_pattern in patterns:
1786+
lookups |= Q(
1787+
**{"path__regex": posix_regex_to_django_regex_lookup(resource_pattern)}
1788+
)
1789+
return self.filter(~lookups)
1790+
17821791
def has_directory_content_fingerprint(self):
17831792
"""
17841793
Resources that have the key `directory_content` set in the `extra_data`

scanpipe/pipes/d2d.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,8 @@ def _map_about_file_resource(project, about_file_resource, to_resources):
790790
)
791791
return
792792

793-
filename = package_data.get("filename")
794-
if not filename:
793+
files_pattern = package_data.get("filename")
794+
if not files_pattern:
795795
# Cannot map anything without the about_resource value.
796796
project.add_error(
797797
description="ABOUT file does not have about_resource",
@@ -800,12 +800,12 @@ def _map_about_file_resource(project, about_file_resource, to_resources):
800800
)
801801
return
802802

803-
ignored_resources = []
803+
ignored_resource_patterns = []
804+
codebase_resources = to_resources.path_pattern(pattern=files_pattern)
804805
if extra_data := package_data.get("extra_data"):
805-
ignored_resources = extra_data.get("ignored_resources")
806+
ignored_resource_patterns = extra_data.get("ignored_resources")
806807

807808
# Fetch all resources that are covered by the .ABOUT file.
808-
codebase_resources = to_resources.filter(path__contains=f"/{filename.lstrip('/')}")
809809
if not codebase_resources:
810810
# If there's nothing to map on the ``to/`` do not create the package.
811811
project.add_warning(
@@ -819,11 +819,10 @@ def _map_about_file_resource(project, about_file_resource, to_resources):
819819
return
820820

821821
# Ignore resources for paths in `ignored_resources` attribute
822-
if ignored_resources:
823-
lookups = Q()
824-
for resource_path in ignored_resources:
825-
lookups |= Q(**{"path__contains": resource_path})
826-
codebase_resources = codebase_resources.filter(~lookups)
822+
if ignored_resource_patterns:
823+
codebase_resources = codebase_resources.path_patterns(
824+
patterns=ignored_resource_patterns
825+
)
827826

828827
# Create the Package using .ABOUT data and assigned related codebase_resources
829828
pipes.update_or_create_package(project, package_data, codebase_resources)

scanpipe/tests/data/d2d/about_files/expected.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"source_packages": [],
6262
"extra_data": {
6363
"ignored_resources": [
64-
"flume-ng-node-1.9.0.jar-extract/org/apache/flume/node/ConfigurationProvider.class"
64+
"*flume-ng-node-*.jar-extract/org/apache/flume/node/ConfigurationProvider.class"
6565
]
6666
},
6767
"package_uid": "",
@@ -112,9 +112,9 @@
112112
"status": "not-deployed",
113113
"tag": "from",
114114
"extension": ".ABOUT",
115-
"md5": "04c25308d59068db649ebfd5d8103338",
116-
"sha1": "9625fc925a01cfa22b6e1ce083a1b1802c2ce78c",
117-
"sha256": "46142b274cda38e9c183001b58a72c8fc442b2162ae85e9a453a2c7a1a86d427",
115+
"md5": "4f8ac19bc3661bbaac91fb8652b6c4cb",
116+
"sha1": "ac27839cc3010c96b34796be83bc2e6b6bc1882b",
117+
"sha256": "055133491484d680991ed98ba1f791e88f4552955e15d9c5b3b14c5d46b4ee16",
118118
"sha512": "",
119119
"programming_language": "",
120120
"is_binary": false,
@@ -143,9 +143,9 @@
143143
"status": "about-mapped",
144144
"tag": "from",
145145
"extension": ".ABOUT",
146-
"md5": "b1d5c62c364d4470557bfba7d0338758",
147-
"sha1": "828f79d9fc0619a5b869c46a54b10ee3573a00bb",
148-
"sha256": "de514210e135dddffb6ace69aa5fe27e1873146e05eeb5b05c6de1f8c00b0010",
146+
"md5": "c7fab493a90ebf247954e1e30582ba8f",
147+
"sha1": "3090f7d036bf68c5421364ac03a094715348f9de",
148+
"sha256": "71f10662c0806de172a04a07fffbb3d22bd72ddacef4188849a480afd4e46849",
149149
"sha512": "",
150150
"programming_language": "",
151151
"is_binary": false,
@@ -174,9 +174,9 @@
174174
"status": "about-mapped",
175175
"tag": "from",
176176
"extension": ".LICENSE",
177-
"md5": "2b42edef8fa55315f34f2370b4715ca9",
178-
"sha1": "58853eb8199b5afe72a73a25fd8cf8c94285174b",
179-
"sha256": "43070e2d4e532684de521b885f385d0841030efa2b1a20bafb76133a5e1379c1",
177+
"md5": "94c82ae800466538d15278d6be4feedc",
178+
"sha1": "3837fdbc9d942bcd1c5f2d419148e944f7ce996a",
179+
"sha256": "a4da19948e6906fa8af95a258b9a354f641adc6215956f0ec63f429a10f0f603",
180180
"sha512": "",
181181
"programming_language": "",
182182
"is_binary": false,
-3.82 KB
Binary file not shown.

scanpipe/tests/test_models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,7 @@ def test_scanpipe_codebase_resource_queryset_path_pattern(self):
14671467
make_resource_file(self.project1, path="dir/.example")
14681468
make_resource_file(self.project1, path="dir/subdir/readme.html")
14691469
make_resource_file(self.project1, path="foo$.class")
1470+
make_resource_file(self.project1, path="example-1.0.jar")
14701471

14711472
patterns = [
14721473
"example",
@@ -1483,6 +1484,7 @@ def test_scanpipe_codebase_resource_queryset_path_pattern(self):
14831484
"dir/*/readme.*",
14841485
r"*$.class",
14851486
"*readme.htm?",
1487+
"example-*.jar",
14861488
]
14871489

14881490
for pattern in patterns:

0 commit comments

Comments
 (0)