From 36c29627afc4d5efa88470d2f45875c0317a97f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sun, 13 Feb 2022 23:19:09 +0100 Subject: [PATCH] Fix broken -f option on single files instead of directories --- main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 53c679b..9578e16 100755 --- a/main.py +++ b/main.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 +# pylint: disable=consider-using-with +# (this code contains some IO stream juggling) from sys import stdin from sys import exit as sysexit from io import BytesIO from subprocess import Popen, PIPE +from pathlib import Path import click from blake3 import blake3 @@ -42,7 +45,7 @@ def get_input_stream(kwargs): if (text := kwargs["text"]) is not None: stream = ClosableStream(BytesIO(text.encode())) elif file := kwargs["file"]: - stream = get_deterministic_tar_stream(file) + stream = get_deterministic_stream(file) elif not stdin.isatty(): stream = ClosableStream(stdin.buffer) return stream @@ -57,8 +60,15 @@ class ClosableStream: return self._close_func() +def get_deterministic_stream(file): + if Path(file).is_dir(): + return get_deterministic_tar_stream(file) + + ifile = open(file, 'rb') + return ClosableStream(ifile, ifile.close) + + def get_deterministic_tar_stream(file): - # pylint: disable=consider-using-with cmd = ( 'tar', f'--blocking-factor={BUF_SIZE//512}',