Migrate Python code to Python 3.

This commit is contained in:
Simon Tatham 2020-08-26 21:13:29 +01:00
parent 1c1c3850e8
commit 558d8cf7cd
2 changed files with 17 additions and 15 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import sys
import os
@ -10,7 +10,7 @@ from collections import namedtuple
CFHEADER_s = struct.Struct("<4sLLLLLBBHHHHH")
CFHEADER = namedtuple("CFHEADER", "sig res0 size res1 firstfile res2 "
"verminor vermajor folders files flags setid icabinet")
CFHEADER_sig = "MSCF"
CFHEADER_sig = b"MSCF"
CFFOLDER_s = struct.Struct("<LHH")
CFFOLDER = namedtuple("CFFOLDER", "firstdata ndata compresstype")
@ -26,7 +26,7 @@ def mszip(data):
zlib.DEF_MEM_LEVEL, zlib.Z_DEFAULT_STRATEGY)
compressed = compressor.compress(data)
compressed += compressor.flush()
return "CK" + compressed # add MSZIP header
return b"CK" + compressed # add MSZIP header
def packdate(y,m,d):
return ((y - 1980) << 9) | (m << 5) | d
@ -34,19 +34,20 @@ def packtime(h,m,s):
return ((h << 11) | (m << 5) | (s >> 1))
def checksum(data):
data_full_words = data[:len(data) & ~3]
data_last_word = data[len(data_full_words):]
data_last_word = "".join(reversed(data_last_word))
data_last_word += "\0" * (3 & -len(data)) # pad to multiple of 4 bytes
data = data_full_words + data_last_word
data_fullwords_len = len(data) & ~3
data_last_word = data[data_fullwords_len:]
data_last_word = bytes(reversed(data_last_word))
data_last_word += b"\0" * (3 & -len(data)) # pad to multiple of 4 bytes
toret = 0
for offset in xrange(0, len(data), 4):
toret ^= struct.unpack_from("<L", data, offset)[0]
for part, partlen in ((data, data_fullwords_len),
(data_last_word, len(data_last_word))):
for offset in range(0, partlen, 4):
toret ^= struct.unpack_from("<L", part, offset)[0]
return toret
def build_cab(files):
uncompressed_data = ""
fileheaders = ""
uncompressed_data = b""
fileheaders = b""
for name, data, mtime in files:
mtime_u = time.gmtime(mtime)
fileheader = CFFILE(
@ -54,9 +55,10 @@ def build_cab(files):
date=packdate(mtime_u.tm_year, mtime_u.tm_mon, mtime_u.tm_mday),
time=packtime(mtime_u.tm_hour, mtime_u.tm_min, mtime_u.tm_sec))
uncompressed_data += data
fileheaders += CFFILE_s.pack(*fileheader) + name + "\0"
fileheaders += CFFILE_s.pack(*fileheader)
fileheaders += name.encode("ASCII") + b"\0"
compressed_data = ""
compressed_data = b""
offset = 0
n_data_blocks = 0
while offset < len(uncompressed_data):

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import sys
import os