cmd/crc32.py

35 lines
846 B
Python
Raw Normal View History

2020-12-08 04:31:03 +00:00
import argparse
2021-05-25 08:40:34 +00:00
import os
2020-05-13 01:17:17 +00:00
import sys
import zlib
2020-12-08 04:31:03 +00:00
from voussoirkit import pipeable
2020-05-13 01:17:17 +00:00
from voussoirkit import winglob
2020-12-08 04:31:03 +00:00
def crc32_argparse(args):
files = (
file
2021-05-25 08:40:34 +00:00
for pattern in pipeable.input_many(args.patterns)
2020-12-08 04:31:03 +00:00
for file in winglob.glob(pattern)
2021-05-25 08:40:34 +00:00
if os.path.isfile(file)
2020-12-08 04:31:03 +00:00
)
for file in files:
try:
with open(file, 'rb') as handle:
crc = zlib.crc32(handle.read())
print(hex(crc)[2:].rjust(8, '0'), file)
except Exception as e:
print(file, e)
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:]))