#!/usr/bin/perl # Prints a list of the largest packages when # about to execute a 'apt-get upgrade', especially # helpful for us poor souls on dial-up. A quick # un-install of an un-wanted package can save # *tons* of download time. # # Paul Boin pboin@intertechconsulting.net $cvsId = q$Id: apt-topten,v 1.3 2004/04/05 21:34:57 paul Exp $; use strict; use warnings; use Getopt::Std; my %packages; my %options; my $i = 0; get_options(); get_packages(); &print_tops($options{n}); exit; sub print_tops(){ my $number = shift; my $package; my $i; my @sorted_by_size; print "Package" . ' ' x 23 . "Bytes\n"; print '=' x 60 . "\n"; foreach $package (sort {$packages{$b} <=> $packages{$a}} keys %packages) { push @sorted_by_size, ($package . (' ' x (30 - length($package))) . $packages{$package}); } for ($i=0; $i<$number; $i++){ print "$sorted_by_size[$i]\n"; } } sub get_packages { my $list = `apt-get -u -V --trivial-only upgrade 2> /dev/null`; my $capture = 0; while ($list =~ /^(.*)$/gm){ my $line = $1; if ($capture){ if ($line !~ /^\ \ \ /){$capture = 0;next;} $line =~ m/^\ \ \ (\S+).*?=>\ (.*)\)$/; $packages{$1} = get_package_size($1,$2); } if ($line =~ /will be upgraded/){$capture = 1;} } } sub get_package_size { my $name = shift; my $ver = shift; my $cmd = "apt-cache --no-all-versions show $name"; my $result = `$cmd`; $result =~ /^Size:\ (.*)$/m; return $1; } sub get_options { my $usage = qq{Usage: $0 options -h : This help message. -n : Number of packages to list (Default = 10) Prints a list of the largest packages that 'apt-get upgrade' wants to download. } . "\n"; getopts('n:h', \%options); die $usage if $options{h}; $options{n} ||= 15; }