More code

jdavidb on 2002-11-01T13:01:42

Finally decided to codify my semi-daily debian update procedure. Instead of a shell script, I decided to write a Perl program, and instead of just doing a bunch of system ""'s I decided to turn on taint-checking and use the multi-arg form. Also played with getpw* stuff, Time::Piece->strftime, and redirecting STDOUT, all of which are features I don't mess with much.

#!/usr/local/bin/perl5.8.0 -T

use warnings;
use strict;

use Time::Piece;

my $HOME = get_users_home();
my($cachefile, $packagefile) = ("$HOME/apt-cache", "$HOME/packages");
my $date = localtime->strftime("%Y%m%d");

$ENV{PATH} = "/bin:/usr/bin:/sbin:/usr/sbin";

system qw(apt-get update);
system qw(apt-get dist-upgrade);
system qw(apt-get autoclean);
open my $fh, ">$cachefile" or die "Can't open $cachefile: $!";
open STDOUT, ">&" . fileno($fh) or die "Can't redirect STDOUT: $!";
system qw(apt-cache dumpavail);
close $fh or warn "Couldn't close $cachefile: $!";
open $fh, $cachefile or die "Can't open $cachefile: $!";
open my $pfh, ">$packagefile" or die "Can't open $packagefile: $!";
while (defined(my $line = <$fh>))
{
  next unless $line =~ m/^Package: /;
  print $pfh $line;
}
link $packagefile, "$packagefile.$date" or warn "Couldn't link $packagefile to $packagefile.$date";

sub get_users_home
{
  return (getpwuid($<))[7];
}