#!/usr/bin/perl -w # # Copyright (c) 2005 Gormand Pty Ltd. All rights reserved. This program # is free software; you can redistribute it and/or modify it under the # same terms as Perl itself. # # $Id: update_rpms_dir,v 1.14 2006/04/19 06:25:05 gordonr Exp $ # # Update RPMS directory with the latest from a given list of directories use RPM2; use Getopt::Long; my $rpm_flags = RPM2->vsf_nodsa; # Deal with missing DSA keys GetOptions(\%opt, "verbose", "debug", "rpms_dir=s", "target_dir=s"); # XXX - FIXME - These will be command line options my $rpms_dir = $opt{rpms_dir} || "/builds/RPMS"; my $target_dir = $opt{target_dir} || $rpms_dir; warn "Using $rpms_dir\n" if $opt{verbose}; my @repositories = qw( /builds/rpms/RPMS/i386/ /builds/rpms/RPMS/i586/ /builds/rpms/RPMS/i686/ /builds/rpms/RPMS/noarch/ /mirrors/centos/4/fasttrack/i386/RPMS/ /mirrors/centos/4/updates/i386/RPMS/ /mirrors/centos/4/os/i386/CentOS/RPMS/ ); chdir $rpms_dir or die "Couldn't chdir $rpms_dir"; opendir RPMS, '.' or die "Couldn't opendir $rpms_dir"; for my $file ( sort grep { /.rpm$/ } readdir RPMS ) { warn "Checking $file:\n" if $opt{verbose}; my $rpm = RPM2->open_package($file, $rpm_flags); for my $dir ( @repositories ) { my $newest = find_newest($rpm, $dir); if ($newest) { # XXX - FIXME - Should we try other repos, or stop here? print "rm $target_dir/$file; cp -p $dir/$newest $target_dir\n" } } } sub find_newest { my ($rpm, $dir) = @_; # XXX - FIXME - Need to cater for multiple architectures my $name = $rpm->name; $name =~ s/\+/\\+/g; # libstdc++ and friends unless (opendir DIR, $dir) { warn "Couldn't opendir $dir"; return; } my @rpms = sort grep { /^${name}-.*.rpm$/ } readdir DIR; closedir DIR; my $newest_file = undef; my $newest_hdr = undef; for my $file (@rpms) { warn "\tExamining $dir/$file\n" if $opt{debug}; my $candidate = RPM2->open_package("$dir/$file", $rpm_flags); if ($candidate->name ne $name) { warn "\tIgnoring $file - name doesn't match\n" if $opt{debug}; next; } if ($candidate->arch ne $rpm->arch) { warn "\tIgnoring $file - arch " . $candidate->arch . " want " . $rpm->arch . "\n" if $opt{debug}; next; } if ( ($candidate cmp $rpm) <= 0) { warn "\tIgnoring $file - older or same version\n" if $opt{debug}; next; } if (not defined $newest_hdr or ($candidate cmp $newest_hdr) > 0) { warn "\tNewest is $file\n" if $opt{debug}; $newest_file = $file; $newest_hdr = $candidate; } } return $newest_file; }