* Refactoring: put the manifest-reading code in a separate file.
This commit is contained in:
		
							parent
							
								
									cff6fd22eb
								
							
						
					
					
						commit
						06c5a7075d
					
				
					 3 changed files with 114 additions and 90 deletions
				
			
		| 
						 | 
					@ -4,9 +4,13 @@ bin_SCRIPTS = nix-collect-garbage \
 | 
				
			||||||
 | 
					
 | 
				
			||||||
noinst_SCRIPTS = nix-profile.sh
 | 
					noinst_SCRIPTS = nix-profile.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
install-exec-local:
 | 
					nix-pull nix-push: readmanifest.pm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					install-exec-local: readmanifest.pm
 | 
				
			||||||
	$(INSTALL) -d $(sysconfdir)/profile.d
 | 
						$(INSTALL) -d $(sysconfdir)/profile.d
 | 
				
			||||||
	$(INSTALL_PROGRAM) nix-profile.sh $(sysconfdir)/profile.d/nix.sh
 | 
						$(INSTALL_PROGRAM) nix-profile.sh $(sysconfdir)/profile.d/nix.sh
 | 
				
			||||||
 | 
						$(INSTALL) -d $(libexecdir)/nix
 | 
				
			||||||
 | 
						$(INSTALL_DATA) readmanifest.pm $(libexecdir)/nix 
 | 
				
			||||||
	$(INSTALL) -d $(sysconfdir)/nix
 | 
						$(INSTALL) -d $(sysconfdir)/nix
 | 
				
			||||||
# !!! don't overwrite local modifications
 | 
					# !!! don't overwrite local modifications
 | 
				
			||||||
	$(INSTALL_DATA) prebuilts.conf $(sysconfdir)/nix/prebuilts.conf
 | 
						$(INSTALL_DATA) prebuilts.conf $(sysconfdir)/nix/prebuilts.conf
 | 
				
			||||||
| 
						 | 
					@ -16,5 +20,5 @@ include ../substitute.mk
 | 
				
			||||||
EXTRA_DIST = nix-collect-garbage.in \
 | 
					EXTRA_DIST = nix-collect-garbage.in \
 | 
				
			||||||
 nix-pull.in nix-push.in nix-profile.sh.in \
 | 
					 nix-pull.in nix-push.in nix-profile.sh.in \
 | 
				
			||||||
 nix-prefetch-url.in nix-install-package.in \
 | 
					 nix-prefetch-url.in nix-install-package.in \
 | 
				
			||||||
 prebuilts.conf
 | 
					 prebuilts.conf readmanifest.pm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,9 @@
 | 
				
			||||||
#! /usr/bin/perl -w
 | 
					#! /usr/bin/perl -w -I@libexecdir@/nix
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use strict;
 | 
					use strict;
 | 
				
			||||||
use IPC::Open2;
 | 
					use IPC::Open2;
 | 
				
			||||||
use POSIX qw(tmpnam);
 | 
					use POSIX qw(tmpnam);
 | 
				
			||||||
 | 
					use readmanifest;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
my $tmpdir;
 | 
					my $tmpdir;
 | 
				
			||||||
do { $tmpdir = tmpnam(); }
 | 
					do { $tmpdir = tmpnam(); }
 | 
				
			||||||
| 
						 | 
					@ -13,93 +14,19 @@ my $conffile = "@sysconfdir@/nix/prebuilts.conf";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#END { unlink $manifest; rmdir $tmpdir; }
 | 
					#END { unlink $manifest; rmdir $tmpdir; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
my @srcpaths;
 | 
					 | 
				
			||||||
my @subs;
 | 
					 | 
				
			||||||
my @sucs;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
my $fullexpr = "[";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
sub processURL {
 | 
					 | 
				
			||||||
    my $url = shift;
 | 
					 | 
				
			||||||
    $url =~ s/\/$//;
 | 
					 | 
				
			||||||
    print "obtaining list of Nix archives at $url...\n";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    system "wget --cache=off '$url'/MANIFEST -O '$manifest' 2> /dev/null"; # !!! escape
 | 
					 | 
				
			||||||
    if ($?) { die "`wget' failed"; }
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
    open MANIFEST, "<$manifest";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    my $inside = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    my $storepath;
 | 
					 | 
				
			||||||
    my $narname;
 | 
					 | 
				
			||||||
    my $hash;
 | 
					 | 
				
			||||||
    my @preds;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    while (<MANIFEST>) {
 | 
					 | 
				
			||||||
        chomp;
 | 
					 | 
				
			||||||
        s/\#.*$//g;
 | 
					 | 
				
			||||||
        next if (/^$/);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (!$inside) {
 | 
					 | 
				
			||||||
            if (/^\{$/) { 
 | 
					 | 
				
			||||||
                $inside = 1;
 | 
					 | 
				
			||||||
                undef $storepath;
 | 
					 | 
				
			||||||
                undef $narname;
 | 
					 | 
				
			||||||
                undef $hash;
 | 
					 | 
				
			||||||
                @preds = ();
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            else { die "bad line: $_"; }
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            if (/^\}$/) {
 | 
					 | 
				
			||||||
                $inside = 0;
 | 
					 | 
				
			||||||
                my $fullurl = "$url/$narname";
 | 
					 | 
				
			||||||
#                print "$storepath\n";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                # Construct a Nix expression that fetches and unpacks a
 | 
					 | 
				
			||||||
                # Nix archive from the network.
 | 
					 | 
				
			||||||
                my $fetch =
 | 
					 | 
				
			||||||
                    "(import @datadir@/nix/corepkgs/fetchurl) " .
 | 
					 | 
				
			||||||
                    "{url = $fullurl; md5 = \"$hash\"; system = \"@system@\"}";
 | 
					 | 
				
			||||||
                my $nixexpr =
 | 
					 | 
				
			||||||
                    "((import @datadir@/nix/corepkgs/nar/unnar.nix) " .
 | 
					 | 
				
			||||||
                    "{narFile = ($fetch); outPath = \"$storepath\"; system = \"@system@\"}) ";
 | 
					 | 
				
			||||||
                $fullexpr .= $nixexpr; # !!! O(n^2)?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                push @srcpaths, $storepath;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                foreach my $p (@preds) {
 | 
					 | 
				
			||||||
                    push @sucs, $p;
 | 
					 | 
				
			||||||
                    push @sucs, $storepath;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            elsif (/^\s*StorePath:\s*(\/\S+)\s*$/) {
 | 
					 | 
				
			||||||
                $storepath = $1;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            elsif (/^\s*NarName:\s*(\S+)\s*$/) {
 | 
					 | 
				
			||||||
                $narname = $1;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            elsif (/^\s*MD5:\s*(\S+)\s*$/) {
 | 
					 | 
				
			||||||
                $hash = $1;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            elsif (/^\s*SuccOf:\s*(\/\S+)\s*$/) {
 | 
					 | 
				
			||||||
                push @preds, $1;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            else { die "bad line: $_"; }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    close MANIFEST;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Obtain URLs either from the command line or from a configuration file.
 | 
					# Obtain URLs either from the command line or from a configuration file.
 | 
				
			||||||
 | 
					my %storepaths2urls;
 | 
				
			||||||
 | 
					my %urls2hashes;
 | 
				
			||||||
 | 
					my %successors;
 | 
				
			||||||
 | 
					sub doURL {
 | 
				
			||||||
 | 
					    my $url = shift;
 | 
				
			||||||
 | 
					    processURL $manifest, $url, \%storepaths2urls, \%urls2hashes, \%successors;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
if (scalar @ARGV > 0) {
 | 
					if (scalar @ARGV > 0) {
 | 
				
			||||||
    while (@ARGV) {
 | 
					    while (@ARGV) {
 | 
				
			||||||
        my $url = shift @ARGV;
 | 
					        my $url = shift @ARGV;
 | 
				
			||||||
        processURL $url;
 | 
						doURL $url;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
} else {
 | 
					} else {
 | 
				
			||||||
    open CONFFILE, "<$conffile";
 | 
					    open CONFFILE, "<$conffile";
 | 
				
			||||||
| 
						 | 
					@ -107,12 +34,32 @@ if (scalar @ARGV > 0) {
 | 
				
			||||||
        chomp;
 | 
					        chomp;
 | 
				
			||||||
        if (/^\s*(\S+)\s*(\#.*)?$/) {
 | 
					        if (/^\s*(\S+)\s*(\#.*)?$/) {
 | 
				
			||||||
            my $url = $1;
 | 
					            my $url = $1;
 | 
				
			||||||
            processURL $url;
 | 
						    doURL $url;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    close CONFFILE;
 | 
					    close CONFFILE;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create a Nix expression for the substitutes.
 | 
				
			||||||
 | 
					my $fullexpr = "[";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					my @storepaths;
 | 
				
			||||||
 | 
					foreach my $storepath (keys %storepaths2urls) {
 | 
				
			||||||
 | 
					    # Construct a Nix expression that fetches and unpacks a
 | 
				
			||||||
 | 
					    # Nix archive from the network.
 | 
				
			||||||
 | 
					    my $url = $storepaths2urls{$storepath};
 | 
				
			||||||
 | 
					    my $hash = $urls2hashes{$url};
 | 
				
			||||||
 | 
					    my $fetch =
 | 
				
			||||||
 | 
						"(import @datadir@/nix/corepkgs/fetchurl) " .
 | 
				
			||||||
 | 
						"{url = $url; md5 = \"$hash\"; system = \"@system@\"}";
 | 
				
			||||||
 | 
					    my $nixexpr =
 | 
				
			||||||
 | 
						"((import @datadir@/nix/corepkgs/nar/unnar.nix) " .
 | 
				
			||||||
 | 
						"{narFile = ($fetch); outPath = \"$storepath\"; system = \"@system@\"}) ";
 | 
				
			||||||
 | 
					    $fullexpr .= $nixexpr; # !!! O(n^2)?
 | 
				
			||||||
 | 
					    push @storepaths, $storepath;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$fullexpr .= "]";
 | 
					$fullexpr .= "]";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -123,14 +70,13 @@ my $pid = open2(\*READ, \*WRITE, "nix-instantiate -") or die "cannot run nix-ins
 | 
				
			||||||
print WRITE $fullexpr;
 | 
					print WRITE $fullexpr;
 | 
				
			||||||
close WRITE;
 | 
					close WRITE;
 | 
				
			||||||
my $i = 0;
 | 
					my $i = 0;
 | 
				
			||||||
 | 
					my %substitutes;
 | 
				
			||||||
while (<READ>) {
 | 
					while (<READ>) {
 | 
				
			||||||
    chomp;
 | 
					    chomp;
 | 
				
			||||||
    die unless /^\//;
 | 
					    die unless /^\//;
 | 
				
			||||||
    my $subpath = $_;
 | 
					    my $subpath = $_;
 | 
				
			||||||
    die unless ($i < scalar @srcpaths);
 | 
					    die unless ($i < scalar @storepaths);
 | 
				
			||||||
    my $srcpath = $srcpaths[$i++];
 | 
					    $substitutes{$storepaths[$i++]} = $subpath;
 | 
				
			||||||
    push @subs, $srcpath;
 | 
					 | 
				
			||||||
    push @subs, $subpath;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
waitpid $pid, 0;
 | 
					waitpid $pid, 0;
 | 
				
			||||||
| 
						 | 
					@ -139,6 +85,7 @@ $? == 0 or die "nix-instantiate failed";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Register all substitutes.
 | 
					# Register all substitutes.
 | 
				
			||||||
print STDERR "registering substitutes...\n";
 | 
					print STDERR "registering substitutes...\n";
 | 
				
			||||||
 | 
					my @subs = %substitutes;
 | 
				
			||||||
while (scalar @subs > 0) {
 | 
					while (scalar @subs > 0) {
 | 
				
			||||||
    my $n = scalar @subs;
 | 
					    my $n = scalar @subs;
 | 
				
			||||||
    if ($n > 256) { $n = 256 };
 | 
					    if ($n > 256) { $n = 256 };
 | 
				
			||||||
| 
						 | 
					@ -151,6 +98,7 @@ while (scalar @subs > 0) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Register all successors.
 | 
					# Register all successors.
 | 
				
			||||||
print STDERR "registering successors...\n";
 | 
					print STDERR "registering successors...\n";
 | 
				
			||||||
 | 
					my @sucs = %successors;
 | 
				
			||||||
while (scalar @sucs > 0) {
 | 
					while (scalar @sucs > 0) {
 | 
				
			||||||
    my $n = scalar @sucs;
 | 
					    my $n = scalar @sucs;
 | 
				
			||||||
    if ($n > 256) { $n = 256 };
 | 
					    if ($n > 256) { $n = 256 };
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										72
									
								
								scripts/readmanifest.pm.in
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								scripts/readmanifest.pm.in
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,72 @@
 | 
				
			||||||
 | 
					use strict;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					sub processURL {
 | 
				
			||||||
 | 
					    my $manifest = shift;
 | 
				
			||||||
 | 
					    my $url = shift;
 | 
				
			||||||
 | 
					    my $storepaths2urls = shift;
 | 
				
			||||||
 | 
					    my $urls2hashes = shift;
 | 
				
			||||||
 | 
					    my $successors = shift;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    $url =~ s/\/$//;
 | 
				
			||||||
 | 
					    print "obtaining list of Nix archives at $url...\n";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    system "wget --cache=off '$url'/MANIFEST -O '$manifest' 2> /dev/null"; # !!! escape
 | 
				
			||||||
 | 
					    if ($?) { die "`wget' failed"; }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					    open MANIFEST, "<$manifest";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    my $inside = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    my $storepath;
 | 
				
			||||||
 | 
					    my $narname;
 | 
				
			||||||
 | 
					    my $hash;
 | 
				
			||||||
 | 
					    my @preds;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    while (<MANIFEST>) {
 | 
				
			||||||
 | 
					        chomp;
 | 
				
			||||||
 | 
					        s/\#.*$//g;
 | 
				
			||||||
 | 
					        next if (/^$/);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!$inside) {
 | 
				
			||||||
 | 
					            if (/^\{$/) { 
 | 
				
			||||||
 | 
					                $inside = 1;
 | 
				
			||||||
 | 
					                undef $storepath;
 | 
				
			||||||
 | 
					                undef $narname;
 | 
				
			||||||
 | 
					                undef $hash;
 | 
				
			||||||
 | 
					                @preds = ();
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
					            else { die "bad line: $_"; }
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            if (/^\}$/) {
 | 
				
			||||||
 | 
					                $inside = 0;
 | 
				
			||||||
 | 
					                my $fullurl = "$url/$narname";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$$storepaths2urls{$storepath} = $fullurl;
 | 
				
			||||||
 | 
							$$urls2hashes{$fullurl} = $hash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                foreach my $p (@preds) {
 | 
				
			||||||
 | 
							    $$successors{$p} = $storepath;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            elsif (/^\s*StorePath:\s*(\/\S+)\s*$/) {
 | 
				
			||||||
 | 
					                $storepath = $1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            elsif (/^\s*NarName:\s*(\S+)\s*$/) {
 | 
				
			||||||
 | 
					                $narname = $1;
 | 
				
			||||||
 | 
						    }
 | 
				
			||||||
 | 
					            elsif (/^\s*MD5:\s*(\S+)\s*$/) {
 | 
				
			||||||
 | 
					                $hash = $1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            elsif (/^\s*SuccOf:\s*(\/\S+)\s*$/) {
 | 
				
			||||||
 | 
					                push @preds, $1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else { die "bad line: $_"; }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    close MANIFEST;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					return 1;
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue