Use argparse in tarsum.py

This commit is contained in:
TKE
2021-03-25 18:35:29 +01:00
parent 2f195ac530
commit 7275391d0a

View File

@@ -1,32 +1,18 @@
#!/usr/bin/python3 -u #!/usr/bin/python3 -u
import tarfile import tarfile
import sys
import hashlib import hashlib
#ask for parameters import argparse
#1 = hashtype or md5 by defualt
#2 = filename of tarfile
try:
if len(sys.argv) == 3:
hashtype=sys.argv[1]
tarfilename=sys.argv[2]
else:
hashtype="md5"
tarfilename=sys.argv[1]
h=hashlib.new(hashtype) parser = argparse.ArgumentParser()
tf=tarfile.open(tarfilename,'r') parser.add_argument('infile', type=argparse.FileType('rb'))
#print usage if anything goes wrong parser.add_argument('-c','--hashtype', default="md5" , choices=hashlib.algorithms_available )
except Exception as e : args = parser.parse_args()
print(e)
print("usage: tarsum.py [hashtype] tarfile.tgz") tf=tarfile.open(fileobj=args.infile)
print("hashtype can be:")
print(hashlib.algorithms_available)
print("md5 is default")
exit(1)
for file in tf: for file in tf:
if file.isfile(): if file.isfile():
h=hashlib.new(hashtype) h=hashlib.new(args.hashtype)
extracted_file=tf.extractfile(file) extracted_file=tf.extractfile(file)
for chunk in iter(lambda: extracted_file.read(h.block_size),b''): for chunk in iter(lambda: extracted_file.read(h.block_size),b''):
h.update(chunk) h.update(chunk)