From 5e60b059ffa932c40258b17f2b234e80862e7270 Mon Sep 17 00:00:00 2001 From: Ayan Sinha Mahapatra Date: Wed, 8 May 2024 18:40:36 +0530 Subject: [PATCH] Fix namespace assignment from distro ID Signed-off-by: Ayan Sinha Mahapatra --- src/packagedcode/distro.py | 30 +++++++++++----- src/packagedcode/models.py | 2 +- ...-container-layer.tar.xz.scan-expected.json | 6 ++-- tests/packagedcode/data/plugin/help.txt | 34 +++++++++---------- .../bdb-fedora-rootfs.tar.xz-expected.json | 6 ++-- 5 files changed, 45 insertions(+), 33 deletions(-) diff --git a/src/packagedcode/distro.py b/src/packagedcode/distro.py index 7e572948a03..233d74e4fdb 100644 --- a/src/packagedcode/distro.py +++ b/src/packagedcode/distro.py @@ -27,24 +27,36 @@ class EtcOsReleaseHandler(models.NonAssemblableDatafileHandler): @classmethod def parse(cls, location, package_only=False): distro = Distro.from_os_release_file(location) - distro_identifier = distro.identifier + distro_identifier = None + if distro: + distro_identifier = distro.identifier + + if not distro_identifier: + return + pretty_name = distro.pretty_name and distro.pretty_name.lower() or '' + # TODO: It is misleading to use package data fields + # name and namespace to store distro/os infomration, + # we should consider using extra_data fields instead. + if distro_identifier == 'debian': - namespace = 'debian' + name = 'debian' if 'distroless' in pretty_name: - name = 'distroless' + namespace = 'distroless' elif pretty_name.startswith('debian'): - name = 'distroless' + namespace = 'distroless' + else: + namespace = 'debian' elif distro_identifier == 'ubuntu' and distro.id_like == 'debian': - namespace = 'debian' - name = 'ubuntu' + namespace = 'ubuntu' + name = 'debian' - elif distro_identifier.startswith('fedora') or distro.id_like == 'fedora': - namespace = distro_identifier - name = distro.id_like or distro_identifier + elif distro_identifier.startswith('fedora') or distro.id_like == 'fedora': + name = distro_identifier or distro.id_like + namespace = distro.id_like else: # FIXME: this needs to be seriously updated diff --git a/src/packagedcode/models.py b/src/packagedcode/models.py index 28abb1436bd..7a6c5b46358 100644 --- a/src/packagedcode/models.py +++ b/src/packagedcode/models.py @@ -1511,7 +1511,7 @@ def get_distro_identifier_rootfs(cls, root_resource, codebase): # there can be only one distro distro = os_release_res.package_data and os_release_res.package_data[0] if distro: - identifier = distro.get("name") + identifier = distro.get("namespace") break return identifier diff --git a/tests/packagedcode/data/debian/debian-container-layer.tar.xz.scan-expected.json b/tests/packagedcode/data/debian/debian-container-layer.tar.xz.scan-expected.json index 8edc71fb05c..3d45f98bb69 100644 --- a/tests/packagedcode/data/debian/debian-container-layer.tar.xz.scan-expected.json +++ b/tests/packagedcode/data/debian/debian-container-layer.tar.xz.scan-expected.json @@ -355,8 +355,8 @@ "package_data": [ { "type": "linux-distro", - "namespace": "debian", - "name": "ubuntu", + "namespace": "ubuntu", + "name": "debian", "version": "22.04", "qualifiers": {}, "subpath": null, @@ -393,7 +393,7 @@ "repository_download_url": null, "api_data_url": null, "datasource_id": "etc_os_release", - "purl": "pkg:linux-distro/debian/ubuntu@22.04" + "purl": "pkg:linux-distro/ubuntu/debian@22.04" } ], "for_packages": [], diff --git a/tests/packagedcode/data/plugin/help.txt b/tests/packagedcode/data/plugin/help.txt index 8fb8389b38f..ea7e10a1007 100755 --- a/tests/packagedcode/data/plugin/help.txt +++ b/tests/packagedcode/data/plugin/help.txt @@ -6,41 +6,41 @@ Package type: about description: AboutCode ABOUT file path_patterns: '*.ABOUT' -------------------------------------------- -Package type: alpine +Package type: android + datasource_id: android_apk + documentation URL: https://en.wikipedia.org/wiki/Apk_(file_format) + primary language: Java + description: Android application package + path_patterns: '*.apk' +-------------------------------------------- +Package type: android_lib + datasource_id: android_aar_library + documentation URL: https://developer.android.com/studio/projects/android-library + primary language: Java + description: Android library archive + path_patterns: '*.aar' +-------------------------------------------- +Package type: apk datasource_id: alpine_apk_archive documentation URL: https://wiki.alpinelinux.org/wiki/Alpine_package_format primary language: None description: Alpine Linux .apk package archive path_patterns: '*.apk' -------------------------------------------- -Package type: alpine +Package type: apk datasource_id: alpine_apkbuild documentation URL: https://wiki.alpinelinux.org/wiki/APKBUILD_Reference primary language: None description: Alpine Linux APKBUILD package script path_patterns: '*APKBUILD' -------------------------------------------- -Package type: alpine +Package type: apk datasource_id: alpine_installed_db documentation URL: None primary language: None description: Alpine Linux installed package database path_patterns: '*lib/apk/db/installed' -------------------------------------------- -Package type: android - datasource_id: android_apk - documentation URL: https://en.wikipedia.org/wiki/Apk_(file_format) - primary language: Java - description: Android application package - path_patterns: '*.apk' --------------------------------------------- -Package type: android_lib - datasource_id: android_aar_library - documentation URL: https://developer.android.com/studio/projects/android-library - primary language: Java - description: Android library archive - path_patterns: '*.aar' --------------------------------------------- Package type: autotools datasource_id: autotools_configure documentation URL: https://www.gnu.org/software/automake/ diff --git a/tests/packagedcode/data/rpm_installed/end-to-end/bdb-fedora-rootfs.tar.xz-expected.json b/tests/packagedcode/data/rpm_installed/end-to-end/bdb-fedora-rootfs.tar.xz-expected.json index c6d7a7a2794..ca43ab07d29 100644 --- a/tests/packagedcode/data/rpm_installed/end-to-end/bdb-fedora-rootfs.tar.xz-expected.json +++ b/tests/packagedcode/data/rpm_installed/end-to-end/bdb-fedora-rootfs.tar.xz-expected.json @@ -203694,8 +203694,8 @@ "package_data": [ { "type": "linux-distro", - "namespace": "fedora-modular", - "name": "fedora", + "namespace": "fedora", + "name": "fedora-modular", "version": "26", "qualifiers": {}, "subpath": null, @@ -203732,7 +203732,7 @@ "repository_download_url": null, "api_data_url": null, "datasource_id": "etc_os_release", - "purl": "pkg:linux-distro/fedora-modular/fedora@26" + "purl": "pkg:linux-distro/fedora/fedora-modular@26" } ], "for_packages": [