]> hydra-www.ietfng.org Git - csdb/commitdiff
Remove stale cdb-digestrelative; new cdb-util
authorNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sat, 19 Feb 2022 09:37:52 +0000 (09:37 +0000)
committerNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sat, 19 Feb 2022 10:14:02 +0000 (10:14 +0000)
README.rst
cdb-digestrelative [deleted file]
cdb-util [new file with mode: 0755]
test-util.sh [new file with mode: 0755]

index d0d4cc7e9d9ca353e4b695944ccc387f28ac003b..97f4ecf39a58d48a0901df26c4dded3e82849275 100644 (file)
@@ -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 (executable)
index f9cfe34..0000000
+++ /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 (executable)
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 (executable)
index 0000000..fc2e670
--- /dev/null
@@ -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 <<HERE
+0  a
+1  ðŸŽµ
+\\2  a\\nb
+3  ./d
+4  /x
+HERE
+} | diff -u /dev/fd/3 - 3<<HERE
+0  foo/a
+1  foo/🎵
+\\2  foo/a\\nb
+3  foo/d
+4  /x
+HERE
+
+# Test digest-relativize
+cat >${LOG1} <<HERE
+0  a
+HERE
+cat >${LOG2} <<HERE
+1  /b
+HERE
+{ ${LUA} ./cdb-util digest-relativize <<HERE
+${LOG1}
+${LOG2}
+HERE
+} | diff -u /dev/fd/3 - 3<<HERE
+0  ${TMPDIR}/a
+1  /b
+HERE
+
+set +x
+echo "OK"