The script 'package-metadata.pl' already has a functionality to generate
JSON output from '.packageinfo'. However, this output containing
information about 'all' packages.

This information is also interesting at the image level in order to
further machine-process the information about the installed packages. This
commit adds the creation of a '*.pkg.info.json' file when building the
image.

The creation of the file is disabled by default and can be enabled via the
new Kconfig option 'CONFIG_JSON_PKG_INFO'.

Signed-off-by: Florian Eckert <[email protected]>
---
 config/Config-build.in      |  7 ++++++
 include/image.mk            |  6 +++++
 scripts/package-metadata.pl | 44 +++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)

diff --git a/config/Config-build.in b/config/Config-build.in
index 42b353ecf7..197a26a8ae 100644
--- a/config/Config-build.in
+++ b/config/Config-build.in
@@ -34,6 +34,13 @@ menu "Global build settings"
                  directory containing Software Bill Of Materials in CycloneDX
                  format.
 
+       config JSON_PKG_INFO
+               bool "Create packages info JSON"
+               default n
+               help
+                 Create a JSON files *.info.pkg.json in the build
+                 directory for further machine processing.
+
        config ALL_NONSHARED
                bool "Select all target specific packages by default"
                select ALL_KMODS
diff --git a/include/image.mk b/include/image.mk
index 048e750f33..5b800956f8 100644
--- a/include/image.mk
+++ b/include/image.mk
@@ -340,6 +340,12 @@ ifneq ($(CONFIG_JSON_CYCLONEDX_SBOM),)
                $(BIN_DIR)/$(IMG_PREFIX)$(if 
$(PROFILE_SANITIZED),-$(PROFILE_SANITIZED)).manifest > \
                $(BIN_DIR)/$(IMG_PREFIX)$(if 
$(PROFILE_SANITIZED),-$(PROFILE_SANITIZED)).bom.cdx.json
 endif
+ifneq ($(CONFIG_JSON_PKG_INFO),)
+       $(SCRIPT_DIR)/package-metadata.pl imgmanifestjson \
+               $(if $(IB),$(TOPDIR)/.packageinfo, $(TMP_DIR)/.packageinfo) \
+               $(BIN_DIR)/$(IMG_PREFIX)$(if 
$(PROFILE_SANITIZED),-$(PROFILE_SANITIZED)).manifest > \
+               $(BIN_DIR)/$(IMG_PREFIX)$(if 
$(PROFILE_SANITIZED),-$(PROFILE_SANITIZED)).pkg.info.json
+endif
 endef
 
 define Image/gzip-ext4-padded-squashfs
diff --git a/scripts/package-metadata.pl b/scripts/package-metadata.pl
index 82bd4360f3..90f0946e3a 100755
--- a/scripts/package-metadata.pl
+++ b/scripts/package-metadata.pl
@@ -589,6 +589,48 @@ sub gen_usergroup_list() {
        }
 }
 
+sub gen_image_manifest_json() {
+       my $pkginfo = shift @ARGV;
+       my $imgmanifest = shift @ARGV;
+       my $json;
+       my %image_packages;
+
+       %image_packages = image_manifest_packages($imgmanifest);
+       %image_packages or exit 1;
+
+       parse_package_metadata($pkginfo) or exit 1;
+       foreach my $name (sort {uc($a) cmp uc($b)} keys %image_packages) {
+               my %depends;
+               my $pkg = $package{$name};
+               foreach my $dep (@{$pkg->{depends} || []}) {
+                       if ($dep =~ m!^\+?(?:[^:]+:)?([^@]+)$!) {
+                               $depends{$1}++;
+                       }
+               }
+               my @depends = sort keys %depends;
+               my $pkg_deps = join ' ', map { qq/"$_",/ } @depends;
+               $pkg_deps =~ s/\,$//;
+
+               my $pkg_maintainer = join ' ', map { qq/"$_",/ } 
@{$pkg->{maintainer} || []};
+               $pkg_maintainer =~ s/\,$//;
+
+               $json = <<"END_JSON";
+${json}{
+"name":"$name",
+"version":"$pkg->{version}",
+"category":"$pkg->{category}",
+"license":"$pkg->{license}",
+"cpe_id":"$pkg->{cpe_id}",
+"maintainer": [$pkg_maintainer],
+"depends":[$pkg_deps]},
+END_JSON
+       }
+
+       $json =~ s/[\n\r]//g;
+       $json =~ s/\,$//;
+       print "[$json]";
+}
+
 sub gen_package_manifest_json() {
        my $json;
        parse_package_metadata($ARGV[0]) or exit 1;
@@ -802,6 +844,7 @@ sub parse_command() {
                /^kconfig/ and return gen_kconfig_overrides();
                /^source$/ and return gen_package_source();
                /^pkgaux$/ and return gen_package_auxiliary();
+               /^imgmanifestjson$/ and return gen_image_manifest_json();
                /^pkgmanifestjson$/ and return gen_package_manifest_json();
                /^imgcyclonedxsbom$/ and return gen_image_cyclonedxsbom();
                /^pkgcyclonedxsbom$/ and return gen_package_cyclonedxsbom();
@@ -817,6 +860,7 @@ Available Commands:
        $0 kconfig [file] [config] [patchver]   Kernel config overrides
        $0 source [file]                                Package source file 
information
        $0 pkgaux [file]                                Package auxiliary 
variables in makefile format
+       $0 imgmanifestjson [file] [manifest]    Image manifests in JSON format
        $0 pkgmanifestjson [file]                       Package manifests in 
JSON format
        $0 imgcyclonedxsbom <file> [manifest]   Image package manifest in 
CycloneDX SBOM JSON format
        $0 pkgcyclonedxsbom <file>                      Package manifest in 
CycloneDX SBOM JSON format
-- 
2.39.5


_______________________________________________
openwrt-devel mailing list
[email protected]
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to