揮発性のメモ2

http://d.hatena.ne.jp/iww/

Debianのパッケージの依存関係を出力

引数で指定したパッケージの依存関係をツリーっぽく表示する

#!/usr/bin/perl

use strict; use warnings;

our %pkg;

sub printdepend {
    my $pname = $_[0];
    my $depth = $_[1];
    
    if( !exists($pkg{$pname}) ){
        $pkg{$pname} = $depth;
        print ' ' x $depth . "$pname\n";
        
        if( open( my $fh, "apt-cache show $pname |") ){
            while(<$fh>){
                my $s = "";
                $s = $1 if( m/^Depends:(.*)$/ );
                $s = $1 if( m/^Recommends:(.*)$/ );
                if( length $s ){
                    my @x = split(/[,|]/, $s);
                    for my $i ( @x ){
                        $i =~ s/\(.*\)//;
                        $i =~ s/^ *//;
                        $i =~ s/ *$//;
                        printdepend($i,$depth+1);
                    }
                }
            }
            close($fh);
        }
    }
}

printdepend($ARGV[0],0);
$ ./printdepend.pl zerofree
zerofree
 e2fslibs
  libc6
   libc-bin
   libgcc1
    gcc-4.4-base

もっと使いやすいツールどっかに落ちてないかな