comparison shasum.py @ 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
comparison
equal deleted inserted replaced
17:184ab1da1307 18:285848db0b52
114 exit_code = 0 114 exit_code = 0
115 if len(opts.files) == 1 and opts.files[0] == '-': 115 if len(opts.files) == 1 and opts.files[0] == '-':
116 for checkline in sys.stdin: 116 for checkline in sys.stdin:
117 if not checkline: 117 if not checkline:
118 continue 118 continue
119 r, fn = handle_checkline(opts, checkline) 119 r, fn, tag = handle_checkline(opts, checkline)
120 print("{}: {}".format(fn, r.upper())) 120 print("{}: {}: {}".format(tag, fn, r.upper()))
121 if r != "ok" and exit_code == 0: 121 if r != "ok" and exit_code == 0:
122 exit_code = 1 122 exit_code = 1
123 else: 123 else:
124 for fn in opts.files: 124 for fn in opts.files:
125 with io.open(fn, "rt", encoding="utf-8") as checkfile: 125 with io.open(fn, "rt", encoding="utf-8") as checkfile:
126 for checkline in checkfile: 126 for checkline in checkfile:
127 if not checkline: 127 if not checkline:
128 continue 128 continue
129 r, fn = handle_checkline(opts, checkline) 129 r, fn, tag = handle_checkline(opts, checkline)
130 print("{}: {}".format(fn, r.upper())) 130 print("{}: {}: {}".format(tag, fn, r.upper()))
131 if r != "ok" and exit_code == 0: 131 if r != "ok" and exit_code == 0:
132 exit_code = 1 132 exit_code = 1
133 return exit_code 133 return exit_code
134 134
135 135
136 def handle_checkline(opts, line): 136 def handle_checkline(opts, line):
137 """ 137 """
138 :return: a tuple with static "ok", "missing", or "failed" and the filename 138 :return: a tuple with static "ok", "missing", or "failed", the filename and
139 :rtype: tuple(str, str) 139 the digest used
140 :rtype: tuple(str, str, str)
140 141
141 """ 142 """
142 # determine checkfile format (BSD or coreutils) 143 # determine checkfile format (BSD or coreutils)
143 # BSD? 144 # BSD?
144 mo = re.search(r"\A(\S+)\s*\((.*)\)\s*=\s*(.+)\n?\Z", line) 145 mo = re.search(r"\A(\S+)\s*\((.*)\)\s*=\s*(.+)\n?\Z", line)
145 if mo: 146 if mo:
146 algo = algotag2algotype(mo.group(1)) 147 tag = mo.group(1)
148 algo = algotag2algotype(tag)
147 fn = mo.group(2) 149 fn = mo.group(2)
148 digest = mo.group(3) 150 digest = mo.group(3)
149 else: 151 else:
150 mo = re.search(r"([^\ ]+) [\*\ ]?(.+)\n?\Z", line) 152 mo = re.search(r"([^\ ]+) [\*\ ]?(.+)\n?\Z", line)
151 if mo: 153 if mo:
154 tag = opts.algorithm[1]
152 algo = opts.algorithm[0] 155 algo = opts.algorithm[0]
153 fn = mo.group(2) 156 fn = mo.group(2)
154 digest = mo.group(1) 157 digest = mo.group(1)
155 else: 158 else:
156 raise ValueError( 159 raise ValueError(
157 "improperly formatted digest line: {}".format(line)) 160 "improperly formatted digest line: {}".format(line))
158 try: 161 try:
159 with open(fn, "rb") as input: 162 with open(fn, "rb") as input:
160 d = compute_digest(algo, input) 163 d = compute_digest(algo, input)
161 if d.lower() == digest.lower(): 164 if d.lower() == digest.lower():
162 return ("ok", fn) 165 return ("ok", fn, tag)
163 else: 166 else:
164 return ("failed", fn) 167 return ("failed", fn, tag)
165 except EnvironmentError: 168 except EnvironmentError:
166 return ("missing", fn) 169 return ("missing", fn, tag)
167 170
168 171
169 def argv2algo(s): 172 def argv2algo(s):
170 """Convert a commane line algorithm specifier into a tuple with the 173 """Convert a commane line algorithm specifier into a tuple with the
171 type/factory of the digest and the algorithms tag for output purposes. 174 type/factory of the digest and the algorithms tag for output purposes.