From 4298cc2332d0d0bf0ad7bc8cf186324da1d0f9c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 8 May 2021 14:57:08 +0200 Subject: Add caching of runs as a fallback in case of failure --- gen.pl | 58 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/gen.pl b/gen.pl index 6a0ec7c..2a6b6e8 100755 --- a/gen.pl +++ b/gen.pl @@ -16,11 +16,12 @@ use Getopt::Std; my %opt; $Getopt::Std::STANDARD_HELP_VERSION = 1; -getopts('d', \%opt); +getopts('cd', \%opt); sub main::HELP_MESSAGE { print <', 'out/index.html.tmp' or die "Could not open > out/index.html.tmp: $!"; +select $out; for (<$src>) { when (/^\.inc (.*)/) { - open my $h, '<', "inc/$1" or do { + open my $inc, '<', "inc/$1" or do { warn "Could not open inc/$1: $!"; next; }; print STDERR "INCLUDE: $_\n" if $opt{d}; - print $out $_ while <$h>; - close $h; + print $_ while <$inc>; + close $inc; } when (/^\.eval (.*)/) { print STDERR "EVAL: $1\n" if $opt{d}; { no strict; no warnings; no experimental; - select $out; eval $1; - select STDOUT; warn $@ if $@; } } when (/^\.run (.*)/) { - open my $h, '<', "run/$1" or do { - warn "Could not open run/$1: $!"; + if (not $opt{c}) { + open my $run, '-|', "run/$1" or do { + warn "Could not start run/$1: $!"; + next; + }; + open my $cache, '>', "cache/$1.tmp" or do { + warn "Could not open cache/$1.tmp: $!"; + next; + }; + print STDERR "RUN: $1\n" if $opt{d}; + my $i; + for (<$run>) { + $i++; + print; + print $cache $_; + } + close $cache; + close $run; + if ($i) { + move "cache/$1.tmp" => "cache/$1"; + next; + } + } + # Fall back to cached run + open my $cache, '<', "cache/$1" or do { + warn "Could not open cache/$1: $!"; next; }; - print STDERR "RUN: $1\n" if $opt{d}; - my $c = do { local $/; <$h> }; - close $h; - { - no strict; no warnings; no experimental; - local $0 = "run/$1"; - select $out; - eval $c; - select STDOUT; - warn $@ if $@; - } - # I guess you *could* just run the script in a separate process... + print STDERR "GET CACHED RUN: $1\n" if $opt{d}; + print for <$cache>; + close $cache; } default { - print $out $_; + print $_; } } +select STDOUT; close $out; close $src; -- cgit v1.2.3