#!/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 $tabsign = "\t"; 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", $tabsign 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, 'tabwidth=i' => sub { $tabsign = ' ' x $_[1] }, '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 dependency, 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 dependency which mach the specified I (comma separated list). Right now, possible types are Depends, Suggests, Recommends. =item B<--tabwidth>=I Indent each recursion level with I spaces (by default: one tab) =back =head1 AUTHOR Daniel Friesel Ederf@derf.homelinux.orgE =head1 LICENSE 0. You just DO WHAT THE FUCK YOU WANT TO.