1 |
#!/usr/bin/perl -w |
2 |
# |
3 |
# Copyright (c) 2005 Gormand Pty Ltd. All rights reserved. This program |
4 |
# is free software; you can redistribute it and/or modify it under the |
5 |
# same terms as Perl itself. |
6 |
# |
7 |
# $Id: update_rpms_dir,v 1.11 2006/03/06 04:02:12 gordonr Exp $ |
8 |
# |
9 |
# Update RPMS directory with the latest from a given list of directories |
10 |
|
11 |
use RPM2; |
12 |
use Getopt::Long; |
13 |
|
14 |
my $rpm_flags = RPM2->vsf_nodsa; # Deal with missing DSA keys |
15 |
|
16 |
GetOptions(\%opt, "verbose", "debug", "rpms_dir=s", "target_dir=s"); |
17 |
|
18 |
# XXX - FIXME - These will be command line options |
19 |
|
20 |
my $rpms_dir = $opt{rpms_dir} || "/builds/RPMS"; |
21 |
|
22 |
my $target_dir = $opt{target_dir} || $rpms_dir; |
23 |
|
24 |
warn "Using $rpms_dir\n" if $opt{verbose}; |
25 |
|
26 |
my @repositories = qw( |
27 |
/builds/rpms/RPMS/i386/ |
28 |
/builds/rpms/RPMS/i586/ |
29 |
/builds/rpms/RPMS/i686/ |
30 |
/builds/rpms/RPMS/noarch/ |
31 |
/mirrors/centos/4.2/updates/i386/RPMS/ |
32 |
/mirrors/centos/4.2/os/i386/CentOS/RPMS/ |
33 |
); |
34 |
|
35 |
chdir $rpms_dir or die "Couldn't chdir $rpms_dir"; |
36 |
|
37 |
opendir RPMS, '.' or die "Couldn't opendir $rpms_dir"; |
38 |
|
39 |
for my $file ( sort grep { /.rpm$/ } readdir RPMS ) |
40 |
{ |
41 |
warn "Checking $file:\n" if $opt{verbose}; |
42 |
|
43 |
my $rpm = RPM2->open_package($file, $rpm_flags); |
44 |
|
45 |
for my $dir ( @repositories ) |
46 |
{ |
47 |
my $newest = find_newest($rpm, $dir); |
48 |
|
49 |
if ($newest) |
50 |
{ |
51 |
# XXX - FIXME - Should we try other repos, or stop here? |
52 |
print "rm $target_dir/$file; cp -p $dir/$newest $target_dir\n" |
53 |
} |
54 |
} |
55 |
} |
56 |
|
57 |
sub find_newest |
58 |
{ |
59 |
my ($rpm, $dir) = @_; |
60 |
|
61 |
# XXX - FIXME - Need to cater for multiple architectures |
62 |
|
63 |
my $name = $rpm->name; |
64 |
$name =~ s/\+/\\+/g; # libstdc++ and friends |
65 |
|
66 |
opendir DIR, $dir or die "Couldn't opendir $dir"; |
67 |
my @rpms = sort grep { /^${name}-.*.rpm$/ } readdir DIR; |
68 |
closedir DIR; |
69 |
|
70 |
my $newest_file = undef; |
71 |
my $newest_hdr = undef; |
72 |
|
73 |
for my $file (@rpms) |
74 |
{ |
75 |
warn "\tExamining $dir/$file\n" if $opt{debug}; |
76 |
|
77 |
my $candidate = RPM2->open_package("$dir/$file", $rpm_flags); |
78 |
|
79 |
if ($candidate->name ne $name) |
80 |
{ |
81 |
warn "\tIgnoring $file - name doesn't match\n" if $opt{debug}; |
82 |
next; |
83 |
} |
84 |
|
85 |
if ( ($candidate cmp $rpm) <= 0) |
86 |
{ |
87 |
warn "\tIgnoring $file - older or same version\n" if $opt{debug}; |
88 |
next; |
89 |
} |
90 |
|
91 |
if (not defined $newest_hdr or ($candidate cmp $newest_hdr) > 0) |
92 |
{ |
93 |
warn "\tNewest is $file\n" if $opt{debug}; |
94 |
$newest_file = $file; |
95 |
$newest_hdr = $candidate; |
96 |
} |
97 |
} |
98 |
|
99 |
return $newest_file; |
100 |
} |