2020-12-08 04:31:03 +00:00
|
|
|
import argparse
|
2020-05-13 01:17:17 +00:00
|
|
|
import sys
|
|
|
|
import zlib
|
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
from voussoirkit import pathclass
|
2020-12-08 04:31:03 +00:00
|
|
|
from voussoirkit import pipeable
|
2021-09-24 06:42:34 +00:00
|
|
|
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):
|
2021-09-24 06:42:34 +00:00
|
|
|
return_status = 0
|
|
|
|
|
|
|
|
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
|
2021-11-08 19:38:10 +00:00
|
|
|
files = pathclass.glob_many_files(patterns)
|
2021-09-24 06:42:34 +00:00
|
|
|
|
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'))
|
2021-09-24 06:42:34 +00:00
|
|
|
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:
|
2021-09-24 06:42:34 +00:00
|
|
|
log.error('%s %s', file, e)
|
|
|
|
return_status = 1
|
|
|
|
|
|
|
|
return return_status
|
2020-12-08 04:31:03 +00:00
|
|
|
|
2021-09-24 06:42:34 +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:]))
|