Skip to content

Commit 02dab2e

Browse files
Assign debian namespace based on other packages
Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent 0b26ec8 commit 02dab2e

File tree

3 files changed

+196
-16
lines changed

3 files changed

+196
-16
lines changed

src/packagedcode/debian.py

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import os
1111
import logging
12+
from collections import Counter
1213
from pathlib import Path
1314

1415
from commoncode import fileutils
@@ -157,15 +158,19 @@ class DebianControlFileInSourceHandler(models.DatafileHandler):
157158

158159
@classmethod
159160
def parse(cls, location):
160-
# TODO: we cannot know the distro from the name only
161161
# NOTE: a control file in a source repo or debina.tar tarball can contain more than one package
162+
debian_packages = []
162163
for debian_data in get_paragraphs_data_from_file(location=location):
163-
yield build_package_data(
164-
debian_data,
165-
datasource_id=cls.datasource_id,
166-
package_type=cls.default_package_type,
164+
debian_packages.append(
165+
build_package_data(
166+
debian_data=debian_data,
167+
datasource_id=cls.datasource_id,
168+
package_type=cls.default_package_type,
169+
)
167170
)
168171

172+
yield from populate_debian_namespace(debian_packages)
173+
169174
@classmethod
170175
def assign_package_to_resources(cls, package, resource, codebase, package_adder):
171176
# two levels up
@@ -214,13 +219,18 @@ class DebianInstalledStatusDatabaseHandler(models.DatafileHandler):
214219
def parse(cls, location):
215220
# note that we do not know yet the distro at this stage
216221
# we could get it... but we get that later during assemble()
217-
for debian_data in get_paragraphs_data_from_file(location):
218-
yield build_package_data(
219-
debian_data,
220-
datasource_id=cls.datasource_id,
221-
package_type=cls.default_package_type,
222+
debian_packages = []
223+
for debian_data in get_paragraphs_data_from_file(location=location):
224+
debian_packages.append(
225+
build_package_data(
226+
debian_data=debian_data,
227+
datasource_id=cls.datasource_id,
228+
package_type=cls.default_package_type,
229+
)
222230
)
223231

232+
yield from populate_debian_namespace(debian_packages)
233+
224234
@classmethod
225235
def assemble(cls, package_data, resource, codebase, package_adder):
226236
# get the root resource of the rootfs
@@ -380,14 +390,18 @@ def parse(cls, location):
380390
rootfs installation. distroless is derived from Debian but each package
381391
has its own status file.
382392
"""
383-
for debian_data in get_paragraphs_data_from_file(location):
384-
yield build_package_data(
385-
debian_data,
386-
datasource_id=cls.datasource_id,
387-
package_type=cls.default_package_type,
388-
distro='distroless',
393+
debian_packages = []
394+
for debian_data in get_paragraphs_data_from_file(location=location):
395+
debian_packages.append(
396+
build_package_data(
397+
debian_data=debian_data,
398+
datasource_id=cls.datasource_id,
399+
package_type=cls.default_package_type,
400+
)
389401
)
390402

403+
yield from populate_debian_namespace(debian_packages)
404+
391405
@classmethod
392406
def assemble(cls, package_data, resource, codebase, package_adder):
393407
# get the root resource of the rootfs
@@ -667,6 +681,29 @@ def build_package_data(debian_data, datasource_id, package_type='deb', distro=No
667681
)
668682

669683

684+
def populate_debian_namespace(packages):
685+
"""
686+
For an iterable of debian `packages`, populate the
687+
most frequently occuring namespace, or the default
688+
namespace 'debian' in packages without namespace.
689+
"""
690+
if not packages:
691+
return
692+
693+
namespaces_with_count = Counter([
694+
package.namespace
695+
for package in packages
696+
])
697+
distro = max(namespaces_with_count, key=namespaces_with_count.get)
698+
if not distro:
699+
distro = 'debian'
700+
701+
for package in packages:
702+
if not package.namespace:
703+
package.namespace = distro
704+
yield package
705+
706+
670707
version_clues_for_namespace = {
671708
'deb': 'debian',
672709
'ubuntu': 'ubuntu',

tests/packagedcode/data/debian/ubuntu-var-lib-dpkg/expected.json

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,66 @@
563563
],
564564
"purl": "pkg:deb/ubuntu/[email protected]?architecture=amd64"
565565
},
566+
{
567+
"type": "deb",
568+
"namespace": "ubuntu",
569+
"name": "e2fsprogs",
570+
"version": "1.46.2-2",
571+
"qualifiers": {
572+
"architecture": "amd64"
573+
},
574+
"subpath": null,
575+
"primary_language": null,
576+
"description": "ext2/ext3/ext4 file system utilities\n The ext2, ext3 and ext4 file systems are successors of the original ext\n (\"extended\") file system. They are the main file system types used for\n hard disks on Debian and other Linux systems.\n .\n This package contains programs for creating, checking, and maintaining\n ext2/3/4-based file systems. It also includes the \"badblocks\" program,\n which can be used to scan for bad blocks on a disk or other storage device.",
577+
"release_date": null,
578+
"parties": [
579+
{
580+
"type": null,
581+
"role": "maintainer",
582+
"name": "Theodore Y. Ts'o <[email protected]>",
583+
"email": null,
584+
"url": null
585+
}
586+
],
587+
"keywords": [
588+
"admin"
589+
],
590+
"homepage_url": "http://e2fsprogs.sourceforge.net",
591+
"download_url": null,
592+
"size": null,
593+
"sha1": null,
594+
"md5": null,
595+
"sha256": null,
596+
"sha512": null,
597+
"bug_tracking_url": null,
598+
"code_view_url": null,
599+
"vcs_url": null,
600+
"copyright": null,
601+
"holder": null,
602+
"declared_license_expression": null,
603+
"declared_license_expression_spdx": null,
604+
"license_detections": [],
605+
"other_license_expression": null,
606+
"other_license_expression_spdx": null,
607+
"other_license_detections": [],
608+
"extracted_license_statement": null,
609+
"notice_text": null,
610+
"source_packages": [],
611+
"extra_data": {
612+
"multi_arch": "foreign"
613+
},
614+
"repository_homepage_url": null,
615+
"repository_download_url": null,
616+
"api_data_url": null,
617+
"package_uid": "pkg:deb/ubuntu/[email protected]?architecture=amd64&uuid=fixed-uid-done-for-testing-5642512d1758",
618+
"datafile_paths": [
619+
"ubuntu-var-lib-dpkg/var/lib/dpkg/status"
620+
],
621+
"datasource_ids": [
622+
"debian_installed_status_db"
623+
],
624+
"purl": "pkg:deb/ubuntu/[email protected]?architecture=amd64"
625+
},
566626
{
567627
"type": "deb",
568628
"namespace": "ubuntu",
@@ -2699,6 +2759,62 @@
26992759
"datasource_id": "debian_installed_status_db",
27002760
"purl": "pkg:deb/ubuntu/[email protected]?architecture=amd64"
27012761
},
2762+
{
2763+
"type": "deb",
2764+
"namespace": "ubuntu",
2765+
"name": "e2fsprogs",
2766+
"version": "1.46.2-2",
2767+
"qualifiers": {
2768+
"architecture": "amd64"
2769+
},
2770+
"subpath": null,
2771+
"primary_language": null,
2772+
"description": "ext2/ext3/ext4 file system utilities\n The ext2, ext3 and ext4 file systems are successors of the original ext\n (\"extended\") file system. They are the main file system types used for\n hard disks on Debian and other Linux systems.\n .\n This package contains programs for creating, checking, and maintaining\n ext2/3/4-based file systems. It also includes the \"badblocks\" program,\n which can be used to scan for bad blocks on a disk or other storage device.",
2773+
"release_date": null,
2774+
"parties": [
2775+
{
2776+
"type": null,
2777+
"role": "maintainer",
2778+
"name": "Theodore Y. Ts'o <[email protected]>",
2779+
"email": null,
2780+
"url": null
2781+
}
2782+
],
2783+
"keywords": [
2784+
"admin"
2785+
],
2786+
"homepage_url": "http://e2fsprogs.sourceforge.net",
2787+
"download_url": null,
2788+
"size": null,
2789+
"sha1": null,
2790+
"md5": null,
2791+
"sha256": null,
2792+
"sha512": null,
2793+
"bug_tracking_url": null,
2794+
"code_view_url": null,
2795+
"vcs_url": null,
2796+
"copyright": null,
2797+
"holder": null,
2798+
"declared_license_expression": null,
2799+
"declared_license_expression_spdx": null,
2800+
"license_detections": [],
2801+
"other_license_expression": null,
2802+
"other_license_expression_spdx": null,
2803+
"other_license_detections": [],
2804+
"extracted_license_statement": null,
2805+
"notice_text": null,
2806+
"source_packages": [],
2807+
"file_references": [],
2808+
"extra_data": {
2809+
"multi_arch": "foreign"
2810+
},
2811+
"dependencies": [],
2812+
"repository_homepage_url": null,
2813+
"repository_download_url": null,
2814+
"api_data_url": null,
2815+
"datasource_id": "debian_installed_status_db",
2816+
"purl": "pkg:deb/ubuntu/[email protected]?architecture=amd64"
2817+
},
27022818
{
27032819
"type": "deb",
27042820
"namespace": "ubuntu",

tests/packagedcode/data/debian/ubuntu-var-lib-dpkg/var/lib/dpkg/status

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ Description: GNU Bourne Again SHell
8787
Homepage: http://tiswww.case.edu/php/chet/bash/bashtop.html
8888
Original-Maintainer: Matthias Klose <[email protected]>
8989

90+
Package: e2fsprogs
91+
Status: install ok installed
92+
Priority: required
93+
Section: admin
94+
Installed-Size: 1511
95+
Maintainer: Theodore Y. Ts'o <[email protected]>
96+
Architecture: amd64
97+
Multi-Arch: foreign
98+
Version: 1.46.2-2
99+
Depends: logsave
100+
Pre-Depends: libblkid1 (>= 2.36), libc6 (>= 2.14), libcom-err2 (>= 1.43.9), libext2fs2 (= 1.46.2-2), libss2 (>= 1.38), libuuid1 (>= 2.16)
101+
Recommends: e2fsprogs-l10n
102+
Suggests: gpart, parted, fuse2fs, e2fsck-static
103+
Conffiles:
104+
/etc/cron.d/e2scrub_all bc533e09f3b3d96bfe1633ad57eb7026
105+
/etc/e2scrub.conf df38534cc670c70a91cf9b035845d244
106+
/etc/mke2fs.conf 72b349d890a9b5cca06c7804cd0c8d1d
107+
Description: ext2/ext3/ext4 file system utilities
108+
The ext2, ext3 and ext4 file systems are successors of the original ext
109+
("extended") file system. They are the main file system types used for
110+
hard disks on Debian and other Linux systems.
111+
.
112+
This package contains programs for creating, checking, and maintaining
113+
ext2/3/4-based file systems. It also includes the "badblocks" program,
114+
which can be used to scan for bad blocks on a disk or other storage device.
115+
Homepage: http://e2fsprogs.sourceforge.net
116+
90117
Package: tar
91118
Essential: yes
92119
Status: install ok installed

0 commit comments

Comments
 (0)