set -euo pipefail
case "$#.$1" in
[12].[^-]*) ;;
*) echo >&2 'bad usage'; exit 8 ;;
esac
url="$1"; shift
override_source="${1-}"
root="$(git rev-parse --show-toplevel)"
out="$root/crates/tor-netdoc/testdata2"
cd "$root"
rm -rf tmp
mkdir tmp
case "$override_source" in
'') wget -O tmp/artifacts.zip "$url" ;;
/*|./*) cp "$override_source" tmp/artifacts.zip ;;
*) echo >&2 'bad source override, should be a path'; exit 4 ;;
esac
cd tmp
unzip -q artifacts.zip chutney-net/nodes.\*
cd chutney-net
cd nodes.*.* cd 000a
rm -rf -- *.log diff-cache pid.* *.stderr *.stdout
chmod a+rx keys
chmod a+r keys/*
cat >README.md <<END
Test data from a CI run using chutney (checked-in constants, occasionally updated)
All files in this directory/module are automatically maintained.
Downloaded and massaged by $0
From CI data at <$url>
END
split-file () {
local f=$1
local headline=$2
perl -wpe '
BEGIN { $seq = 0; }
if (m{^'"$headline"' }) {
open STDOUT, ">", "$ARGV--".(++$seq) or die $!;
}
' "$f"
}
split-file v3-status-votes network-status-version
split-file cached-certs dir-key-certificate-version
patch_atline () {
local f=$1
perl -i -ne 'print unless /^\@/' "$f"
}
patch_atline cached-descriptors.new
patch_atline cached-microdescs.new
exec 4>generated_consts.rs
cat <<'END' >&4
#![doc = include_str!("README.md")]
//!
//! Constants are the file contents from CI, with names converted to SHOUTING_SNAKE_CASE.
//! TODO it would be nice if this data were more structured, like `testdata-live` is.
END
binaries_re='^ed25519|secret_onion_key_ntor'
find . -type f \
-name '[a-z]*' \
! -name \*.md \
! -name \*.rs \
-print0 \
| sort -z | xargs -0 perl >&4 -mautodie -we '
use strict;
our %files;
foreach my $f (@ARGV) {
$f =~ s{^\./}{};
my $d = \%files;
while ($f =~ s{^([^/]+)/}{}) {
$d = $d->{$1} //= {};
}
$d->{$f} = 1;
}
#use Data::Dumper;
#print STDERR Dumper(\%files);
# Write a Rust module containing constants for files collected in %$d.
sub pr ($$);
sub pr ($$) {
my ($dirpath, $d) = @_;
foreach my $k (sort keys %$d) {
my $path = $dirpath.$k;
my $v = $d->{$k};
my $name = $k;
$name =~ y/-./__/;
my $const = $name;
$const =~ y/-a-z/_A-Z/;
print "/// `testdata2/$path`\n";
if (ref $v) {
print "pub mod $name {\n";
pr("$path/", $v);
print "}\n";
} elsif ($k =~ m{'"$binaries_re"'}) { # binaries!
print "pub const $const: &[u8] = include_bytes!(\"$path\");\n" or die $!;
} else {
print "pub const $const: &str = include_str!(\"$path\");\n" or die $!;
}
}
}
pr("", \%files);
flush STDOUT or die $!; # detect ENOSPC, bugs.debian.org/28250, from 1998, still unfixed
'
chmod -R a+r,u+w .
rm -rf "$out"
mkdir "$out"
mv -- * "$out"
cd "$root"
rm -rf tmp
echo "Updated $out. Don't forget to git add."