about summary refs log tree commit diff
path: root/src/nix-instantiate.in
blob: b5093391b2d2f7631d614576c0f08a180b402707 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/perl -w

use strict;
use FileHandle;
use File::Spec;
use Digest::MD5;

my $system = "@SYSTEM@";

my $outdir = File::Spec->rel2abs($ARGV[0]);
my $netdir = File::Spec->rel2abs($ARGV[1]);

my %donetmpls = ();

sub fetchFile {
    my $loc = shift;

    if ($loc =~ /^([+\w\d\.\/-]+)$/) {
	return $1;
    } elsif ($loc =~ /^url\((.*)\)$/) {
	my $url = $1;
	$url =~ /\/([^\/]+)$/ || die "invalid url $url";
	my $fn = "$netdir/$1";
	if (! -f $fn) {
	    print "fetching $url...\n";
	    system "cd $netdir; wget --quiet -N $url";
	    if ($? != 0) {
		unlink($fn);
		die;
	    }
	}
	return $fn;
    } else {
	die "invalid file specified $loc";
    }
}

sub hashFile {
    my $file = shift;
    open FILE, "< $file" or die "cannot open $file";
    # !!! error checking
    my $hash = Digest::MD5->new->addfile(*FILE)->hexdigest;
    close FILE;
    return $hash;
}

sub convert {
    my $descr = shift;

    if (defined $donetmpls{$descr}) {
        return $donetmpls{$descr};
    }

    my ($x, $dir, $fn) = File::Spec->splitpath($descr);

    print "$descr\n";

    my $IN = new FileHandle;
    my $OUT = new FileHandle;
    my $tmpfile = "$outdir/$fn-tmp";
    open $IN, "< $descr" or die "cannot open $descr";

# Descr([Bind("x", Str("y")), Bind("x", File("1234")), Bind("x", Pkg("1234"))])
# bindings alphabetisch gesorteerd

    my %bindings;

    while (<$IN>) {
        chomp;
	s/\s*#.*$//;
	next if (/^$/);

        if (/^(\w+)\s*=\s*([^\#\s]*)\s*(\#.*)?$/) {
            my ($name, $loc) = ($1, $2);
            my $file = fetchFile($loc);
            $file = File::Spec->rel2abs($file, $dir);
	    my $hash = hashFile($file);
            $bindings{$name} = "File(\"$hash\")";
        } elsif (/^(\w+)\s*<-\s*([+\w\d\.\/-]+)\s*(\#.*)?$/) {
            my $name = $1;
            my $file = $2;
            $file = File::Spec->rel2abs($file, $dir);
            $file = convert($file);
	    my $hash = hashFile($file);
            $bindings{$name} = "Pkg(\"$hash\")";
        } elsif (/^(\w+)\s*:\s*([+\w\d\.\/-]+)\s*(\#.*)?$/) {
            my $name = $1;
            my $value = $2;
            $bindings{$name} = "Str(\"$value\")";
        } else {
	    die "syntax error: $_";
        }
    }

    close $IN;

    $bindings{"system"} = "Str(\"$system\")";

    open $OUT, "| baffle -wt > $tmpfile" or die "cannot create $tmpfile";
    print $OUT "Descr([";
    my $first = 1;
    foreach my $name (sort (keys %bindings)) {
	if (!$first) { print $OUT ","; };
	print $OUT "Bind(\"$name\",$bindings{$name})";
	$first = 0;
    }
    print $OUT "])";
    close $OUT;

    my $hash = hashFile($tmpfile);

    my $outfile = "$outdir/$fn-$hash";
    rename($tmpfile, $outfile) or die "cannot rename $tmpfile to $outfile";

    $donetmpls{$descr} = $outfile;
    return $outfile;
}

for (my $i = 2; $i < scalar @ARGV; $i++) {
    convert(File::Spec->rel2abs($ARGV[$i]));
}