1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-13 01:36:27 +00:00

fix no such file or directory 7z (#728)

* refactor archive code

* simplify code

* Revert "simplify code"

This reverts commit 3e90dd66c3.

* add link to missing extraction software

* fix tar

* fix wording
This commit is contained in:
Alex Xu
2024-08-09 17:08:13 -07:00
committed by GitHub
parent 2c770f4562
commit ab089adbca
4 changed files with 67 additions and 46 deletions

View File

@@ -35,7 +35,7 @@ from distutils.version import StrictVersion
from raven import Client from raven import Client
from tempfile import gettempdir from tempfile import gettempdir
from .shared import HTMLStripper, sanitizeTrace, walkLevel, subprocess_run_silent from .shared import HTMLStripper, sanitizeTrace, walkLevel, subprocess_run
from . import __version__ from . import __version__
from . import comic2ebook from . import comic2ebook
from . import metadata from . import metadata
@@ -846,7 +846,7 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
except Exception: except Exception:
pass pass
try: try:
versionCheck = subprocess_run_silent(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8') versionCheck = subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8')
self.kindleGen = True self.kindleGen = True
for line in versionCheck.stdout.splitlines(): for line in versionCheck.stdout.splitlines():
if 'Amazon kindlegen' in line: if 'Amazon kindlegen' in line:
@@ -1041,12 +1041,12 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
'<a href="https://github.com/ciromattia/kcc/wiki/Important-tips">important tips</a>.', '<a href="https://github.com/ciromattia/kcc/wiki/Important-tips">important tips</a>.',
'info') 'info')
try: try:
subprocess_run_silent(['tar'], stdout=PIPE, stderr=STDOUT) subprocess_run(['tar'], stdout=PIPE, stderr=STDOUT)
self.tar = True self.tar = True
except FileNotFoundError: except FileNotFoundError:
self.tar = False self.tar = False
try: try:
subprocess_run_silent(['7z'], stdout=PIPE, stderr=STDOUT) subprocess_run(['7z'], stdout=PIPE, stderr=STDOUT)
self.sevenzip = True self.sevenzip = True
except FileNotFoundError: except FileNotFoundError:
self.sevenzip = False self.sevenzip = False

View File

@@ -40,7 +40,7 @@ from subprocess import STDOUT, PIPE
from psutil import virtual_memory, disk_usage from psutil import virtual_memory, disk_usage
from html import escape as hescape from html import escape as hescape
from .shared import md5Checksum, getImageFileName, walkSort, walkLevel, sanitizeTrace, subprocess_run_silent from .shared import md5Checksum, getImageFileName, walkSort, walkLevel, sanitizeTrace, subprocess_run
from . import comic2panel from . import comic2panel
from . import image from . import image
from . import comicarchive from . import comicarchive
@@ -1114,13 +1114,13 @@ def checkTools(source):
if source.endswith('.CB7') or source.endswith('.7Z') or source.endswith('.RAR') or source.endswith('.CBR') or \ if source.endswith('.CB7') or source.endswith('.7Z') or source.endswith('.RAR') or source.endswith('.CBR') or \
source.endswith('.ZIP') or source.endswith('.CBZ'): source.endswith('.ZIP') or source.endswith('.CBZ'):
try: try:
subprocess_run_silent(['7z'], stdout=PIPE, stderr=STDOUT) subprocess_run(['7z'], stdout=PIPE, stderr=STDOUT)
except FileNotFoundError: except FileNotFoundError:
print('ERROR: 7z is missing!') print('ERROR: 7z is missing!')
sys.exit(1) sys.exit(1)
if options.format == 'MOBI': if options.format == 'MOBI':
try: try:
subprocess_run_silent(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT) subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT)
except FileNotFoundError: except FileNotFoundError:
print('ERROR: KindleGen is missing!') print('ERROR: KindleGen is missing!')
sys.exit(1) sys.exit(1)
@@ -1277,7 +1277,7 @@ def makeMOBIWorker(item):
kindlegenError = '' kindlegenError = ''
try: try:
if os.path.getsize(item) < 629145600: if os.path.getsize(item) < 629145600:
output = subprocess_run_silent(['kindlegen', '-dont_append_source', '-locale', 'en', item], output = subprocess_run(['kindlegen', '-dont_append_source', '-locale', 'en', item],
stdout=PIPE, stderr=STDOUT, encoding='UTF-8') stdout=PIPE, stderr=STDOUT, encoding='UTF-8')
for line in output.stdout.splitlines(): for line in output.stdout.splitlines():
# ERROR: Generic error # ERROR: Generic error

View File

@@ -18,15 +18,14 @@
# PERFORMANCE OF THIS SOFTWARE. # PERFORMANCE OF THIS SOFTWARE.
# #
from functools import cached_property
import os import os
import platform import platform
import subprocess
import distro import distro
from shutil import move
from subprocess import STDOUT, PIPE, CalledProcessError from subprocess import STDOUT, PIPE, CalledProcessError
from xml.dom.minidom import parseString from xml.dom.minidom import parseString
from xml.parsers.expat import ExpatError from xml.parsers.expat import ExpatError
from .shared import subprocess_run_silent from .shared import subprocess_run
EXTRACTION_ERROR = 'Failed to extract archive. Try extracting file outside of KCC.' EXTRACTION_ERROR = 'Failed to extract archive. Try extracting file outside of KCC.'
@@ -34,56 +33,78 @@ EXTRACTION_ERROR = 'Failed to extract archive. Try extracting file outside of KC
class ComicArchive: class ComicArchive:
def __init__(self, filepath): def __init__(self, filepath):
self.filepath = filepath self.filepath = filepath
self.type = None
if not os.path.isfile(self.filepath): if not os.path.isfile(self.filepath):
raise OSError('File not found.') raise OSError('File not found.')
try:
process = subprocess_run_silent(['7z', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE) @cached_property
except FileNotFoundError: def type(self):
return extraction_commands = [
for line in process.stdout.splitlines(): ['7z', 'l', '-y', '-p1', self.filepath],
if b'Type =' in line: ]
self.type = line.rstrip().decode().split(' = ')[1].upper()
break if distro.id() == 'fedora':
if process.returncode != 0 and distro.id() == 'fedora': extraction_commands.append(
process = subprocess_run_silent(['unrar', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE) ['unrar', 'l', '-y', '-p1', self.filepath],
for line in process.stdout.splitlines(): )
if b'Details: ' in line:
self.type = line.rstrip().decode().split(' ')[1].upper() for cmd in extraction_commands:
break try:
if process.returncode != 0: process = subprocess_run(cmd, capture_output=True, check=True)
raise OSError(EXTRACTION_ERROR) for line in process.stdout.splitlines():
if b'Type =' in line:
return line.rstrip().decode().split(' = ')[1].upper()
except FileNotFoundError:
pass
except CalledProcessError:
pass
raise OSError(EXTRACTION_ERROR)
def extract(self, targetdir): def extract(self, targetdir):
if not os.path.isdir(targetdir): if not os.path.isdir(targetdir):
raise OSError('Target directory doesn\'t exist.') raise OSError('Target directory doesn\'t exist.')
try:
process = subprocess_run_silent(['tar', '-xf', self.filepath, '-C', targetdir], missing = []
stdout=PIPE, stderr=STDOUT, check=True)
return targetdir extraction_commands = [
except (FileNotFoundError, CalledProcessError): ['tar', '-xf', self.filepath, '-C', targetdir],
pass ['7z', 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath],
process = subprocess_run_silent(['7z', 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath], ]
stdout=PIPE, stderr=STDOUT)
if process.returncode != 0 and distro.id() == 'fedora': if platform.system() == 'Darwin':
process = subprocess_run_silent(['unrar', 'x', '-y', '-x__MACOSX', '-x.DS_Store', '-xthumbs.db', '-xThumbs.db', self.filepath, targetdir] extraction_commands.append(
, stdout=PIPE, stderr=STDOUT) ['unar', self.filepath, '-f', '-o', targetdir]
if process.returncode != 0: )
raise OSError(EXTRACTION_ERROR)
elif process.returncode != 0: if distro.id() == 'fedora':
extraction_commands.append(
['unrar', 'x', '-y', '-x__MACOSX', '-x.DS_Store', '-xthumbs.db', '-xThumbs.db', self.filepath, targetdir]
)
for cmd in extraction_commands:
try:
subprocess_run(cmd, capture_output=True, check=True)
return targetdir
except FileNotFoundError:
missing.append(cmd[0])
except CalledProcessError:
pass
if missing:
raise OSError(f'Extraction failed, install <a href="https://github.com/ciromattia/kcc#7-zip">specialized extraction software.</a> ')
else:
raise OSError(EXTRACTION_ERROR) raise OSError(EXTRACTION_ERROR)
return targetdir
def addFile(self, sourcefile): def addFile(self, sourcefile):
if self.type in ['RAR', 'RAR5']: if self.type in ['RAR', 'RAR5']:
raise NotImplementedError raise NotImplementedError
process = subprocess_run_silent(['7z', 'a', '-y', self.filepath, sourcefile], process = subprocess_run(['7z', 'a', '-y', self.filepath, sourcefile],
stdout=PIPE, stderr=STDOUT) stdout=PIPE, stderr=STDOUT)
if process.returncode != 0: if process.returncode != 0:
raise OSError('Failed to add the file.') raise OSError('Failed to add the file.')
def extractMetadata(self): def extractMetadata(self):
process = subprocess_run_silent(['7z', 'x', '-y', '-so', self.filepath, 'ComicInfo.xml'], process = subprocess_run(['7z', 'x', '-y', '-so', self.filepath, 'ComicInfo.xml'],
stdout=PIPE, stderr=STDOUT) stdout=PIPE, stderr=STDOUT)
if process.returncode != 0: if process.returncode != 0:
raise OSError(EXTRACTION_ERROR) raise OSError(EXTRACTION_ERROR)

View File

@@ -137,7 +137,7 @@ def dependencyCheck(level):
print('ERROR: ' + ', '.join(missing) + ' is not installed!') print('ERROR: ' + ', '.join(missing) + ' is not installed!')
sys.exit(1) sys.exit(1)
def subprocess_run_silent(command, **kwargs): def subprocess_run(command, **kwargs):
if (os.name == 'nt'): if (os.name == 'nt'):
kwargs.setdefault('creationflags', subprocess.CREATE_NO_WINDOW) kwargs.setdefault('creationflags', subprocess.CREATE_NO_WINDOW)
return subprocess.run(command, **kwargs) return subprocess.run(command, **kwargs)