[OpenWrt-Devel,v3] feeds: search packages by license
diff mbox

Message ID 54C6B007.60006@prplfoundation.org
State Rejected
Delegated to: John Crispin
Headers show

Commit Message

Eric Schultz Jan. 26, 2015, 9:22 p.m. UTC
Currently, the feeds scripts provides no way to filter packages by their license.
This patch adds a search feature to feeds which can be used to search for packages
using the -l <license> option.

Additionally, use of the -n option modifies the search to only return packages
which don't have a license.

Calls to script/feeds which do not use the new -l or -n options should work the same as before.

Signed-off-by: Eric Schultz <eschultz@prplfoundation.org>
---
 scripts/feeds | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 75 insertions(+), 11 deletions(-)

Patch
diff mbox

diff --git a/scripts/feeds b/scripts/feeds
index 31ad544..b098463 100755
--- a/scripts/feeds
+++ b/scripts/feeds
@@ -206,41 +206,103 @@  sub get_installed() {
 
 sub search_feed {
 	my $feed = shift;
+	my $search_parameters = shift;
 	my @substr = @_;
 	my $display;
 
-	return unless @substr > 0;
+	return unless @substr > 0 or $search_parameters->{use_params};
+
 	get_feed($feed);
 	foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
+
 		my $pkg = $feed_package->{$name};
 		my $substr;
 		my $pkgmatch = 1;
 
 		next if $pkg->{vdepends};
-		foreach my $substr (@substr) {
-			my $match;
-			foreach my $key (qw(name title description src)) {
-				$pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
-			}
-			$match or undef $pkgmatch;
-		};
+		if ( $search_parameters->{use_params})
+		{
+			$pkgmatch = do_package_tags_match($pkg, $search_parameters);
+		}
+
+		if (! $search_parameters->{use_params} or ( $search_parameters->{use_params} and $pkgmatch))
+		{
+			foreach my $substr (@substr) {
+				my $match;
+				foreach my $key (qw(name title description src)) {
+					$pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
+				}
+
+				$match or undef $pkgmatch;
+			};
+		}
 		$pkgmatch and do {
 			$display or do {
 				print "Search results in feed '$feed':\n";
 				$display = 1;
 			};
-			printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
+			my $output_print = format_search_parameter_output($pkg, $search_parameters);
+			printf "\%-20s\%s\%s\n", $pkg->{name}, $output_print, $pkg->{title};
 		};
 	}
 	return 0;
 }
 
+sub format_search_parameter_output
+{
+	my $pkg = shift;
+	my $search_parameters = shift;
+
+	my $output = "\t";
+
+	if (!$search_parameters->{use_params}) {
+		return $output;
+	}
+
+	if (defined($search_parameters->{license})) {
+		$output = $output . sprintf(("\%-10s\t"), $pkg->{license});
+	}
+
+	return $output;
+}
+
+sub do_package_tags_match {
+	my $pkg = shift;
+  my $parameterized_search = shift;
+
+
+	my $match = 0;
+	if ($pkg->{license} and $parameterized_search->{license}) {
+		my $substr = $parameterized_search->{license};
+		my @results = grep(/^\Q$substr\E.*$/i, split(/\s/, $pkg->{license}));
+
+		$match = @results > 0;
+	}
+
+	if ($parameterized_search->{no_license}) {
+		$match = !defined($pkg->{license});
+	}
+
+	return $match;
+}
+
 sub search {
 	my %opts;
 
-	getopt('r:', \%opts);
+	getopts('r:l:n', \%opts);
+	my %search_parameters;
+	if (defined($opts{l})) {
+		$search_parameters{license} = $opts{l};
+		$search_parameters{use_params} = 1;
+	}
+
+	if (defined($opts{n})) {
+		$search_parameters{no_license} = $opts{n};
+		$search_parameters{use_params} = 1;
+	}
+
 	foreach my $feed (@feeds) {
-		search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
+		search_feed($feed->[1], \%search_parameters, @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
 	}
 }
 
@@ -642,6 +704,8 @@  Commands:
 	search [options] <substring>: Search for a package
 	Options:
 	    -r <feedname>: Only search in this feed
+	    -l <license>:  Only include packages with this license
+	    -n:	           Only include packages without a license tag
 
 	uninstall -a|<package>: Uninstall a package
 	Options: