update_ubuntu_sources.list.pl


Code Index:


NAME

update_ubuntu_sources.list.pl <ubuntu_release_name>


DESCRIPTION

Select fastest mirror for the Ubuntu </etc/apt/sources.list>.


COPYRIGHT

Copyright 2010 Etienne LEMEE <coding AT etilem DOT net>

This piece of code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


#!/usr/bin/perl -w

use strict;
$|=1;

@ARGV > 0 or die << "USAGE";

usage: $0 <ubuntu_release_name>

Select fastest mirror for the Ubuntu </etc/apt/sources.list>.
USAGE

my $sour = "/etc/apt/sources.list";
my $base = "archive.ubuntu.com";
my $rele = shift;

use LWP::Simple;
head (
    sprintf (
        "http://%s/ubuntu/dists/%s",
        $base, $rele,
    )
) or die "release '$rele' not found !";

my @serv = qw( fr ch cz uk de es it us au );

qx(which netselect) or die "'netselect' not found on your system !";

my $quer;
for (@serv) { $quer .= $_ . "." . $base . " " }

print
    "\nSelecting fastest mirror from : $quer\n",
    "This may take a while, please wait...\n";

$base = qx(netselect -t 20 $quer);
$base =~ s/^\s*-?\d+\s*(.+)$/$1/;
chomp $base;

print "Using: $base\n";

$quer = << "EOF";

    deb http://security.ubuntu.com/ubuntu $rele-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu $rele-security main restricted universe multiverse

    deb http://$base/ubuntu $rele main restricted universe multiverse
deb-src http://$base/ubuntu $rele main restricted universe multiverse

    deb http://$base/ubuntu $rele-updates main restricted universe multiverse
deb-src http://$base/ubuntu $rele-updates main restricted universe multiverse

    deb http://$base/ubuntu $rele-backports main restricted universe multiverse
deb-src http://$base/ubuntu $rele-backports main restricted universe multiverse

EOF

print $quer;

sub ask_exit {
    print shift;
    <STDIN> =~ /^n/i && die "Skipping...";
}

ask_exit("Write this content to $sour [Y/n] ? ");

open my $OUT, '>',  $sour or die $!;
print $OUT $quer;
close $OUT;

!system("apt-get update") or die;

ask_exit("Perform apt-get upgrade [Y/n] ? ");

!system("apt-get upgrade") or die;