From bee0369c8e82b85227ea1597131e2aee901f8dbb Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sat, 19 Feb 2022 09:37:52 +0000 Subject: [PATCH] Remove stale cdb-digestrelative; new cdb-util --- README.rst | 4 +- cdb-digestrelative | 40 ------------------- cdb-util | 97 ++++++++++++++++++++++++++++++++++++++++++++++ test-util.sh | 44 +++++++++++++++++++++ 4 files changed, 143 insertions(+), 42 deletions(-) delete mode 100755 cdb-digestrelative create mode 100755 cdb-util create mode 100755 test-util.sh diff --git a/README.rst b/README.rst index d0d4cc7..97f4ecf 100644 --- a/README.rst +++ b/README.rst @@ -54,9 +54,9 @@ Or, for all files under a path:: If we have a pile of digest files already, each of which contains digests of paths relative to its location, we can generate a database, ``${DB2}`` from them -with the assistance of the ``cdb-digestrelative`` tool:: +with the assistance of the ``cdb-util digest-relativize`` tool:: - find ${DIR} -type f -name SHA512SUMS -print0 | cdb-digestrelative --inul | cdb --db ${DB} addh + find ${DIR} -type f -name SHA512SUMS -print0 | cdb-util drel --inul | cdb --db ${DB} addh Revalidate A Path Observation ============================= diff --git a/cdb-digestrelative b/cdb-digestrelative deleted file mode 100755 index f9cfe34..0000000 --- a/cdb-digestrelative +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env luajit --- Also runs under lua5.3 - --- Read a stream of GNU digest filenames and concat them together, adjusting --- paths by prefixing the relative path of each digest file. - -local argparse = require "argparse" -local plpath = require "pl.path" - -local cdblib = require "cdblib" - -local argp = argparse("digestmangle", "GNU digest stream mangling tool") -argp:flag("--nul -0") - :description("NUL-terminate lines rather than newline") - :default(false) -argp:flag("--inul -1") - :description("Input is NUL terminated rather than newline") - :default(false) -argp:flag("--fnul -2") - :description("Input files are NUL terminated rather than newline") - :default(false) - -local args = argp:parse() - -local render = cdblib.renderers_for(args.nul, false) - -local function mklineiter(f) - return cdblib.iter_gnu_digest(cdblib.iter_lines_or_nul(args.fnul, f)) -end - -for fileline in cdblib.iter_lines_or_nul(args.inul)() do - local prefix = plpath.dirname(fileline) - - local f = io.open(fileline, "r") - for h, p in mklineiter(f)() do - local np = plpath.normpath(plpath.join(prefix, p)) - io.write(render(h, np)) - end - io.close(f) -end diff --git a/cdb-util b/cdb-util new file mode 100755 index 0000000..ab7b0ef --- /dev/null +++ b/cdb-util @@ -0,0 +1,97 @@ +#!/usr/bin/env luajit + +--------------------------------------------------------- Imports {{{ + +local argparse = require "argparse" +local plapp = require "pl.app" +local plpath = require "pl.path" + +plapp.require_here() +local cdblib = require "cdblib" + +local function argparse_flag_nul(c) + return c:flag("--nul -0") + :description("NUL-terminate output records") + :default(false) +end + +local function argparse_flag_inul(c) + return c:flag("--inul -1") + :description("Input is NUL-delimited, not newline") + :default(false) +end + +local argp = argparse("cdb-util", "CDB-adjacent Utility Multitool") +local function mksubcmd(cmdinit, body) + local cmd = argp:command() + cmd:action(function(args, name) + args._command = name + args._command_fn = body + end) + cmdinit(cmd) + return cmd +end + +----------------------------------------------------------------- }}} +------------------------------------------- Digest stream helpers {{{ + +local function path_prefix_digest_stream(pfx, iter, sink) + for h, p in iter() do sink(h, plpath.normpath(plpath.join(pfx, p))) end +end + +local function path_prefix_digest_stream_stdout(inul, onul, pfx, f) + local rend = cdblib.renderer_for(onul, false, + cdblib.mk_default_render_template()) + return path_prefix_digest_stream(pfx, + cdblib.iter_gnu_digest_stderr(cdblib.iter_lines_or_nul(inul, f)), + function(...) io.write(rend(...)) end) +end + +----------------------------------------------------------------- }}} +------------------------------------- Command: digest-prefix dpre {{{ + +mksubcmd(function(c) + c:name("digest-prefix dpre") + :description("Relativize a digest stream") + c:argument("prefix") + :description("New path prefix") + argparse_flag_nul(c) + argparse_flag_inul(c) + end, + function(args) + return path_prefix_digest_stream_stdout( + args.inul, args.nul, args.prefix, io.stdin) + end +) + +----------------------------------------------------------------- }}} +--------------------------------- Command: digest-relativize drel {{{ + +mksubcmd(function(c) + c:name("digest-relativize drel") + :description("Relativize and concatenate many digest streams") + argparse_flag_nul(c) + argparse_flag_inul(c) + argp:flag("--fnul -2") + :description("Input files are NUL terminated rather than newline") + :default(false) + end, + function(args) + for fileline in cdblib.iter_lines_or_nul(args.inul)() do + local prefix = plpath.dirname(fileline) + local f = assert(io.open(fileline, "r")) + path_prefix_digest_stream_stdout(args.fnul, args.nul, prefix, f) + io.close(f) + end + end +) + +----------------------------------------------------------------- }}} +--------------------------------------------- Top-level executive {{{ + +local args = argp:parse() +-- io.stderr:write((require "pl.pretty").write(args), "\n") + +args:_command_fn() + +----------------------------------------------------------------- }}} diff --git a/test-util.sh b/test-util.sh new file mode 100755 index 0000000..fc2e670 --- /dev/null +++ b/test-util.sh @@ -0,0 +1,44 @@ +#!/bin/zsh + +: ${LUA:=luajit} ${TMPDIR:=/tmp/cdbtest} +LOG1=${TMPDIR}/test-log1 +LOG2=${TMPDIR}/test-log2 +LOG3=${TMPDIR}/test-log3 +LOG4=${TMPDIR}/test-log4 + +set -e -u -x + +# Test digest-prefix +{ ${LUA} ./cdb-util digest-prefix foo <${LOG1} <${LOG2} <