2008-04-09 10:06:16 +00:00
|
|
|
use strict;
|
|
|
|
use XML::Simple;
|
2011-12-27 12:56:07 +00:00
|
|
|
use List::Util qw(min);
|
2008-04-09 10:06:16 +00:00
|
|
|
|
2011-10-11 13:22:09 +00:00
|
|
|
my @packagesFiles = ();
|
|
|
|
my @urlPrefixes = ();
|
|
|
|
|
|
|
|
# rpm-closure.pl (<package-file> <url-prefix>)+ <toplevel-pkg>+
|
|
|
|
|
|
|
|
while(-f $ARGV[0]) {
|
|
|
|
my $packagesFile = shift @ARGV;
|
|
|
|
my $urlPrefix = shift @ARGV;
|
|
|
|
push(@packagesFiles, $packagesFile);
|
|
|
|
push(@urlPrefixes, $urlPrefix);
|
|
|
|
}
|
|
|
|
|
2011-12-27 12:56:07 +00:00
|
|
|
|
|
|
|
sub rpmvercmp {
|
|
|
|
my ($version1, $version2) = @_;
|
|
|
|
my @vercmps1 = split /\./, $version1;
|
|
|
|
my @vercmps2 = split /\./, $version2;
|
|
|
|
my $l1 = scalar(@vercmps1);
|
|
|
|
my $l2 = scalar(@vercmps2);
|
|
|
|
my $l = min($l1, $l2);
|
|
|
|
|
|
|
|
for(my $i=0; $i<$l; $i++) {
|
|
|
|
my $v1 = $vercmps1[$i];
|
|
|
|
my $v2 = $vercmps2[$i];
|
|
|
|
|
|
|
|
if($v1 =~ /^[0-9]*$/ && $v2 =~ /^[0-9]*$/) {
|
2015-02-12 16:00:31 +00:00
|
|
|
if ( int($v1) > int($v2) ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ( int($v1) < int($v2) ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( $v1 gt $v2 ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ( $v1 lt $v2 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 12:56:07 +00:00
|
|
|
}
|
|
|
|
if($l1 == $l2) {
|
|
|
|
return 0;
|
|
|
|
} elsif ($l1 > $l2) {
|
|
|
|
return 1;
|
|
|
|
} elsif ($l1 < $l2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-09 10:06:16 +00:00
|
|
|
my @toplevelPkgs = @ARGV;
|
|
|
|
|
2008-04-11 11:52:40 +00:00
|
|
|
my @archs = split ' ', ($ENV{'archs'} or "");
|
|
|
|
|
2011-10-11 13:22:09 +00:00
|
|
|
my %pkgs;
|
|
|
|
for (my $i = 0; $i < scalar(@packagesFiles); $i++) {
|
|
|
|
my $packagesFile = $packagesFiles[$i];
|
|
|
|
print STDERR "parsing packages in $packagesFile...\n";
|
2008-04-09 10:06:16 +00:00
|
|
|
|
2011-10-11 13:22:09 +00:00
|
|
|
my $xml = XMLin($packagesFile, ForceArray => ['package', 'rpm:entry', 'file'], KeyAttr => []) or die;
|
2008-04-09 10:06:16 +00:00
|
|
|
|
2011-10-11 13:22:09 +00:00
|
|
|
print STDERR "$packagesFile contains $xml->{packages} packages\n";
|
2008-04-09 10:06:16 +00:00
|
|
|
|
2011-10-11 13:22:09 +00:00
|
|
|
foreach my $pkg (@{$xml->{'package'}}) {
|
|
|
|
if (scalar @archs > 0) {
|
|
|
|
my $arch = $pkg->{arch};
|
|
|
|
my $found = 0;
|
|
|
|
foreach my $a (@archs) { $found = 1 if $arch eq $a; }
|
|
|
|
next if !$found;
|
|
|
|
}
|
|
|
|
if (defined $pkgs{$pkg->{name}}) {
|
|
|
|
my $earlierPkg = $pkgs{$pkg->{name}};
|
|
|
|
print STDERR "WARNING: duplicate occurrence of package $pkg->{name}\n";
|
2011-12-27 12:56:07 +00:00
|
|
|
# <version epoch="0" ver="1.28.0" rel="2.el6"/>
|
2012-02-02 09:53:45 +00:00
|
|
|
my $cmp = rpmvercmp($pkg->{'version'}->{ver}, $earlierPkg->{'version'}->{ver});
|
|
|
|
if ($cmp > 0 || ($cmp == 0 && rpmvercmp($pkg->{'version'}->{rel}, $earlierPkg->{'version'}->{rel})>0)) {
|
|
|
|
print STDERR "WARNING: replaced package $pkg->{name} (".$earlierPkg->{'version'}->{ver}." ".$earlierPkg->{'version'}->{rel}.") with newer one (".$pkg->{'version'}->{ver}." ".$pkg->{'version'}->{rel}.")\n";
|
2011-10-11 13:22:09 +00:00
|
|
|
$pkg->{urlPrefix} = $urlPrefixes[$i];
|
|
|
|
$pkgs{$pkg->{name}} = $pkg;
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
$pkg->{urlPrefix} = $urlPrefixes[$i];
|
|
|
|
$pkgs{$pkg->{name}} = $pkg;
|
2008-04-11 11:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-09 10:06:16 +00:00
|
|
|
my %provides;
|
2016-03-01 10:01:00 +00:00
|
|
|
PKG: foreach my $pkgName (sort(keys %pkgs)) {
|
2014-09-05 10:37:07 +00:00
|
|
|
#print STDERR "looking at $pkgName\n";
|
2008-04-11 11:52:40 +00:00
|
|
|
my $pkg = $pkgs{$pkgName};
|
2015-02-12 16:00:31 +00:00
|
|
|
|
2015-02-12 16:41:58 +00:00
|
|
|
# Skip packages that conflict with a required package.
|
|
|
|
my $conflicts = $pkg->{format}->{'rpm:conflicts'}->{'rpm:entry'} // [];
|
|
|
|
foreach my $conflict (@{$conflicts}) {
|
|
|
|
next if $conflict->{flags} // "" eq "LT" || $conflict->{flags} // "" eq "LE";
|
|
|
|
#print STDERR " $pkgName conflicts with $conflict->{name}\n";
|
|
|
|
if (grep { $_ eq $conflict->{name} } @toplevelPkgs) {
|
|
|
|
print STDERR "skipping package $pkgName because it conflicts with a required package\n";
|
|
|
|
next PKG;
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 16:00:31 +00:00
|
|
|
|
2008-04-11 11:52:40 +00:00
|
|
|
my $provides = $pkg->{format}->{'rpm:provides'}->{'rpm:entry'} or die;
|
|
|
|
foreach my $req (@{$provides}) {
|
2015-02-12 16:41:58 +00:00
|
|
|
#print STDERR " $pkgName provides $req->{name}\n";
|
|
|
|
#die "multiple provides for $req->{name}" if defined $provides{$req->{name}};
|
2008-04-11 11:52:40 +00:00
|
|
|
$provides{$req->{name}} = $pkgName;
|
2008-04-09 10:06:16 +00:00
|
|
|
}
|
2008-04-11 11:52:40 +00:00
|
|
|
|
2008-04-09 10:06:16 +00:00
|
|
|
if (defined $pkg->{format}->{file}) {
|
|
|
|
foreach my $file (@{$pkg->{format}->{file}}) {
|
|
|
|
#print STDERR " provides file $file\n";
|
|
|
|
$provides{$file} = $pkgName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my %donePkgs;
|
|
|
|
my @needed = ();
|
|
|
|
|
|
|
|
sub closePackage {
|
|
|
|
my $pkgName = shift;
|
|
|
|
|
|
|
|
return if defined $donePkgs{$pkgName};
|
|
|
|
$donePkgs{$pkgName} = 1;
|
2015-02-12 16:00:31 +00:00
|
|
|
|
2008-04-09 10:06:16 +00:00
|
|
|
print STDERR ">>> $pkgName\n";
|
2015-02-12 16:00:31 +00:00
|
|
|
|
2008-04-11 11:52:40 +00:00
|
|
|
my $pkg = $pkgs{$pkgName} or die "package $pkgName doesn't exist";
|
2008-04-09 10:06:16 +00:00
|
|
|
|
2008-11-26 11:14:39 +00:00
|
|
|
my $requires = $pkg->{format}->{'rpm:requires'}->{'rpm:entry'} || [];
|
2008-04-09 10:06:16 +00:00
|
|
|
|
|
|
|
my @deps = ();
|
2008-04-11 11:52:40 +00:00
|
|
|
foreach my $req (@{$requires}) {
|
|
|
|
next if $req->{name} =~ /^rpmlib\(/;
|
2015-02-12 16:41:58 +00:00
|
|
|
#print STDERR " needs $req->{name}\n";
|
2008-04-11 11:52:40 +00:00
|
|
|
my $provider = $provides{$req->{name}};
|
2008-04-09 10:06:16 +00:00
|
|
|
if (!defined $provider) {
|
2008-04-11 11:52:40 +00:00
|
|
|
print STDERR " WARNING: no provider for $req->{name}\n";
|
2008-04-09 10:06:16 +00:00
|
|
|
next;
|
|
|
|
}
|
2015-02-12 16:41:58 +00:00
|
|
|
#print STDERR " satisfied by $provider\n";
|
2015-02-12 16:00:31 +00:00
|
|
|
push @deps, $provider;
|
2008-04-09 10:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closePackage($_) foreach @deps;
|
|
|
|
|
|
|
|
push @needed, $pkgName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach my $pkgName (@toplevelPkgs) {
|
|
|
|
closePackage $pkgName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Generate the output Nix expression.
|
|
|
|
print "# This is a generated file. Do not modify!\n";
|
|
|
|
print "# Following are the RPM packages constituting the closure of: @toplevelPkgs\n\n";
|
|
|
|
print "{fetchurl}:\n\n";
|
|
|
|
print "[\n\n";
|
|
|
|
|
|
|
|
foreach my $pkgName (@needed) {
|
2008-04-11 11:52:40 +00:00
|
|
|
my $pkg = $pkgs{$pkgName};
|
2008-04-09 10:06:16 +00:00
|
|
|
print " (fetchurl {\n";
|
2011-10-11 13:22:09 +00:00
|
|
|
print " url = $pkg->{urlPrefix}/$pkg->{location}->{href};\n";
|
2009-06-09 14:25:45 +00:00
|
|
|
if ($pkg->{checksum}->{type} eq "sha") {
|
|
|
|
print " sha1 = \"$pkg->{checksum}->{content}\";\n";
|
|
|
|
} elsif ($pkg->{checksum}->{type} eq "sha256") {
|
|
|
|
print " sha256 = \"$pkg->{checksum}->{content}\";\n";
|
|
|
|
} else {
|
|
|
|
die "unsupported hash type";
|
|
|
|
}
|
2008-04-09 10:06:16 +00:00
|
|
|
print " })\n";
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
print "]\n";
|