Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
unmerger.pl + packageSpace.pl
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Amemes
n00b
n00b


Joined: 15 Sep 2002
Posts: 13

PostPosted: Tue Apr 06, 2004 12:49 pm    Post subject: unmerger.pl + packageSpace.pl Reply with quote

Hi,
i made a simple script that prints a list of packages that you can remove if you want to remove for example kde.I also made a script that tells you how much space is used by a package.
You must use the scripts like this:
Code:
./scriptname.pl packagename

Some notes for unmerger.pl:
I have tested it with several packages and it works pretty good.The script uses emerge to find dependencies and it uses qpkg to get a list of all installed packages. If you have several versions of a package installed it only removes the newest version. If a package is not needed by any other package but it is in the world file the script tells you that.
Some notes for packageSpace:
If your search maches several packages the total space is also printed. You need IO::All for this script.To install IO::All use cpan.

I hope you like the scripts :) If you have any ideas ar suggestions don`t hesitate to tell them.

And here is the code for unmerger.pl:
Code:

#!/usr/bin/perl
$package=$ARGV[0];
open(WORLD,"< /var/cache/edb/world") or die "Couldn´t open world file\n";
while($line=<WORLD>) {
   chomp $line;
   if($line ne "") {
   push @world,$line;
   }
}
@outputPackage=qx/emerge --nospinner --columns -ep $package 2>\/dev\/null/;
@outputSystem=qx/emerge --nospinner --columns -ep system 2>\/dev\/null/;
@outputAllPackages=qx/qpkg -I -nc/;
@outputPackage = cleanEmergeOutput(@outputPackage);
@outputSystem = cleanEmergeOutput(@outputSystem);
@outputAllPackages = cleanOutputAllPackages(@outputAllPackages);
@listOfDeps = buildDepList(@outputAllPackages);

foreach $op (@outputPackage) {
   my $removable=1;
   foreach $os (@outputSystem) {
      if($op eq $os) {
         $removable=0;
         last;
      }    
   }
   if($removable==1) {
      push @removePackages,$op;
      push @removePackages,1;
   }
   
}

%removeList=@removePackages;
foreach $da (@listOfDeps) {
   foreach $rp (@removePackages) {
      if($rp eq $da) {
         $removeList{$rp}=0;
         last;
      }    
   }   
}

foreach $r (sort keys %removeList) {
   foreach $w (@world) {
      if($w eq $r) {
         if($removeList{$r}==1) {
            $removeList{$r}=2;
            last;
         }
      }
   }
   if($removeList{$r}==1) {
      print $r,"\n";   
   }
}

foreach $r (sort keys %removeList) {
   if($removeList{$r}==2) {
      print "No other deps but is in world file: ",$r,"\n";
   }
}

sub cleanEmergeOutput() {
   my @temp;
   foreach $line (@_){
      chomp $line;
      if($line!~s/^\[.+\]\s(.+?)\s.*\[.*$/$1/) {next;}
      push @temp,$line;
   }
   return @temp;
}

sub cleanOutputAllPackages() {
   my @temp;
   foreach $oap (@_) {
      my $notInDeps=1;
      chomp $oap;
      foreach $op (@outputPackage) {
         if($oap eq $op) {
                 $notInDeps=0;
            last;
                }
      }
      if($notInDeps==1) {
         push @temp,$oap;
      }                        
   }
   return @temp;
}

sub buildDepList() {
   my $arg="";
   my @temp;
   foreach $ap (@_) {
      $arg.=$ap." ";
   }
   @temp=qx/emerge --nospinner --columns -ep $arg 2>\/dev\/null/;
   foreach $doa (@temp) {
      chomp $doa;
      if($doa!~s/^\[.+\]\s(.+?)\s.*\[.*$/$1/) {next;}
      push @temp,$doa;
   }      
   return @temp;
}

And here is the code for packageSpace.pl:
Code:

#!/usr/bin/perl
use IO::All;
$pkgDB=io("/var/db/pkg");
$package=$ARGV[0];
#get all directories recursively
@pkgDB=$pkgDB->All_Dirs();
$totalSum=0.0;
foreach $dir (@pkgDB) {
   my @contents;
   if(($dir=~/$package/)&&($dir->all_files)) {
      my $packageName=$dir->name;
      $packageName=~s/^.+\/(.+)/$1/;
      print "Space used by ",$packageName,":\n";
      @contents=io($dir->name."\/"."CONTENTS")->slurp;
      my $spaceUsed=0.0;
      foreach my $contents (@contents) {
         chomp $contents;
         if($contents=~s/^obj\s(.+?)\s.+$/$1/) {
            my @stats=stat($contents);
             $spaceUsed+=($stats[12]*512);
          }else {
             $spaceUsed+=4096;
         }
      }
      $totalSum+=$spaceUsed;
      #printf " $spaceUsed/(1024*1024)," MB = ",$spaceUsed/1024," kb = ",$spaceUsed," byte\n";
      printf "\t%.2f MB = %.2f kb = %.0f\n",$spaceUsed/(1024*1024),$spaceUsed/1024,$spaceUsed;
   }
}
print  "Total space used:\n";
printf "\t%.2f MB\n",$totalSum/(1024*1024);


Last edited by Amemes on Thu Apr 08, 2004 1:21 pm; edited 1 time in total
Back to top
View user's profile Send private message
Vitaliy
Guru
Guru


Joined: 06 Apr 2004
Posts: 451
Location: Brooklyn, NY

PostPosted: Tue Apr 06, 2004 1:39 pm    Post subject: Reply with quote

Looks neat, I was looking for something like it... good job. :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum