[OpenWrt-Devel] [PATCH v2] opkg: add --license arg for installed packages

Paul Spooren mail at aparcar.org
Thu Jun 13 13:12:39 EDT 2019


add --license option to print (SPDX) license tag in the
`installed-packages` overview. This is useful for manifest generation,
as it show all licenses used within an image. In case the license is not
available, a question mark (?) is printed instead.

base-files - 198-r10203+1-c12bd3a21b - GPL-2.0
bnx2-firmware - 20190416-1 - ?
busybox - 1.30.1-4 - GPL-2.0
dnsmasq - 2.80-13 - GPL-2.0
dropbear - 2019.78-2 - MIT
e2fsprogs - 1.44.5-1 - GPL-2.0
firewall - 2019-01-02-70f8785b-2 - ISC
fstools - 2019-03-28-ff1ded63-4 - GPL-2.0
fwtool - 1 - ?
getrandom - 2019-04-07-5130fa4d-2 - GPL-2.0
ip6tables - 1.8.2-3 - GPL-2.0
iptables - 1.8.2-3 - GPL-2.0
jshn - 2019-02-27-eeef7b50-1 - ISC
jsonfilter - 2018-02-04-c7e938d6-1 - ISC
...

CC: Jo-Philipp Wich <jo at mein.io>
Signed-off-by: Paul Spooren <mail at aparcar.org>
---
v2 <- v1 ignore license via PFM_LICENSE in opkg_cmd.c when not actually needed

 libopkg/opkg_cmd.c  | 51 +++++++++++++++++++++++++--------------------
 libopkg/opkg_conf.c |  1 +
 libopkg/opkg_conf.h |  1 +
 libopkg/pkg.c       | 11 ++++++++++
 libopkg/pkg.h       |  1 +
 libopkg/pkg_parse.c |  5 +++++
 libopkg/pkg_parse.h |  1 +
 src/opkg-cl.c       |  7 +++++++
 8 files changed, 55 insertions(+), 23 deletions(-)

diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c
index c823df8..5fc9ae5 100644
--- a/libopkg/opkg_cmd.c
+++ b/libopkg/opkg_cmd.c
@@ -45,9 +45,12 @@ static void print_pkg(pkg_t * pkg)
 {
 	char *version = pkg_version_str_alloc(pkg);
 	char *description = pkg_get_string(pkg, PKG_DESCRIPTION);
+	char *license = pkg_get_string(pkg, PKG_LICENSE);
 	printf("%s - %s", pkg->name, version);
 	if (conf->size)
 		printf(" - %lu", (unsigned long) pkg_get_int(pkg, PKG_SIZE));
+	if (conf->license)
+		printf(" - %s", license ? license : "?");
 	if (description)
 		printf(" - %s", description);
 	printf("\n");
@@ -1255,10 +1258,10 @@ static int opkg_print_architecture_cmd(int argc, char **argv)
    array for easier maintenance */
 static opkg_cmd_t cmds[] = {
 	{"update", 0, (opkg_cmd_fun_t) opkg_update_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"upgrade", 1, (opkg_cmd_fun_t) opkg_upgrade_cmd,
 	 PFM_DESCRIPTION | PFM_SOURCE},
-	{"list", 0, (opkg_cmd_fun_t) opkg_list_cmd, PFM_SOURCE},
+	{"list", 0, (opkg_cmd_fun_t) opkg_list_cmd, PFM_SOURCE | PFM_LICENSE},
 	{"list_installed", 0, (opkg_cmd_fun_t) opkg_list_installed_cmd,
 	 PFM_SOURCE},
 	{"list-installed", 0, (opkg_cmd_fun_t) opkg_list_installed_cmd,
@@ -1268,55 +1271,57 @@ static opkg_cmd_t cmds[] = {
 	{"list-upgradable", 0, (opkg_cmd_fun_t) opkg_list_upgradable_cmd,
 	 PFM_SOURCE},
 	{"list_changed_conffiles", 0,
-	 (opkg_cmd_fun_t) opkg_list_changed_conffiles_cmd, PFM_SOURCE},
+	 (opkg_cmd_fun_t) opkg_list_changed_conffiles_cmd,
+	 PFM_SOURCE | PFM_LICENSE},
 	{"list-changed-conffiles", 0,
-	 (opkg_cmd_fun_t) opkg_list_changed_conffiles_cmd, PFM_SOURCE},
+	 (opkg_cmd_fun_t) opkg_list_changed_conffiles_cmd,
+	 PFM_SOURCE | PFM_LICENSE},
 	{"info", 0, (opkg_cmd_fun_t) opkg_info_cmd, 0},
 	{"flag", 1, (opkg_cmd_fun_t) opkg_flag_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"status", 0, (opkg_cmd_fun_t) opkg_status_cmd,
 	 PFM_DESCRIPTION | PFM_SOURCE},
 	{"install", 1, (opkg_cmd_fun_t) opkg_install_cmd,
 	 PFM_DESCRIPTION | PFM_SOURCE},
 	{"remove", 1, (opkg_cmd_fun_t) opkg_remove_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"configure", 0, (opkg_cmd_fun_t) opkg_configure_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"files", 1, (opkg_cmd_fun_t) opkg_files_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"search", 1, (opkg_cmd_fun_t) opkg_search_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
-	{"find", 1, (opkg_cmd_fun_t) opkg_find_cmd, PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
+	{"find", 1, (opkg_cmd_fun_t) opkg_find_cmd, PFM_SOURCE | PFM_LICENSE},
 	{"download", 1, (opkg_cmd_fun_t) opkg_download_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"compare_versions", 1, (opkg_cmd_fun_t) opkg_compare_versions_cmd, 0},
 	{"compare-versions", 1, (opkg_cmd_fun_t) opkg_compare_versions_cmd, 0},
 	{"print-architecture", 0, (opkg_cmd_fun_t) opkg_print_architecture_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"print_architecture", 0, (opkg_cmd_fun_t) opkg_print_architecture_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"print-installation-architecture", 0,
 	 (opkg_cmd_fun_t) opkg_print_architecture_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"print_installation_architecture", 0,
 	 (opkg_cmd_fun_t) opkg_print_architecture_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"depends", 1, (opkg_cmd_fun_t) opkg_depends_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"whatdepends", 1, (opkg_cmd_fun_t) opkg_whatdepends_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"whatdependsrec", 1, (opkg_cmd_fun_t) opkg_whatdepends_recursively_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"whatrecommends", 1, (opkg_cmd_fun_t) opkg_whatrecommends_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"whatsuggests", 1, (opkg_cmd_fun_t) opkg_whatsuggests_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"whatprovides", 1, (opkg_cmd_fun_t) opkg_whatprovides_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"whatreplaces", 1, (opkg_cmd_fun_t) opkg_whatreplaces_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 	{"whatconflicts", 1, (opkg_cmd_fun_t) opkg_whatconflicts_cmd,
-	 PFM_DESCRIPTION | PFM_SOURCE},
+	 PFM_DESCRIPTION | PFM_SOURCE | PFM_LICENSE},
 };
 
 opkg_cmd_t *opkg_cmd_find(const char *name)
diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c
index 08855eb..40b1734 100644
--- a/libopkg/opkg_conf.c
+++ b/libopkg/opkg_conf.c
@@ -70,6 +70,7 @@ opkg_option_t options[] = {
 	{"proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user},
 	{"query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all},
 	{"size", OPKG_OPT_TYPE_BOOL, &_conf.size},
+	{"license", OPKG_OPT_TYPE_BOOL, &_conf.license},
 	{"tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir},
 	{"verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity},
 	{NULL, 0, NULL}
diff --git a/libopkg/opkg_conf.h b/libopkg/opkg_conf.h
index 37f95a1..0413ccd 100644
--- a/libopkg/opkg_conf.h
+++ b/libopkg/opkg_conf.h
@@ -87,6 +87,7 @@ struct opkg_conf {
 	int verbosity;
 	int noaction;
 	int size;
+	int license;
 	int download_only;
 	char *cache;
 
diff --git a/libopkg/pkg.c b/libopkg/pkg.c
index e5bfe6f..fb1b2f1 100644
--- a/libopkg/pkg.c
+++ b/libopkg/pkg.c
@@ -777,6 +777,15 @@ void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field)
 			}
 		}
 		break;
+	case 'l':
+	case 'L':
+		if (strcasecmp(field, "License") == 0) {
+			p = pkg_get_string(pkg, PKG_LICENSE);
+			if (p && *p) {
+				fprintf(fp, "License: %s\n", p);
+			}
+		}
+		break;
 	case 'm':
 	case 'M':
 		if (strcasecmp(field, "Maintainer") == 0) {
@@ -926,6 +935,7 @@ void pkg_formatted_info(FILE * fp, pkg_t * pkg)
 	pkg_formatted_field(fp, pkg, "Package");
 	pkg_formatted_field(fp, pkg, "Version");
 	pkg_formatted_field(fp, pkg, "Depends");
+	pkg_formatted_field(fp, pkg, "License");
 	pkg_formatted_field(fp, pkg, "Recommends");
 	pkg_formatted_field(fp, pkg, "Suggests");
 	pkg_formatted_field(fp, pkg, "Provides");
@@ -956,6 +966,7 @@ void pkg_print_status(pkg_t * pkg, FILE * file)
 	pkg_formatted_field(file, pkg, "Package");
 	pkg_formatted_field(file, pkg, "Version");
 	pkg_formatted_field(file, pkg, "Depends");
+	pkg_formatted_field(file, pkg, "License");
 	pkg_formatted_field(file, pkg, "Recommends");
 	pkg_formatted_field(file, pkg, "Suggests");
 	pkg_formatted_field(file, pkg, "Provides");
diff --git a/libopkg/pkg.h b/libopkg/pkg.h
index 600fc9e..0065043 100644
--- a/libopkg/pkg.h
+++ b/libopkg/pkg.h
@@ -79,6 +79,7 @@ typedef enum pkg_state_status pkg_state_status_t;
 
 enum pkg_fields {
 	PKG_MAINTAINER,
+	PKG_LICENSE,
 	PKG_PRIORITY,
 	PKG_SOURCE,
 	PKG_TAGS,
diff --git a/libopkg/pkg_parse.c b/libopkg/pkg_parse.c
index 0baa4db..87db2fa 100644
--- a/libopkg/pkg_parse.c
+++ b/libopkg/pkg_parse.c
@@ -269,6 +269,11 @@ int pkg_parse_line(void *ptr, char *line, uint mask)
 			pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
 		}
 		break;
+	case 'l':
+	case 'L':
+		if ((mask & PFM_LICENSE) && is_field("License:", line))
+			pkg_set_string(pkg, PKG_LICENSE, line + strlen("License") + 1);
+		break;
 
 	case 'M':
 		if ((mask & PFM_MD5SUM) && (is_field("MD5sum:", line) || is_field("MD5Sum:", line)))
diff --git a/libopkg/pkg_parse.h b/libopkg/pkg_parse.h
index d1f901a..ccb97aa 100644
--- a/libopkg/pkg_parse.h
+++ b/libopkg/pkg_parse.h
@@ -54,6 +54,7 @@ int pkg_parse_line(void *ptr, char *line, uint mask);
 #define PFM_SUGGESTS		(1 << 25)
 #define PFM_TAGS		(1 << 26)
 #define PFM_VERSION		(1 << 27)
+#define PFM_LICENSE		(1 << 28)
 
 #define PFM_ALL	(~(uint)0)
 
diff --git a/src/opkg-cl.c b/src/opkg-cl.c
index a3ea5c1..b867487 100644
--- a/src/opkg-cl.c
+++ b/src/opkg-cl.c
@@ -54,6 +54,7 @@ enum {
 	ARGS_OPT_FORCE_SIGNATURE,
 	ARGS_OPT_NO_CHECK_CERTIFICATE,
 	ARGS_OPT_SIZE,
+	ARGS_OPT_LICENSE,
 };
 
 static struct option long_options[] = {
@@ -103,6 +104,7 @@ static struct option long_options[] = {
 	{"add-arch", 1, 0, ARGS_OPT_ADD_ARCH},
 	{"add-dest", 1, 0, ARGS_OPT_ADD_DEST},
 	{"size", 0, 0, ARGS_OPT_SIZE},
+	{"license", 0, 0, ARGS_OPT_LICENSE},
 	{"test", 0, 0, ARGS_OPT_NOACTION},
 	{"tmp-dir", 1, 0, 't'},
 	{"tmp_dir", 1, 0, 't'},
@@ -220,6 +222,9 @@ static int args_parse(int argc, char *argv[])
 		case ARGS_OPT_SIZE:
 			conf->size = 1;
 			break;
+		case ARGS_OPT_LICENSE:
+			conf->license = 1;
+			break;
 		case ARGS_OPT_NOACTION:
 			conf->noaction = 1;
 			break;
@@ -349,6 +354,8 @@ static void usage()
 	    ("\t--nocase		Perform case insensitive pattern matching\n");
 	printf
 	    ("\t--size			Print package size when listing available packages\n");
+	printf
+	    ("\t--license		Print package license when listing available packages\n");
 	printf("\t--force-removal-of-dependent-packages\n");
 	printf("\t			Remove package and all dependencies\n");
 	printf("\t--autoremove		Remove packages that were installed\n");
-- 
2.20.1


_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list