pkg_tree.pl (1504B)
1 #!/usr/bin/env perl 2 use strict; 3 use warnings; 4 use Getopt::Std; 5 6 my $PROGRAM_NAME = $0; 7 my %deps_cache; 8 my %installed_cache; 9 my %displayed_cache; 10 my $package_name; 11 my %flags = (); 12 my $usage = "Usage: $PROGRAM_NAME [-i] [-f] package_name\ 13 -i: exclude (i)nstalled packages\ 14 -f: display (f)ull tree with repeating dependencies\ 15 "; 16 17 sub create_installed_cache { 18 my $packages_str = `pkg_info -Aqz`; 19 my @packages_vec = split('\n', $packages_str); 20 foreach (@packages_vec) { 21 $_ =~ s/--//g; 22 $_ =~ s/%.*$//g; 23 } 24 %installed_cache = map { $_ => 1 } @packages_vec; 25 } 26 27 sub print_deps { 28 my $packagename = shift; 29 my $level = shift; 30 $level++; 31 print(" " x $level); 32 print("`- $packagename"); 33 print(' (i)') if (exists($installed_cache{$packagename})); 34 print("\n"); 35 my $ret_str; 36 my @ret; 37 38 if ($deps_cache{$packagename}) { 39 @ret = @{$deps_cache{$packagename}}; 40 } else { 41 $ret_str = `pkg_info -f $packagename | grep '^\@depend' | cut -d ':' -f 2`; 42 @ret = split('\n', $ret_str); 43 foreach (@ret) { 44 $_ =~ s/\*/-/g; 45 $_ =~ s/,.*$//g; 46 $_ =~ s/>=.*$//g; 47 $_ =~ s/--//g; 48 $_ =~ s/-$//g; 49 } 50 @{$deps_cache{$packagename}} = @ret; 51 } 52 53 foreach my $r (@ret) { 54 if ($flags{i} && exists($installed_cache{$r})) { 55 next; 56 } 57 58 if ($flags{f} || !exists($displayed_cache{$r})) { 59 $displayed_cache{$r} = 1; 60 print_deps($r, $level); 61 } 62 } 63 } 64 65 getopts('if', \%flags) or die $usage; 66 67 if (@ARGV == 0) { 68 die $usage; 69 } 70 71 $package_name = $ARGV[0]; 72 73 create_installed_cache(); 74 print_deps($package_name, 0);