# HG changeset patch # User Franz Glasner # Date 1642580074 -3600 # Node ID 5bec7a5d894aaca91899627f27b1ad91e6caabba # Parent dc198b6611497f2ae86c503cf6b52aab126a3680 Allow internal output redirection: print() always to explicitely given file objects diff -r dc198b661149 -r 5bec7a5d894a shasum.py --- a/shasum.py Wed Jan 19 01:04:36 2022 +0100 +++ b/shasum.py Wed Jan 19 09:14:34 2022 +0100 @@ -97,11 +97,13 @@ if not opts.algorithm: opts.algorithm = argv2algo("1") + opts.dest = None + return shasum(opts) def gen_opts(files=[], algorithm="SHA1", bsd=False, text_mode=False, - checklist=False, check=False): + checklist=False, check=False, dest=None): if text_mode: raise ValueError("text mode not supported") if checklist and check: @@ -112,7 +114,8 @@ bsd=bsd, checklist=checklist, check=check, - text_mode=False) + text_mode=False, + dest=dest) return opts @@ -145,7 +148,7 @@ True) else: for fn in opts.files: - out(sys.stdout, + out(opts.dest or sys.stdout, compute_digest_file(opts.algorithm[0], fn), fn, opts.algorithm[1], @@ -154,6 +157,7 @@ def verify_digests_with_checklist(opts): + dest = opts.dest or sys.stdout exit_code = 0 if not opts.files or (len(opts.files) == 1 and opts.files[0] == '-'): if PY2: @@ -166,7 +170,7 @@ pl = get_parsed_digest_line_from_checklist(opts.checklist, opts, None) if pl is None: exit_code = 1 - print("-: MISSING") + print("-: MISSING", file=dest) else: tag, algo, cl_filename, cl_digest = pl computed_digest = compute_digest_stream(algo, source) @@ -175,12 +179,12 @@ else: res = "FAILED" exit_code = 1 - print("{}: {}: {}".format(tag, "-", res)) + print("{}: {}: {}".format(tag, "-", res), file=dest) else: for fn in opts.files: pl = get_parsed_digest_line_from_checklist(opts.checklist, opts, fn) if pl is None: - print("{}: MISSING".format(fn)) + print("{}: MISSING".format(fn), file=dest) exit_code = 1 else: tag, algo, cl_filename, cl_digest = pl @@ -190,18 +194,19 @@ else: exit_code = 1 res = "FAILED" - print("{}: {}: {}".format(tag, fn, res)) + print("{}: {}: {}".format(tag, fn, res), file=dest) return exit_code def verify_digests_from_files(opts): + dest = opts.dest or sys.stdout exit_code = 0 if not opts.files or (len(opts.files) == 1 and opts.files[0] == '-'): for checkline in sys.stdin: if not checkline: continue r, fn, tag = handle_checkline(opts, checkline) - print("{}: {}: {}".format(tag, fn, r.upper())) + print("{}: {}: {}".format(tag, fn, r.upper()), file=dest) if r != "ok" and exit_code == 0: exit_code = 1 else: @@ -211,7 +216,7 @@ if not checkline: continue r, fn, tag = handle_checkline(opts, checkline) - print("{}: {}: {}".format(tag, fn, r.upper())) + print("{}: {}: {}".format(tag, fn, r.upper()), file=dest) if r != "ok" and exit_code == 0: exit_code = 1 return exit_code