Skip to content

Commit a063e44

Browse files
authored
Fix a bug in scan_rootfs_for_system_packages #1462 (#1464)
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 20107ac commit a063e44

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
v34.9.2 (unreleased)
5+
--------------------
6+
7+
- Fix an issue with the ``scan_rootfs_for_system_packages`` pipe when a namespace is
8+
missing for the discovered packages.
9+
https://github.com/aboutcode-org/scancode.io/issues/1462
10+
411
v34.9.1 (2024-12-09)
512
--------------------
613

scanpipe/pipes/rootfs.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def _create_system_package(project, purl, package):
209209
# We have no files for this installed package, we cannot go further.
210210
if not installed_files:
211211
logger.info(f" No installed_files for: {purl}")
212-
return
212+
return created_package
213213

214214
missing_resources = created_package.missing_resources[:]
215215
modified_resources = created_package.modified_resources[:]
@@ -245,6 +245,8 @@ def _create_system_package(project, purl, package):
245245
modified_resources=modified_resources,
246246
)
247247

248+
return created_package
249+
248250

249251
def scan_rootfs_for_system_packages(project, rootfs):
250252
"""
@@ -270,24 +272,23 @@ def scan_rootfs_for_system_packages(project, rootfs):
270272
seen_namespaces = []
271273
for index, (purl, package) in enumerate(installed_packages):
272274
logger.info(f"Creating package #{index}: {purl}")
273-
created_system_packages.append(package)
274-
seen_namespaces.append(package.namespace)
275-
_create_system_package(project, purl, package)
275+
discovered_package = _create_system_package(project, purl, package)
276+
created_system_packages.append(discovered_package)
277+
if package.namespace:
278+
seen_namespaces.append(package.namespace)
276279

277280
namespace_counts = Counter(seen_namespaces)
278-
# we overwrite namespace only when there are multiple
279-
# namespaces in the packages
281+
# Overwrite namespace only when there are multiple namespaces in the packages
280282
if not len(namespace_counts.keys()) > 1:
281283
return
282284

283285
most_seen_namespace = max(namespace_counts)
284-
# if the distro_id is different from the namespace
285-
# most seen in packages, we update all the package
286-
# namespaces to the distro_id
286+
# If the distro_id is different from the namespace most seen in packages,
287+
# we update all the package namespaces to the distro_id.
287288
if most_seen_namespace != distro_id:
288-
for package in created_system_packages:
289-
if package.namespace != distro_id:
290-
package.update(namespace=distro_id)
289+
for discovered_package in created_system_packages:
290+
if discovered_package.namespace != distro_id:
291+
discovered_package.update(namespace=distro_id)
291292

292293

293294
def get_resource_with_md5(project, status):

scanpipe/tests/pipes/test_rootfs.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
from django.test import TestCase
2727

2828
from commoncode.archive import extract_tar
29+
from container_inspector.distro import Distro
30+
from packagedcode.models import PackageWithResources
2931

3032
from scanpipe.models import CodebaseResource
3133
from scanpipe.models import Project
3234
from scanpipe.pipes import rootfs
35+
from scanpipe.pipes.rootfs import RootFs
3336

3437

3538
class ScanPipeRootfsPipesTest(TestCase):
@@ -159,3 +162,61 @@ def test_scanpipe_pipes_rootfs_flag_media_files_as_uninteresting(self):
159162
self.assertEqual("ignored-media-file", resource1.status)
160163
self.assertEqual("ignored-media-file", resource2.status)
161164
self.assertEqual("", resource3.status)
165+
166+
@mock.patch("scanpipe.pipes.rootfs.RootFs.get_installed_packages")
167+
def test_scanpipe_pipes_rootfs_scan_rootfs_for_system_packages(
168+
self, mock_get_installed_packages
169+
):
170+
project = Project.objects.create(name="Analysis")
171+
rootfs_instance = RootFs(location="")
172+
rootfs_instance.distro = Distro(identifier="debian")
173+
174+
system_packages = [
175+
(
176+
"pkg:deb/ubuntu/libncurses5@1.0",
177+
PackageWithResources(
178+
type="deb",
179+
namespace="ubuntu",
180+
name="libncurses5",
181+
version="1.0",
182+
),
183+
),
184+
(
185+
# Same namespace
186+
"pkg:deb/ubuntu/libncurses5@2.0",
187+
PackageWithResources(
188+
type="deb",
189+
namespace="ubuntu",
190+
name="libncurses5",
191+
version="2.0",
192+
),
193+
),
194+
(
195+
# Different namespace
196+
"pkg:deb/other/libncurses5@3.0",
197+
PackageWithResources(
198+
type="deb",
199+
namespace="debian",
200+
name="libncurses5",
201+
version="3.0",
202+
),
203+
),
204+
(
205+
# This package has no namespace on purpose.
206+
"pkg:deb/libndp0@1.4-2ubuntu0.16.04.1",
207+
PackageWithResources(
208+
type="deb",
209+
name="libndp0",
210+
version="1.4-2ubuntu0.16.04.1",
211+
),
212+
),
213+
]
214+
215+
mock_get_installed_packages.return_value = system_packages
216+
rootfs.scan_rootfs_for_system_packages(project, rootfs_instance)
217+
218+
package_qs = project.discoveredpackages.all()
219+
self.assertEqual(4, package_qs.count())
220+
self.assertEqual(0, package_qs.filter(namespace="ubuntu").count())
221+
# All namespaces updated to "debian" as the most common namespace
222+
self.assertEqual(4, package_qs.filter(namespace="debian").count())

0 commit comments

Comments
 (0)