annotate bin/cek.py @ 693:37a2470380f2

[bashrc] Fix comment about freebsd grep
author nanaya <me@nanaya.pro>
date Sun, 23 Oct 2022 01:56:54 +0900
parents 137b18a1898e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
79
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
1 #!/usr/bin/env python
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
2
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
3 import zlib, os, sys
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
4
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
5 def get_file_crc32(filename):
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
6 block_size = 1*2**20
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
7 return_code = 0
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
8 file_crc32 = 0
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
9 file_digest = 0
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
10 file_handle = open(filename, "rb")
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
11 if return_code == 0:
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
12 file_block = file_handle.read(block_size)
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
13 while file_block:
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
14 file_digest = zlib.crc32(file_block, file_digest)
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
15 file_block = file_handle.read(block_size)
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
16 file_handle.close()
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
17 file_digest = file_digest & 0xffffffff
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
18 return "%08X" % file_digest
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
19
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
20 if __name__ == "__main__":
08d5f6023998 Replace checksum calculator with python version.
Edho Prima Arief <me@myconan.net>
parents:
diff changeset
21 for file in sys.argv[1:]:
681
137b18a1898e [cek.py] Python 3
nanaya <me@nanaya.pro>
parents: 79
diff changeset
22 print(f'{file} {get_file_crc32(file)}')