summaryrefslogtreecommitdiff
path: root/bin/apt-why
blob: c19aac27816925c6a646d7490396a5995e9ea05c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env perl
## Copyright © 2008-2009 by Daniel Friesel <derf@derf.homelinux.org>
## License: WTFPL <http://sam.zoy.org/wtfpl>
use strict;
use warnings;
use 5.010;
use AptPkg::Cache;
use Switch;
use Getopt::Long;

my $cache = AptPkg::Cache->new;
my $max_depth = 5;
my @deptypes;
GetOptions(
	'deptype=s' => \@deptypes,
	'depth=i' => \$max_depth,
);
my $packagename = shift or die("No packagename given");
my @known;

@deptypes = split(/,/, join(',', @deptypes));

sub revdeps {
	my $name = shift;
	my $package = $cache->{$name};
	my @return;
	my $rdeps = $package->{RevDependsList};
	foreach (@$rdeps) {
		if (
			$cache->{$_->{ParentPkg}{Name}}->{CurrentState} eq 'Installed' and (
				(not @deptypes and $_->{DepType} !~ /^(Conflicts|Replaces|Obsoletes)$/) or
				(@deptypes and "$_->{DepType}" ~~ @deptypes)
			)
		) {
			next if ($_->{ParentPkg}{Name} ~~ @known);
			push(@return, $_->{ParentPkg}{Name});
		}
	}
	push(@known, @return);
	return(@return);
}

sub recurse {
	my $name = shift;
	my $depth = (shift) + 1;
	printf("%s%s\n", "  " x ($depth-1), $name);
	return if ($depth >= $max_depth);
	recurse($_, $depth) foreach revdeps($name);
}

recurse($packagename, 0);

__END__

=head1 NAME

apt-why - filtered reverse dependency displayer using AptPkg::Cache

=head1 SYNOPSIS

B<apt-why> [ I<options> ] I<package>

=head1 DESCRIPTION

B<apt-why> recursively displays I<package>'s reverse dependency, thus
showing which packages require I<package> on the system.

=head1 OPTIONS

=over

=item B<--depth>=I<integer> (default: 5)

Specify the maximum recursion depth

=item B<--deptype>=I<types>

Only show reverse dependency which mach the specified I<types> (comma separated list).
Right now, possible types are Depends, Suggests, Recommends.

=head1 AUTHOR

Daniel Friesel <derf@derf.homelinux.org>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.