2008-03-14 13:51:01 +00:00
|
|
|
use strict;
|
2012-12-04 18:56:31 +00:00
|
|
|
use Dpkg::Control;
|
2008-03-14 13:51:01 +00:00
|
|
|
use Dpkg::Deps;
|
2008-06-17 08:53:09 +01:00
|
|
|
use File::Basename;
|
2008-03-14 13:51:01 +00:00
|
|
|
|
|
|
|
my $packagesFile = shift @ARGV;
|
|
|
|
my $urlPrefix = shift @ARGV;
|
|
|
|
my @toplevelPkgs = @ARGV;
|
|
|
|
|
2012-12-04 18:56:31 +00:00
|
|
|
|
2008-03-14 13:51:01 +00:00
|
|
|
my %packages;
|
|
|
|
|
|
|
|
|
|
|
|
# Parse the Packages file.
|
|
|
|
open PACKAGES, "<$packagesFile" or die;
|
|
|
|
|
|
|
|
while (1) {
|
2012-12-04 18:56:31 +00:00
|
|
|
my $cdata = Dpkg::Control->new(type => CTRL_INFO_PKG);
|
|
|
|
last if not $cdata->parse(\*PACKAGES, $packagesFile);
|
2008-03-14 13:51:01 +00:00
|
|
|
die unless defined $cdata->{Package};
|
2012-12-04 18:56:31 +00:00
|
|
|
#print STDERR $cdata->{Package}, "\n";
|
2008-03-14 13:51:01 +00:00
|
|
|
$packages{$cdata->{Package}} = $cdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
close PACKAGES;
|
|
|
|
|
|
|
|
|
|
|
|
# Flatten a Dpkg::Deps dependency value into a list of package names.
|
|
|
|
sub getDeps {
|
|
|
|
my $deps = shift;
|
|
|
|
#print "$deps\n";
|
|
|
|
if ($deps->isa('Dpkg::Deps::AND')) {
|
|
|
|
my @res = ();
|
|
|
|
foreach my $dep ($deps->get_deps()) {
|
|
|
|
push @res, getDeps($dep);
|
|
|
|
}
|
|
|
|
return @res;
|
|
|
|
} elsif ($deps->isa('Dpkg::Deps::OR')) {
|
|
|
|
# Arbitrarily pick the first alternative.
|
|
|
|
return getDeps(($deps->get_deps())[0]);
|
|
|
|
} elsif ($deps->isa('Dpkg::Deps::Simple')) {
|
|
|
|
return ($deps->{package});
|
|
|
|
} else {
|
|
|
|
die "unknown dep type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-07 10:15:44 +01:00
|
|
|
# Process the "Provides" and "Replaces" fields to be able to resolve
|
|
|
|
# virtual dependencies.
|
2008-03-14 13:51:01 +00:00
|
|
|
my %provides;
|
|
|
|
|
2020-12-30 10:23:45 +00:00
|
|
|
foreach my $cdata (sort {$a->{Package} cmp $b->{Package}} (values %packages)) {
|
2013-05-07 10:15:44 +01:00
|
|
|
if (defined $cdata->{Provides}) {
|
|
|
|
my @provides = getDeps(Dpkg::Deps::deps_parse($cdata->{Provides}));
|
|
|
|
foreach my $name (@provides) {
|
|
|
|
#die "conflicting provide: $name\n" if defined $provides{$name};
|
|
|
|
#warn "provide by $cdata->{Package} conflicts with package with the same name: $name\n";
|
|
|
|
next if defined $packages{$name};
|
|
|
|
$provides{$name} = $cdata->{Package};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Treat "Replaces" like "Provides".
|
|
|
|
if (defined $cdata->{Replaces}) {
|
|
|
|
my @replaces = getDeps(Dpkg::Deps::deps_parse($cdata->{Replaces}));
|
|
|
|
foreach my $name (@replaces) {
|
|
|
|
next if defined $packages{$name};
|
|
|
|
$provides{$name} = $cdata->{Package};
|
|
|
|
}
|
2008-03-14 13:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Determine the closure of a package.
|
|
|
|
my %donePkgs;
|
|
|
|
my %depsUsed;
|
|
|
|
my @order = ();
|
|
|
|
|
|
|
|
sub closePackage {
|
|
|
|
my $pkgName = shift;
|
|
|
|
print STDERR ">>> $pkgName\n";
|
|
|
|
my $cdata = $packages{$pkgName};
|
2012-08-27 18:39:13 +01:00
|
|
|
|
2008-03-14 13:51:01 +00:00
|
|
|
if (!defined $cdata) {
|
|
|
|
die "unknown (virtual) package $pkgName"
|
|
|
|
unless defined $provides{$pkgName};
|
|
|
|
print STDERR "virtual $pkgName: using $provides{$pkgName}\n";
|
|
|
|
$pkgName = $provides{$pkgName};
|
|
|
|
$cdata = $packages{$pkgName};
|
|
|
|
}
|
2012-08-27 18:39:13 +01:00
|
|
|
|
2008-03-14 13:51:01 +00:00
|
|
|
die "unknown package $pkgName" unless defined $cdata;
|
|
|
|
return if defined $donePkgs{$pkgName};
|
|
|
|
$donePkgs{$pkgName} = 1;
|
|
|
|
|
|
|
|
if (defined $cdata->{Provides}) {
|
2012-12-04 18:56:31 +00:00
|
|
|
foreach my $name (getDeps(Dpkg::Deps::deps_parse($cdata->{Provides}))) {
|
2008-03-14 13:51:01 +00:00
|
|
|
$provides{$name} = $cdata->{Package};
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 18:39:13 +01:00
|
|
|
|
2008-03-14 13:51:01 +00:00
|
|
|
my @depNames = ();
|
2012-08-27 18:39:13 +01:00
|
|
|
|
2008-03-14 13:51:01 +00:00
|
|
|
if (defined $cdata->{Depends}) {
|
|
|
|
print STDERR " $pkgName: $cdata->{Depends}\n";
|
2012-12-04 18:56:31 +00:00
|
|
|
my $deps = Dpkg::Deps::deps_parse($cdata->{Depends});
|
2008-03-14 13:51:01 +00:00
|
|
|
die unless defined $deps;
|
|
|
|
push @depNames, getDeps($deps);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $cdata->{'Pre-Depends'}) {
|
|
|
|
print STDERR " $pkgName: $cdata->{'Pre-Depends'}\n";
|
2012-12-04 18:56:31 +00:00
|
|
|
my $deps = Dpkg::Deps::deps_parse($cdata->{'Pre-Depends'});
|
2008-03-14 13:51:01 +00:00
|
|
|
die unless defined $deps;
|
|
|
|
push @depNames, getDeps($deps);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $depName (@depNames) {
|
|
|
|
closePackage($depName);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @order, $pkgName;
|
|
|
|
$depsUsed{$pkgName} = \@depNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 Debian packages constituting the closure of: @toplevelPkgs\n\n";
|
|
|
|
print "{fetchurl}:\n\n";
|
|
|
|
print "[\n\n";
|
|
|
|
|
|
|
|
# Output the packages in strongly connected components.
|
|
|
|
my %done;
|
|
|
|
my %forward;
|
|
|
|
my $newComponent = 1;
|
|
|
|
foreach my $pkgName (@order) {
|
|
|
|
$done{$pkgName} = 1;
|
|
|
|
my $cdata = $packages{$pkgName};
|
|
|
|
my @deps = @{$depsUsed{$pkgName}};
|
|
|
|
foreach my $dep (@deps) {
|
|
|
|
$dep = $provides{$dep} if defined $provides{$dep};
|
|
|
|
$forward{$dep} = 1 unless defined $done{$dep};
|
|
|
|
}
|
|
|
|
delete $forward{$pkgName};
|
|
|
|
|
|
|
|
print " [\n\n" if $newComponent;
|
|
|
|
$newComponent = 0;
|
2008-06-17 08:53:09 +01:00
|
|
|
|
|
|
|
my $origName = basename $cdata->{Filename};
|
|
|
|
my $cleanedName = $origName;
|
|
|
|
$cleanedName =~ s/~//g;
|
2012-08-27 18:39:13 +01:00
|
|
|
|
2008-03-14 13:51:01 +00:00
|
|
|
print " (fetchurl {\n";
|
2023-02-20 00:38:31 +00:00
|
|
|
print " url = \"$urlPrefix/$cdata->{Filename}\";\n";
|
2008-03-14 13:51:01 +00:00
|
|
|
print " sha256 = \"$cdata->{SHA256}\";\n";
|
2008-06-17 08:53:09 +01:00
|
|
|
print " name = \"$cleanedName\";\n" if $cleanedName ne $origName;
|
2008-03-14 13:51:01 +00:00
|
|
|
print " })\n";
|
|
|
|
print "\n";
|
|
|
|
|
|
|
|
if (keys %forward == 0) {
|
|
|
|
print " ]\n\n";
|
|
|
|
$newComponent = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $pkgName (@order) {
|
|
|
|
my $cdata = $packages{$pkgName};
|
|
|
|
}
|
|
|
|
|
|
|
|
print "]\n";
|
|
|
|
|
|
|
|
if ($newComponent != 1) {
|
|
|
|
print STDERR "argh: ", keys %forward, "\n";
|
|
|
|
exit 1;
|
|
|
|
}
|