Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

apt-topten

by pboin (Deacon)
on Apr 05, 2004 at 21:32 UTC ( [id://342756]=sourcecode: print w/replies, xml ) Need Help??
Category: utility scripts
Author/Contact Info Paul Boin pboin@intertechconsulting.net http://www.intertechconsulting.net
Description: Apt-topten will help bandwidth-challenged folks make good decisions on what packages would be good candidates to remove before doing an 'apt-get upgrade'. It lists the top packages that apt wants by size, descending.
#!/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 %pac
+kages) {
        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;
}
Replies are listed 'Best First'.
Re: apt-topten
by muba (Priest) on Apr 05, 2004 at 21:34 UTC
    Aww, this hurts my eyes!
    Consider adding some <CODE> tags

      Done...

      The submission form has that entire text-entry marked as 'Code', which in my (obviously incorrect) understanding meant that it was for code only, and the tags were implied.

      There's also no preview option on 'Code' posts.

        Hmm, maybe my reply was a little bit too impulsive. Anyhow, let's have a look at it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://342756]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found