summaryrefslogtreecommitdiff
path: root/bin/apt-why
blob: ed1e0a224a2b67f127cbe0d2620130cf2db48214 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env perl
use strict;
use warnings;
use AptPkg::Cache;
use Switch;
use Getopt::Long;

my $cache = AptPkg::Cache->new;
my $not = 0;
GetOptions(
	"not" => \$not,
);
my $packagename = shift or die("No packagename given");
my $package = $cache->{$packagename};

unless($package) {
	print STDERR "E: Package not found: $packagename\n";
	exit(100);
}

sub revdeps {
	my $rdepsref = shift;
	my $depsref;
	my $parent;

	foreach(@$rdepsref) {
		undef($parent);
		$parent->{name} = $_->{ParentPkg}{Name};
		$parent->{ver} = $_->{ParentVer}{VerStr};
		$parent->{current_state} = $cache->{$_->{ParentPkg}{Name}}->{CurrentState};
		$parent->{selected_state} = $cache->{$_->{ParentPkg}{Name}}->{SelectedState};
		$parent->{deptype} = $_->{DepType};
		$parent->{targetname} = $_->{TargetPkg}{Name};
		if ($_->{TargetVer}) {
			$parent->{depsign} = $_;
			$parent->{depver} = $_;
		}
		$depsref->{$parent->{name}} = $parent;
	}
	return($depsref);
}

sub why {
	my $revdeps = revdeps(shift);
	my $why;
	foreach(keys(%$revdeps)) {
		if ($revdeps->{$_}->{current_state} eq 'Installed' and $revdeps->{$_}->{deptype} !~ /^(Conflicts|Replaces|Obsoletes)$/) {
			$why->{$_} = $revdeps->{$_};
		}
	}
	return($why);
}

sub why_not {
	my $revdeps = revdeps(shift);
	my $why_not;
	foreach(keys(%$revdeps)) {
		if ($revdeps->{$_}->{current_state} eq 'Installed' and $revdeps->{$_}->{deptype} =~ /^(Conflicts|Replaces|Obsoletes)$/) {
			$why_not->{$_} = $revdeps->{$_};
		}
	}
	return($why_not);
}

sub short_states {
	my $pkg = shift;
	my $state = '';
	switch($pkg->{selected_state}) {
		case('Unkwnon') {
			$state .= ' ';
		} case('Install') {
			$state .= 'i';
		} case('Hold') {
			$state .= 'h';
		} case('DeInstall') {
			$state .= 'r';
		} case('Purge') {
			$state .= 'p';
		}
	}
	switch($pkg->{current_state}) {
		case('Installed') {
			$state .= 'i';
		} case('UnPacked') {
			$state .= 'u';
		} case('HalfConfigured') {
			$state .= 'f';
		} case('HalfInstalled') {
			$state .= 'h';
		} case('ConfigFiles') {
			$state .= 'c';
		} case('NotInstalled') {
			$state .= ' ';
		}
	}
	return($state);
}


sub print_deps {
	my $deps = shift;
	foreach(keys(%$deps)) {
		printf "%-2s  %-30s %-15s %s\n", short_states($deps->{$_}), $deps->{$_}->{name}, $deps->{$_}->{deptype}, $deps->{$_}->{targetname};
	}
}

if ($not) {
	print_deps(why_not($package->{RevDependsList}));
} else {
	print_deps(why($package->{RevDependsList}));
}

__END__

=head1 NAME

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

=head1 SYNOPSIS

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

=head1 DESCRIPTION

B<apt-why> displays various informations based on a I<package>s reverse
dependencies

The output is prefixed by two charactes, the former representing the desired
package state, the latter the current state.

The states are I<i>nstall, I<h>old, I<r>emove (deinstall), I<p>urge,
I<u>npacked, halI<f> configured, I<h>alf installed, I<c>onfigfiles installed.
An empty field means not installed.

If B<not> is specified, reverse dependencies prohibiting the install of
I<package> are shown. Else, reverse dependencies justifying the install
of I<package> are shown

=head1 AUTHOR

Daniel Friesel <derf@derf.homelinux.org>

=head1 LICENSE

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