#!/usr/bin/env perl ## Copyright © 2008-2009 by Daniel Friesel ## License: WTFPL use strict; use warnings; use 5.010; use AptPkg::Cache; use Getopt::Long; my $cache = AptPkg::Cache->new; my $max_depth = 5; my $all = 0; my @deptypes = qw/Depends Recommends Suggests/; my @known; sub revdeps { my $name = shift; my $package = $cache->{$name}; my @return; my $rdeps = $package->{RevDependsList}; foreach my $dependency ( @{$rdeps} ) { if ( not $dependency->{ParentPkg}{Name} ~~ @known and ( $all or $cache->{ $dependency->{ParentPkg}{Name} }->{CurrentState} eq 'Installed' ) and "$dependency->{DepType}" ~~ @deptypes ) { push( @return, $dependency->{ParentPkg}{Name} ); } } push( @known, @return ); return (@return); } sub recurse { my $name = shift; my $depth = (shift) + 1; printf( "%s%s\n", "\t" x ( $depth - 1 ), $name ); if ( $depth >= $max_depth ) { return; } foreach ( revdeps($name) ) { recurse( $_, $depth ); } return; } GetOptions( 'all!' => \$all, 'deptype=s' => sub { @deptypes = split( /,/, $_[1] ) }, 'depth=i' => \$max_depth, 'h|?|help' => sub { exec( 'perldoc', '-F', $0 ) or die("See perldoc -F $0\n") }, ); my $packagename = shift or die("Usage: $0 [options] ; see $0 --help\n"); if ( not defined $cache->{$packagename} ) { die("No such package: $packagename\n"); } if ( $cache->{$packagename}->{Flags} =~ /Essential/ ) { print "Essential: Yes\n"; } recurse( $packagename, 0 ); __END__ =head1 NAME apt-why - filtered reverse dependency displayer using AptPkg::Cache =head1 SYNOPSIS B [ I ] I =head1 DESCRIPTION B recursively displays I's reverse dependencies, thus showing which installed packages require I on the system. =head1 OPTIONS =over =item B<--all> Consider all packages, not just installed ones =item B<--depth>=I (default: 5) Specify the maximum recursion depth =item B<--deptype>=I Only show reverse dependencies which mach the specified I (comma separated list). Right now, possible types are Depends, Suggests, Recommends. =back =head1 EXIT STATUS Zero on success, non-zero on error (i.e. no such package). =head1 CONFIGURATION None. =head1 DEPENDENCIES This program requires the perl module B. On Debian, it is available via the package B. =head1 BUGS AND LIMITATIONS There may be problems with virtual packages and "Provides: " stuff. =head1 AUTHOR Daniel Friesel Ederf@finalrewind.orgE =head1 LICENSE 0. You just DO WHAT THE FUCK YOU WANT TO.