file.py (1495B)
1 # standard imports 2 import os 3 import sys 4 import argparse 5 import logging 6 7 # local imports 8 from pylibswarm.io import Outputter 9 10 11 logging.basicConfig(level=logging.WARNING) 12 logg = logging.getLogger() 13 14 argparser = argparse.ArgumentParser() 15 argparser.add_argument('-n', action='store_true', help='skip newline at end of output') 16 argparser.add_argument('-b', action='store_true', help='output raw bytes') 17 argparser.add_argument('--prepend-hash', dest='prepend_hash', action='store_true', help='prepend hash bytes to chunk output (no effect without -o)') 18 argparser.add_argument('-o', type=str, help='chunk output location') 19 argparser.add_argument('-v', action='store_true', help='verbose output') 20 argparser.add_argument('-vv', action='store_true', help='very verbose output') 21 argparser.add_argument('file', nargs='?', type=str, help='file to hash') 22 largs = argparser.parse_args(sys.argv[1:]) 23 24 25 if largs.vv: 26 logg.setLevel(logging.DEBUG) 27 elif largs.v: 28 logg.setLevel(logging.INFO) 29 30 filepath = os.path.realpath(largs.file) 31 32 outputter = None 33 if largs.o: 34 outputter = Outputter(largs.o, prepend_hash=largs.prepend_hash) 35 36 def main(): 37 import swarm 38 39 if outputter: 40 r = swarm.filehash_path(filepath, outputter.dump) 41 else: 42 r = swarm.filehash_path(filepath) 43 44 if largs.b: 45 sys.stdout.buffer.write(r[:32]) 46 else: 47 s = '{}'.format(r[:32].hex()) 48 if not largs.n: 49 s += '\n' 50 sys.stdout.write(s) 51 52 53 if __name__ == '__main__': 54 main()