changeset 18:285848db0b52

When verifying/checking digests: also print the digest tag used
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 04 Dec 2020 23:22:30 +0100
parents 184ab1da1307
children 2f9e702e3f7a
files shasum.py
diffstat 1 files changed, 13 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/shasum.py	Fri Dec 04 23:06:48 2020 +0100
+++ b/shasum.py	Fri Dec 04 23:22:30 2020 +0100
@@ -116,8 +116,8 @@
         for checkline in sys.stdin:
             if not checkline:
                 continue
-            r, fn = handle_checkline(opts, checkline)
-            print("{}: {}".format(fn, r.upper()))
+            r, fn, tag = handle_checkline(opts, checkline)
+            print("{}: {}: {}".format(tag, fn, r.upper()))
             if r != "ok" and exit_code == 0:
                 exit_code = 1
     else:
@@ -126,8 +126,8 @@
                 for checkline in checkfile:
                     if not checkline:
                         continue
-                    r, fn = handle_checkline(opts, checkline)
-                    print("{}: {}".format(fn, r.upper()))
+                    r, fn, tag = handle_checkline(opts, checkline)
+                    print("{}: {}: {}".format(tag, fn, r.upper()))
                     if r != "ok" and exit_code == 0:
                         exit_code = 1
     return exit_code
@@ -135,20 +135,23 @@
 
 def handle_checkline(opts, line):
     """
-    :return: a tuple with static "ok", "missing", or "failed" and the filename
-    :rtype: tuple(str, str)
+    :return: a tuple with static "ok", "missing", or "failed", the filename and
+             the digest used
+    :rtype: tuple(str, str, str)
 
     """
     # determine checkfile format (BSD or coreutils)
     # BSD?
     mo = re.search(r"\A(\S+)\s*\((.*)\)\s*=\s*(.+)\n?\Z", line)
     if mo:
-        algo = algotag2algotype(mo.group(1))
+        tag = mo.group(1)
+        algo = algotag2algotype(tag)
         fn = mo.group(2)
         digest = mo.group(3)
     else:
         mo = re.search(r"([^\ ]+) [\*\ ]?(.+)\n?\Z", line)
         if mo:
+            tag = opts.algorithm[1]
             algo = opts.algorithm[0]
             fn = mo.group(2)
             digest = mo.group(1)
@@ -159,11 +162,11 @@
         with open(fn, "rb") as input:
             d = compute_digest(algo, input)
             if d.lower() == digest.lower():
-                return ("ok", fn)
+                return ("ok", fn, tag)
             else:
-                return ("failed", fn)
+                return ("failed", fn, tag)
     except EnvironmentError:
-        return ("missing", fn)
+        return ("missing", fn, tag)
 
 
 def argv2algo(s):