Clean up code.

This commit is contained in:
2023-10-20 18:37:43 +02:00
parent 103714e9c6
commit 158bc83a14

62
ulzx
View File

@@ -3,32 +3,33 @@
Read only, Amiga LZX[1] archiver Virtual filesystem executive for Midnight Read only, Amiga LZX[1] archiver Virtual filesystem executive for Midnight
Commander. Commander.
Tested against python 3.6, unlzx[1] 1.1 and mc 4.8.22 Tested against python 3.8, unlzx[1] 1.1 and mc 4.8.22
[1] ftp://us.aminet.net/pub/aminet/misc/unix/unlzx.c.gz.readme [1] ftp://us.aminet.net/pub/aminet/misc/unix/unlzx.c.gz.readme
Changelog: Changelog:
1.3 Code cleanup
1.2 Use python3 1.2 Use python3
1.1 Moved common code into extfslib library 1.1 Moved common code into extfslib library
1.0 Initial release 1.0 Initial release
Author: Roman 'gryf' Dobosz <gryf73@gmail.com> Author: Roman 'gryf' Dobosz <gryf73@gmail.com>
Date: 2019-06-30 Date: 2023-10-20
Version: 1.2 Version: 1.2
Licence: BSD Licence: BSD
""" """
import os import os
import sys
import re import re
import shutil import shutil
from subprocess import call, CalledProcessError import subprocess
from tempfile import mkdtemp, mkstemp import sys
import tempfile
from extfslib import Archive, parse_args import extfslib
class ULzx(Archive): class ULzx(extfslib.Archive):
"""Archive handle. Provides interface to MC's extfs subsystem""" """LZX archive handle. Provides interface to MC's extfs subsystem"""
LINE_PAT = re.compile(b"^\s+(?P<size>\d+)\s+" LINE_PAT = re.compile(b"^\s+(?P<size>\d+)\s+"
b"((n/a)|\d+)\s" b"((n/a)|\d+)\s"
b"(?P<time>\d{2}:\d{2}:\d{2})\s+" b"(?P<time>\d{2}:\d{2}:\d{2})\s+"
@@ -87,7 +88,7 @@ class ULzx(Archive):
def run(self, dst): def run(self, dst):
"""Execute file out of archive""" """Execute file out of archive"""
fdesc, tmp_file = mkstemp() fdesc, tmp_file = tempfile.mkstemp()
os.close(fdesc) os.close(fdesc)
result = 0 result = 0
@@ -96,42 +97,35 @@ class ULzx(Archive):
os.chmod(tmp_file, int("700", 8)) os.chmod(tmp_file, int("700", 8))
try: with open(os.devnull, "w") as fnull:
result = call([tmp_file]) result = subprocess.run([tmp_file], stderr=fnull)
finally: os.unlink(tmp_file)
try:
os.unlink(tmp_file)
except OSError:
pass
return result return result.returncode
def copyout(self, src, dst): def copyout(self, src, dst):
"""Unfortunately, to copy one file out entire LZX archive have to be """Unfortunately, to copy one file out entire LZX archive have to be
extracted. For small archives is not a problem, but in relatively big extracted. For small archives is not a problem, but in relatively big
one it could be a performance issue.""" one it could be a performance issue."""
tmp_dir = mkdtemp() tmp_dir = tempfile.mkdtemp()
src = self._get_real_name(src) src = self._get_real_name(src)
current_dir = os.path.abspath(os.curdir) current_dir = os.path.abspath(os.curdir)
os.chdir(tmp_dir) os.chdir(tmp_dir)
try: with open(os.devnull, "w") as fnull:
with open(os.devnull, "w") as fnull: result = subprocess.run([self.ARCHIVER, self.CMDS['read'],
result = call([self.ARCHIVER, self.CMDS['read'], os.path.join(current_dir, self._arch)],
os.path.join(current_dir, self._arch)], stdout=fnull, stderr=fnull)
stdout=fnull, stderr=fnull) if result.returncode == 0:
if result == 0: # use subprocess, as shutil.copy2 will complain about mixing
# use subprocess, as shutil.copy2 will complain about mixing # strings with bytes
# strings with bytes subprocess.run(['cp', src, dst])
subprocess.run(['cp', src, dst])
except CalledProcessError:
return 1
finally:
shutil.rmtree(tmp_dir)
os.chdir(current_dir)
return result shutil.rmtree(tmp_dir)
os.chdir(current_dir)
return result.returncode
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(parse_args(ULzx)) sys.exit(extfslib.parse_args(ULzx))