Skip to content

Commit

Permalink
python 3.9 kompatibel gemacht
Browse files Browse the repository at this point in the history
  • Loading branch information
oli committed Jan 2, 2021
1 parent 38dca1b commit 06f230e
Showing 1 changed file with 42 additions and 49 deletions.
91 changes: 42 additions & 49 deletions resources/lib/fixetzipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,20 +397,20 @@ def _decodeExtra(self):
elif ln == 0:
counts = ()
else:
raise RuntimeError, "Corrupt extra field %s"%(ln,)
raise RuntimeError("Corrupt extra field %s" % (ln,))

idx = 0

# ZIP64 extension (large files and/or large archives)
if self.file_size in (0xffffffffffffffffL, 0xffffffffL):
if self.file_size in (0xffffffffffffffff, 0xffffffff):
self.file_size = counts[idx]
idx += 1

if self.compress_size == 0xFFFFFFFFL:
if self.compress_size == 0xFFFFFFFF:
self.compress_size = counts[idx]
idx += 1

if self.header_offset == 0xffffffffL:
if self.header_offset == 0xffffffff:
old = self.header_offset
self.header_offset = counts[idx]
idx+=1
Expand All @@ -431,7 +431,7 @@ class _ZipDecrypter:
plain_text = map(zd, cypher_text)
"""

def _GenerateCRCTable():
def _GenerateCRCTable(self):
"""Generate a CRC-32 table.
ZIP encryption uses the CRC32 one-byte primitive for scrambling some
Expand Down Expand Up @@ -733,10 +733,9 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
pass
elif compression == ZIP_DEFLATED:
if not zlib:
raise RuntimeError,\
"Compression requires the (missing) zlib module"
raise RuntimeError("Compression requires the (missing) zlib module")
else:
raise RuntimeError, "That compression method is not supported"
raise RuntimeError("That compression method is not supported")

self._allowZip64 = allowZip64
self._didModify = False
Expand Down Expand Up @@ -811,9 +810,9 @@ def _RealGetContents(self):
except IOError:
raise BadZipfile("File is not a zip file")
if not endrec:
raise BadZipfile, "File is not a zip file"
raise BadZipfile("File is not a zip file")
if self.debug > 1:
print endrec
print(endrec)
size_cd = endrec[_ECD_SIZE] # bytes in central directory
offset_cd = endrec[_ECD_OFFSET] # offset of central directory
self._comment = endrec[_ECD_COMMENT] # archive comment
Expand All @@ -827,7 +826,7 @@ def _RealGetContents(self):

if self.debug > 2:
inferred = self._start_disk + offset_cd
print "given, inferred, offset", offset_cd, inferred, self._start_disk
print("given, inferred, offset", offset_cd, inferred, self._start_disk)
# self.start_dir: Position of start of central directory
self.start_dir = offset_cd + self._start_disk
fp.seek(self.start_dir, 0)
Expand All @@ -842,7 +841,7 @@ def _RealGetContents(self):
if centdir[_CD_SIGNATURE] != stringCentralDir:
raise BadZipfile("Bad magic number for central directory")
if self.debug > 2:
print centdir
print(centdir)
filename = fp.read(centdir[_CD_FILENAME_LENGTH])
# Create ZipInfo instance to store file information
x = ZipInfo(filename)
Expand Down Expand Up @@ -870,7 +869,7 @@ def _RealGetContents(self):
+ centdir[_CD_COMMENT_LENGTH])

if self.debug > 2:
print "total", total
print("total", total)


def namelist(self):
Expand All @@ -887,10 +886,10 @@ def infolist(self):

def printdir(self):
"""Print a table of contents for the zip file."""
print "%-46s %19s %12s" % ("File Name", "Modified ", "Size")
print("%-46s %19s %12s" % ("File Name", "Modified ", "Size"))
for zinfo in self.filelist:
date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
print "%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size)
print("%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size))

def testzip(self):
"""Read all the files and check the CRC."""
Expand Down Expand Up @@ -941,10 +940,9 @@ def read(self, name, pwd=None):
def open(self, name, mode="r", pwd=None):
"""Return file-like object for 'name'."""
if mode not in ("r", "U", "rU"):
raise RuntimeError, 'open() requires mode "r", "U", or "rU"'
raise RuntimeError('open() requires mode "r", "U", or "rU"')
if not self.fp:
raise RuntimeError, \
"Attempt to read ZIP archive that was already closed"
raise RuntimeError("Attempt to read ZIP archive that was already closed")

# Only open a new file for instances where we were not
# given a file object in the constructor
Expand Down Expand Up @@ -979,9 +977,8 @@ def open(self, name, mode="r", pwd=None):
zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])

if fname != zinfo.orig_filename:
raise BadZipfile, \
'File name in directory "%s" and header "%s" differ.' % (
zinfo.orig_filename, fname)
raise BadZipfile('File name in directory "%s" and header "%s" differ.' % (
zinfo.orig_filename, fname))

# check for encrypted flag & handle password
is_encrypted = zinfo.flag_bits & 0x1
Expand All @@ -990,8 +987,8 @@ def open(self, name, mode="r", pwd=None):
if not pwd:
pwd = self.pwd
if not pwd:
raise RuntimeError, "File %s is encrypted, " \
"password required for extraction" % name
raise RuntimeError("File %s is encrypted, " \
"password required for extraction" % name)

zd = _ZipDecrypter(pwd)
# The first 12 bytes in the cypher stream is an encryption header
Expand Down Expand Up @@ -1095,16 +1092,13 @@ def _writecheck(self, zinfo):
import warnings
warnings.warn('Duplicate name: %r' % zinfo.filename, stacklevel=3)
if self.mode not in ("w", "a"):
raise RuntimeError, 'write() requires mode "w" or "a"'
raise RuntimeError('write() requires mode "w" or "a"')
if not self.fp:
raise RuntimeError, \
"Attempt to write ZIP archive that was already closed"
raise RuntimeError("Attempt to write ZIP archive that was already closed")
if zinfo.compress_type == ZIP_DEFLATED and not zlib:
raise RuntimeError, \
"Compression requires the (missing) zlib module"
raise RuntimeError("Compression requires the (missing) zlib module")
if zinfo.compress_type not in (ZIP_STORED, ZIP_DEFLATED):
raise RuntimeError, \
"That compression method is not supported"
raise RuntimeError("That compression method is not supported")
if not self._allowZip64:
requires_zip64 = None
if len(self.filelist) >= ZIP_FILECOUNT_LIMIT:
Expand Down Expand Up @@ -1137,7 +1131,7 @@ def write(self, filename, arcname=None, compress_type=None):
if isdir:
arcname += '/'
zinfo = ZipInfo(arcname, date_time)
zinfo.external_attr = (st[0] & 0xFFFF) << 16L # Unix attributes
zinfo.external_attr = (st[0] & 0xFFFF) << 16 # Unix attributes
if isdir:
zinfo.compress_type = ZIP_STORED
elif compress_type is None:
Expand Down Expand Up @@ -1291,7 +1285,7 @@ def close(self):
header_offset = zinfo.header_offset - self._start_disk
if header_offset > ZIP64_LIMIT:
extra.append(header_offset)
header_offset = 0xffffffffL
header_offset = 0xffffffff

extra_data = zinfo.extra
if extra:
Expand Down Expand Up @@ -1400,10 +1394,10 @@ def writepy(self, pathname, basename = ""):
else:
basename = name
if self.debug:
print "Adding package in", pathname, "as", basename
print("Adding package in", pathname, "as", basename)
fname, arcname = self._get_codename(initname[0:-3], basename)
if self.debug:
print "Adding", arcname
print("Adding", arcname)
self.write(fname, arcname)
dirlist = os.listdir(pathname)
dirlist.remove("__init__.py")
Expand All @@ -1419,28 +1413,27 @@ def writepy(self, pathname, basename = ""):
fname, arcname = self._get_codename(path[0:-3],
basename)
if self.debug:
print "Adding", arcname
print("Adding", arcname)
self.write(fname, arcname)
else:
# This is NOT a package directory, add its files at top level
if self.debug:
print "Adding files from directory", pathname
print("Adding files from directory", pathname)
for filename in os.listdir(pathname):
path = os.path.join(pathname, filename)
root, ext = os.path.splitext(filename)
if ext == ".py":
fname, arcname = self._get_codename(path[0:-3],
basename)
if self.debug:
print "Adding", arcname
print("Adding", arcname)
self.write(fname, arcname)
else:
if pathname[-3:] != ".py":
raise RuntimeError, \
'Files added with writepy() must end with ".py"'
raise RuntimeError('Files added with writepy() must end with ".py"')
fname, arcname = self._get_codename(pathname[0:-3], basename)
if self.debug:
print "Adding file", arcname
print("Adding file", arcname)
self.write(fname, arcname)

def _get_codename(self, pathname, basename):
Expand All @@ -1460,11 +1453,11 @@ def _get_codename(self, pathname, basename):
os.stat(file_pyc).st_mtime < os.stat(file_py).st_mtime:
import py_compile
if self.debug:
print "Compiling", file_py
print("Compiling", file_py)
try:
py_compile.compile(file_py, file_pyc, None, True)
except py_compile.PyCompileError,err:
print err.msg
except py_compile.PyCompileError as err:
print(err.msg)
fname = file_pyc
else:
fname = file_pyc
Expand All @@ -1487,37 +1480,37 @@ def main(args = None):
args = sys.argv[1:]

if not args or args[0] not in ('-l', '-c', '-e', '-t'):
print USAGE
print(USAGE)
sys.exit(1)

if args[0] == '-l':
if len(args) != 2:
print USAGE
print(USAGE)
sys.exit(1)
with ZipFile(args[1], 'r') as zf:
zf.printdir()

elif args[0] == '-t':
if len(args) != 2:
print USAGE
print(USAGE)
sys.exit(1)
with ZipFile(args[1], 'r') as zf:
badfile = zf.testzip()
if badfile:
print("The following enclosed file is corrupted: {!r}".format(badfile))
print "Done testing"
print("Done testing")

elif args[0] == '-e':
if len(args) != 3:
print USAGE
print(USAGE)
sys.exit(1)

with ZipFile(args[1], 'r') as zf:
zf.extractall(args[2])

elif args[0] == '-c':
if len(args) < 3:
print USAGE
print(USAGE)
sys.exit(1)

def addToZip(zf, path, zippath):
Expand Down

0 comments on commit 06f230e

Please sign in to comment.