cmd/crc32.py

40 lines
1,001 B
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_files(patterns)
2020-12-08 04:31:03 +00:00
for file in files:
try:
2021-10-05 00:21:14 +00:00
crc = zlib.crc32(file.read('rb'))
crc = hex(crc)[2:].rjust(8, '0')
2021-10-05 00:21:14 +00:00
pipeable.stdout(f'{crc} {file.absolute_path}')
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:]))