cmd/crc32.py

41 lines
1 KiB
Python
Raw Normal View History

2020-12-08 04:31:03 +00:00
import argparse
2020-05-13 01:17:17 +00:00
import sys
import zlib
from voussoirkit import pathclass
2020-12-08 04:31:03 +00:00
from voussoirkit import pipeable
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'crc32')
2020-05-13 01:17:17 +00:00
2020-12-08 04:31:03 +00:00
def crc32_argparse(args):
return_status = 0
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
files = pathclass.glob_many(patterns, files=True)
2020-12-08 04:31:03 +00:00
for file in files:
try:
with open(file, 'rb') as handle:
crc = zlib.crc32(handle.read())
crc = hex(crc)[2:].rjust(8, '0')
pipeable.stdout(f'{crc} {file}')
2020-12-08 04:31:03 +00:00
except Exception as e:
log.error('%s %s', file, e)
return_status = 1
return return_status
2020-12-08 04:31:03 +00:00
@vlogging.main_decorator
2020-12-08 04:31:03 +00:00
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
2021-05-25 08:40:34 +00:00
parser.add_argument('patterns', nargs='+')
2020-12-08 04:31:03 +00:00
parser.set_defaults(func=crc32_argparse)
args = parser.parse_args(argv)
return args.func(args)
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))