changeset 135:dbf27681a1f6

Allow to put comments into the output with "--comment"
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 02 Jan 2025 23:15:10 +0100
parents d2c303695fb8
children db4fd1f64538
files cutils/treesum.py
diffstat 1 files changed, 16 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Thu Jan 02 23:03:20 2025 +0100
+++ b/cutils/treesum.py	Thu Jan 02 23:15:10 2025 +0100
@@ -46,6 +46,10 @@
         "--base64", action="store_true",
         help="Output checksums in base64 notation, not hexadecimal (OpenBSD).")
     aparser.add_argument(
+        "--comment", action="append", default=[],
+        help="Put given comment COMMENT into the output as \"COMMENT\"."
+             " Can be given more than once.")
+    aparser.add_argument(
         "--follow-directory-symlinks", action="store_true",
         dest="follow_directory_symlinks",
         help="Follow symbolic links to directories when walking a directory"
@@ -90,6 +94,7 @@
              algorithm="BLAKE2b-256",
              append_output=False,
              base64=False,
+             comment=[],
              follow_directory_symlinks=False,
              logical=None,
              mmap=None,
@@ -100,6 +105,7 @@
                    algorithm),
         append_output=append_output,
         base64=base64,
+        comment=comment,
         follow_directory_symlinks=follow_directory_symlinks,
         logical=logical,
         mmap=mmap,
@@ -131,12 +137,13 @@
         for d in opts.directories:
             generate_treesum_for_directory(
                 outfp, d, opts.algorithm, opts.mmap, opts.base64, opts.logical,
-                opts.follow_directory_symlinks)
+                opts.follow_directory_symlinks,
+                comment=opts.comment)
 
 
 def generate_treesum_for_directory(
         outfp, root, algorithm, use_mmap, use_base64, handle_root_logical,
-        follow_directory_symlinks):
+        follow_directory_symlinks, comment=None):
     """
 
     :param outfp: a *binary* file with a "write()" and a "flush()" method
@@ -162,6 +169,9 @@
     outfp.write(format_bsd_line("ISOTIMESTAMP", ts, None, False))
     outfp.flush()
 
+    if comment:
+        for line in comment:
+            outfp.write(format_bsd_line("COMMENT", None, line, False))
     outfp.write(format_bsd_line("ROOT", None, root, False))
     outfp.flush()
 
@@ -238,6 +248,10 @@
             value = value.encode("ascii")
         return b"%s = %s%s" % (digestname, value, ls)
     assert filename is not None
+    if digestname == b"COMMENT":
+        if not isinstance(filename, bytes):
+            filename = filename.encode("utf-8")
+        return b"COMMENT (%s)%s" % (filename, ls)
     if not isinstance(filename, bytes):
         filename = util.fsencode(filename)
     if value is None: