chiark / gitweb /
nailing-cargo: Partially converted to Perl..., now runs partway
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 3 May 2020 19:35:26 +0000 (20:35 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 3 May 2020 19:35:26 +0000 (20:35 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
nailing-cargo

index 3b673d83807ea4a71f356571d2664596abcf249a..9bcdfeda51e06675de36d16ec1d3a33a6c2d740d 100755 (executable)
@@ -42,11 +42,10 @@ use strict;
 use TOML;
 use POSIX;
 use Fcntl qw(LOCK_EX);
-use Cwd;
 
 my $self = $0;  $self =~ s{^.*/(?=.)}{};
 
-my $worksphere = fastcwd // die "$self: Cwd::fastcwd failed: $!\n";
+our $worksphere = getcwd() // die "$self: getcwd failed: $!\n";
 $worksphere =~ s{/[^/]+}{} or die "$self: cwd \`$worksphere' unsupported!\n";
 our $lockfile = "../.nailing-cargo-sphere.lock";
 
@@ -66,9 +65,10 @@ sub read_or_enoent ($) {
 sub toml_or_enoent ($$) {
     my ($f,$what) = @_;
     my $toml = read_or_enoent($f) // return;
-    my ($toml,$e) = from_toml($toml);
-    die "$self: parse TOML: $what: $f: $e\n" if defined $e;
-    $toml;
+    my ($v,$e) = from_toml($toml);
+    die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
+    die "$e ?" if length $e;
+    $v;
 }
 
 sub load1config ($) {
@@ -81,19 +81,19 @@ sub loadconfigs () {
     my $cfgleaf = ".nailing-cargo-cfg.toml";
     load1config("/etc/nailing-cargo/cfg.toml");
     load1config("$worksphere/$cfgleaf");
-    load1config("$HOME/$cfgleaf") if defined $HOME;
+    load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
 }
 
 sub getcfg ($$) {
     my ($k, $def) = @_;
     foreach my $cfg (@configs) {
-       my $v = $cfg{$k};
+       my $v = $cfg->{$k};
        return $v if defined $v;
     }
-    return $df;
+    return $def;
 }
 
-sub lock () {
+sub takelock () {
     for (;;) {
        open LOCK, ">", $lockfile or die "$0: open/create $lockfile: $!\n";
        flock LOCK, LOCK_EX or die "$0: lock $lockfile: $!\n";
@@ -103,7 +103,7 @@ sub lock () {
            next if $! == ENOENT;
            die "$0: stat $lockfile: $!\n";
        }
-       last if "@fstat[0..5]" == "@stat[0..5]";
+       last if "@fstat[0..5]" eq "@stat[0..5]";
     }
 }
 sub unlock () {
@@ -121,20 +121,20 @@ sub readnail () {
     if ($toml !~ m{^\s*\[/}m &&
        $toml !~ m{^[^\n\#]*\=}m &&
        # old non-toml syntax
-       $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = $2}mig) {
+       $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
        $toml =~ s{^}{[packages\]\n};
        $transformed = 1;
     }
     my $e;
     ($nail,$e) = from_toml($toml);
-    if (defined $e) {
+    if (!defined $nail) {
        if ($transformed) {
            $toml =~ s/^/    /mg;
            print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
        }
        die "$0: parse $nailfile: $e\n";
     }
-    die unless defined $nail;
+    die "$e ?" if length $e;
 }
 
 our %manifests;
@@ -143,7 +143,7 @@ our %packagemap;
 sub read_manifest ($) {
     my ($subdir) = @_;
     my $manifest = "../$subdir/Cargo.toml";
-    if (defined $manifests{$manifesst}) {
+    if (defined $manifests{$manifest}) {
        print STDERR
 "$self: warning: $subdir: specified more than once!\n";
        return undef;
@@ -190,10 +190,13 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
     last if m/^--$/;
 }
 
-lock();
+takelock();
 readnail();
 readorigs();
 
+use Data::Dumper;
+print STDERR Dumper(\%packagemap, \%manifests);
+
 __DATA__
 
 lock=${PWD%/*}/.nail.lock