comparison cutils/treesum.py @ 260:07a0bc723139

treesum: Implement the CRC-32 using the new util.crc32 module
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 16 Feb 2025 11:07:04 +0100
parents 61e5b1c2685c
children a3e25957afb7
comparison
equal deleted inserted replaced
259:b24080e5ca96 260:07a0bc723139
22 import os 22 import os
23 import re 23 import re
24 import stat 24 import stat
25 import sys 25 import sys
26 import time 26 import time
27 import zlib
28 27
29 from . import (__version__, __revision__) 28 from . import (__version__, __revision__)
30 from . import util 29 from . import util
31 from .util import cm 30 from .util import cm
32 from .util import digest 31 from .util import digest
33 from .util import walk 32 from .util import walk
33 from .util.crc32 import crc32
34 34
35 35
36 def main(argv=None): 36 def main(argv=None):
37 37
38 def _populate_generate_arguments(gp): 38 def _populate_generate_arguments(gp):
935 self._fp = None 935 self._fp = None
936 return rv 936 return rv
937 937
938 def write(self, what): 938 def write(self, what):
939 self._fp.write(what) 939 self._fp.write(what)
940 self._crc32 = zlib.crc32(what, self._crc32) 940 self._crc32.update(what)
941 941
942 def flush(self): 942 def flush(self):
943 self._fp.flush() 943 self._fp.flush()
944 944
945 def resetdigest(self): 945 def resetdigest(self):
946 """Reset the current CRC digest""" 946 """Reset the current CRC digest"""
947 self._crc32 = zlib.crc32(b"") 947 self._crc32 = crc32()
948 948
949 def hexcrcdigest(self): 949 def hexcrcdigest(self):
950 """ 950 """
951 951
952 :rtype: str 952 :rtype: str
953 953
954 """ 954 """
955 return (hex(self.crcdigest())[2:]).upper() 955 return self._crc32.hexdigest()
956
957 def crcdigest(self):
958 """
959
960 :rtype: int
961
962 """
963 if util.PY2:
964 if self._crc32 < 0:
965 # Return the bitpattern as unsigned 32-bit number
966 return (~self._crc32 ^ 0xFFFFFFFF)
967 else:
968 return self._crc32
969 else:
970 return self._crc32
971 956
972 957
973 def normalized_compatible_mode_str(mode): 958 def normalized_compatible_mode_str(mode):
974 # XXX FIXME: Windows and "executable" 959 # XXX FIXME: Windows and "executable"
975 modebits = stat.S_IMODE(mode) 960 modebits = stat.S_IMODE(mode)
1194 if line: 1179 if line:
1195 self._line_no += 1 1180 self._line_no += 1
1196 return line 1181 return line
1197 1182
1198 def _reset_crc(self): 1183 def _reset_crc(self):
1199 self._crc32 = zlib.crc32(b"") 1184 self._crc32 = crc32()
1200 1185
1201 def _update_crc(self, data): 1186 def _update_crc(self, data):
1202 self._crc32 = zlib.crc32(data, self._crc32) 1187 self._crc32.update(data)
1203 1188
1204 def _hex_crc(self): 1189 def _hex_crc(self):
1205 return (hex(self._get_crc())[2:]).upper() 1190 return self._crc32.hexdigest()
1206
1207 def _get_crc(self):
1208 """Get the current CRC always as positive number with the same bit#
1209 pattern because Python2 yields negative numbers also.
1210
1211 :return: The current CRC value as positive number on all Python
1212 versions
1213 :rtype: int
1214
1215 """
1216 if util.PY2:
1217 if self._crc32 < 0:
1218 # Return the bitpattern as unsigned 32-bit number
1219 return (~self._crc32 ^ 0xFFFFFFFF)
1220 else:
1221 return self._crc32
1222 else:
1223 return self._crc32
1224 1191
1225 def _get_digest_size(self, algo_name): 1192 def _get_digest_size(self, algo_name):
1226 if self._current_algo_name == algo_name: 1193 if self._current_algo_name == algo_name:
1227 return self._current_algo_digest_size 1194 return self._current_algo_digest_size
1228 h = util.algotag2algotype(algo_name)() 1195 h = util.algotag2algotype(algo_name)()