commit 288ec9035edc0a4bfd30876851fdad7ac7cd8b3e
parent 6cacb642389f24d21ca83ee9e39f04d19db7efed
Author: nolash <dev@holbrook.no>
Date: Tue, 14 Sep 2021 23:06:34 +0200
Add hash prepend option to output
Diffstat:
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/pylibswarm/runnable/file.py b/pylibswarm/runnable/file.py
@@ -10,16 +10,19 @@ logg = logging.getLogger()
argparser = argparse.ArgumentParser()
argparser.add_argument('-n', action='store_true', help='skip newline at end of output')
argparser.add_argument('-b', action='store_true', help='output raw bytes')
+argparser.add_argument('--prepend-hash', dest='prepend_hash', action='store_true', help='prepend hash bytes to chunk output (no effect without -o)')
+argparser.add_argument('-o', type=str, help='chunk output location')
argparser.add_argument('-v', action='store_true', help='verbose output')
-argparser.add_argument('-o', type=str, help='chunk output directory')
+argparser.add_argument('-vv', action='store_true', help='very verbose output')
argparser.add_argument('file', nargs='?', type=str, help='file to hash')
largs = argparser.parse_args(sys.argv[1:])
class Outputter:
- def __init__(self, outdir):
+ def __init__(self, outdir, prepend_hash=False):
self.outdir = outdir
+ self.prepend_hash = prepend_hash
os.makedirs(self.outdir, exist_ok=True)
logg.info('outputter set to {}'.format(self.outdir))
@@ -28,19 +31,27 @@ class Outputter:
hsh_hex = hsh.hex()
fp = os.path.join(self.outdir, hsh_hex)
f = open(fp, 'wb')
+
+ l = len(data)
+ if self.prepend_hash:
+ l += len(hsh)
+ f.write(hsh)
f.write(data)
f.close()
- logg.debug('wrote {} chunk bytes for hash {}'.format(len(data), hsh_hex))
+
+ logg.debug('wrote {} bytes for chunk file {}'.format(l, hsh_hex))
-if largs.v:
+if largs.vv:
logg.setLevel(logging.DEBUG)
+elif largs.v:
+ logg.setLevel(logging.INFO)
filepath = os.path.realpath(largs.file)
outputter = None
if largs.o:
- outputter = Outputter(largs.o)
+ outputter = Outputter(largs.o, prepend_hash=largs.prepend_hash)
def main():
import swarm