]> git.donarmstrong.com Git - wannabuild.git/blob - bin/keep-latest
Auto-committed schema changes.
[wannabuild.git] / bin / keep-latest
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib '/org/wanna-build/lib';
7 use WannaBuild; # for version compare
8
9 if (!@ARGV) {
10         print STDERR "Usage: $0 arch Packages1 Packages2 ..\n";
11         print STDERR "\n";
12         print STDERR "This perl scripts reads the Packages files given on the command line and\n";
13         print STDERR "outputs a Packages file which contians each package at most\n";
14         print STDERR "once, using the one with the highest version number.\n";
15         print STDERR "";
16         print STDERR "Since it only looks at Package:, Version: and Architecture:, it works with\n";
17         print STDERR "Sources files as well\n";
18         print STDERR "\n";
19         print STDERR "It throws out any package not arch \"all\" or the given architecture.\n";
20         print STDERR "Pass \"source\" to keep all entries.\n";
21
22         exit 1;
23 }
24
25 my %version;
26 my %data;
27
28 my $arch = shift @ARGV;
29
30 #Read in all data files:
31 for my $file (@ARGV) {
32         if (! -f $file) {
33                 next;
34         }
35         my $fh;
36         if ($file =~ /.gz$/) {
37                 open($fh, '-|', 'gzip', '-d', '-c', $file) 
38                         or die("Can't open pipe from gzip: $!");
39         } else {
40                 open($fh, '<', $file)
41                         or die("Can't open $file: $!");
42         }
43
44         local($/) = ""; # read in paragraph mode
45         while (<$fh>) {
46                 my( $version, $name, $architecture );
47                 $architecture="none"; # better to keep an entry too much than to delete an entry
48                 /^Package:\s*(\S+)$/mi and $name = $1;
49                 /^Version:\s*(\S+)$/mi and $version = $1;
50                 /^Architecture:\s*(\S+)$/mi and $architecture = $1;
51                 if (!defined $name or !defined $version) {
52                         warn "Stanza without Package or Version\n";
53                         next;
54                 }
55                 if ($arch ne "source" && defined $architecture && $architecture ne "all" && $architecture ne $arch) {
56                         next;
57                 }
58                 my $key = $name;
59
60                 if ((!exists $version{$key}) or version_less($version{$key},$version)) {
61                         $version{$key} = $version;
62                         $data{$key} = $_;
63                 }
64         }
65
66         close($fh);
67 }
68
69 #Now output the shortened list:
70 foreach (values %data) {
71         chomp; chomp; $_ .= "\n\n";
72         print;
73 }
74