-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartrpmsort
executable file
·68 lines (60 loc) · 2.01 KB
/
smartrpmsort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/perl
use File::Basename;
use File::Copy;
use Data::Dumper;
my %packages;
if (not defined $ARGV[0]) {
print "smartrpmsort directory [subdirectory name]\n";
print "\n";
print "Keep only newest RPM package version in provided directory and\n";
print "move older ones to subdirectory\n";
exit 0;
}
my $dir = $ARGV[0];
my $archive_fn = defined $ARGV[1] ? $ARGV[1] : "archive";
my $archive_dir = $dir . "/" . $archive_fn;
my $ok = 1;
for my $path (glob "$dir/*.rpm") {
my $fn = basename $path;
if (not $fn =~ m@^(.+?)-(\d+(?:\.\d+)*)-(\d+)(?:.\w+)*?\.(?:x86_64|noarch)\.rpm@) {
print "Failed to parse filename '$fn'\n";
next;
}
my $name = $1;
my $version = $2 . ".0.0.0.0.0";
my $release = $3;
my @version_parts = split(/\./, $version);
@version_parts = splice @version_parts, 0, 5;
foreach my $part (@version_parts) { $part = sprintf("%05u", $part); }
my $sort_key = join("", @version_parts);
#print "'$fn' name='$name' ('$1','$2','$3','$release','$rhel') -> sort_key='$sort_key'\n";
if (defined $packages{$name}{$sort_key}) {
print "Duplicate sort key $sort_key for package $path ($packages{$name}{$sort_key} have the same sort key\n";
next;
}
$packages{$name}{$sort_key} = $fn;
}
#print Dumper(\%packages);
for my $name (sort keys %packages) {
my $versions_ref = $packages{$name};
my %versions = %$versions_ref;
my @keys = reverse sort keys %versions;
print "\n";
print "LEAVE $versions{$keys[0]} where it is\n";
for (my $i = 1; $i <= $#keys; $i++) {
if ( ! -d $archive_dir ) {
mkdir($archive_dir) or die "Failed to create directory $archive_dir: $!";
}
my $fn = basename $versions{$keys[$i]};
my $src = "$dir/$fn";
my $dest = "$archive_dir/$fn";
print " MOVE: $src => $dest: ";
if (move("$src", "$dest")) {
print "OK\n";
} else {
print "$!\n";
$ok = 0;
}
}
}
exit $ok ? 0 : 1;