commit 4c80e1f33311596b787a3f8d640d6b165ac65221
parent 9a9b0468cb0333b84eb07003e15cb028f4913fda
Author: bsandro <brian.drosan@gmail.com>
Date: Fri, 6 Aug 2021 02:35:21 +0300
Added -f argument to display full tree (including repeating dependencies);
Installed packages are marked with (i) now;
Package versions (and version requirements) are completely ignored from now on.
Diffstat:
M | pkg_tree.pl | | | 54 | ++++++++++++++++++++++++++++++++++++------------------ |
1 file changed, 36 insertions(+), 18 deletions(-)
diff --git a/pkg_tree.pl b/pkg_tree.pl
@@ -1,22 +1,26 @@
#!/usr/bin/env perl
use strict;
use warnings;
+use Getopt::Std;
my $PROGRAM_NAME = $0;
my %deps_cache;
my %installed_cache;
my %displayed_cache;
my $package_name;
-
-sub print_usage {
- print("Usage: $PROGRAM_NAME [-i] package_name\
- -i: exclude installed packages from tree
-");
-}
+my %flags = ();
+my $usage = "Usage: $PROGRAM_NAME [-i] [-f] package_name\
+ -i: exclude (i)nstalled packages\
+ -f: display (f)ull tree with repeating dependencies\
+";
sub create_installed_cache {
- my $packages_str = `pkg_info -Aq`;
+ my $packages_str = `pkg_info -Aqz`;
my @packages_vec = split('\n', $packages_str);
+ foreach (@packages_vec) {
+ $_ =~ s/--//g;
+ $_ =~ s/%.*$//g;
+ }
%installed_cache = map { $_ => 1 } @packages_vec;
}
@@ -25,32 +29,46 @@ sub print_deps {
my $level = shift;
$level++;
print(" " x $level);
- print("`- $packagename\n");
+ print("`- $packagename");
+ print(' (i)') if (exists($installed_cache{$packagename}));
+ print("\n");
my $ret_str;
my @ret;
+
if ($deps_cache{$packagename}) {
@ret = @{$deps_cache{$packagename}};
} else {
- $ret_str = `pkg_info -f $packagename | grep '^\@depend' | cut -d ':' -f 3`;
+ $ret_str = `pkg_info -f $packagename | grep '^\@depend' | cut -d ':' -f 2`;
@ret = split('\n', $ret_str);
+ foreach (@ret) {
+ $_ =~ s/\*/-/g;
+ $_ =~ s/,.*$//g;
+ $_ =~ s/>=.*$//g;
+ $_ =~ s/--//g;
+ $_ =~ s/-$//g;
+ }
@{$deps_cache{$packagename}} = @ret;
}
+
foreach my $r (@ret) {
- if (!exists($installed_cache{$r}) && !exists($displayed_cache{$r})) {
+ if ($flags{i} && exists($installed_cache{$r})) {
+ next;
+ }
+
+ if ($flags{f} || !exists($displayed_cache{$r})) {
$displayed_cache{$r} = 1;
print_deps($r, $level);
}
}
}
-if (@ARGV == 1) {
- $package_name = $ARGV[0]
-} elsif (@ARGV == 2 && $ARGV[0] eq "-i") {
- create_installed_cache();
- $package_name = $ARGV[1];
-} else {
- print_usage();
- exit();
+getopts('if', \%flags) or die $usage;
+
+if (@ARGV == 0) {
+ die $usage;
}
+$package_name = $ARGV[0];
+
+create_installed_cache();
print_deps($package_name, 0);