diff --git a/kindlecomicconverter/KCC_gui.py b/kindlecomicconverter/KCC_gui.py index a92f13ed..ccf4adac 100644 --- a/kindlecomicconverter/KCC_gui.py +++ b/kindlecomicconverter/KCC_gui.py @@ -35,7 +35,7 @@ from raven import Client 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 comic2ebook from . import metadata @@ -846,7 +846,7 @@ def detectKindleGen(self, startup=False): except Exception: pass 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 for line in versionCheck.stdout.splitlines(): if 'Amazon kindlegen' in line: @@ -1041,12 +1041,12 @@ def __init__(self, kccapp, kccwindow): 'important tips.', 'info') try: - subprocess_run_silent(['tar'], stdout=PIPE, stderr=STDOUT) + subprocess_run(['tar'], stdout=PIPE, stderr=STDOUT) self.tar = True except FileNotFoundError: self.tar = False try: - subprocess_run_silent(['7z'], stdout=PIPE, stderr=STDOUT) + subprocess_run(['7z'], stdout=PIPE, stderr=STDOUT) self.sevenzip = True except FileNotFoundError: self.sevenzip = False diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index dc673b42..5198b5fd 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -40,7 +40,7 @@ from psutil import virtual_memory, disk_usage 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 image 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 \ source.endswith('.ZIP') or source.endswith('.CBZ'): try: - subprocess_run_silent(['7z'], stdout=PIPE, stderr=STDOUT) + subprocess_run(['7z'], stdout=PIPE, stderr=STDOUT) except FileNotFoundError: print('ERROR: 7z is missing!') sys.exit(1) if options.format == 'MOBI': try: - subprocess_run_silent(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT) + subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT) except FileNotFoundError: print('ERROR: KindleGen is missing!') sys.exit(1) @@ -1277,7 +1277,7 @@ def makeMOBIWorker(item): kindlegenError = '' try: 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') for line in output.stdout.splitlines(): # ERROR: Generic error diff --git a/kindlecomicconverter/comicarchive.py b/kindlecomicconverter/comicarchive.py index ee9b0d3a..a56d6d50 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -18,15 +18,14 @@ # PERFORMANCE OF THIS SOFTWARE. # +from functools import cached_property import os import platform -import subprocess import distro -from shutil import move from subprocess import STDOUT, PIPE, CalledProcessError from xml.dom.minidom import parseString 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.' @@ -34,56 +33,78 @@ class ComicArchive: def __init__(self, filepath): self.filepath = filepath - self.type = None if not os.path.isfile(self.filepath): raise OSError('File not found.') - try: - process = subprocess_run_silent(['7z', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE) - except FileNotFoundError: - return - for line in process.stdout.splitlines(): - if b'Type =' in line: - self.type = line.rstrip().decode().split(' = ')[1].upper() - break - if process.returncode != 0 and distro.id() == 'fedora': - process = subprocess_run_silent(['unrar', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE) - for line in process.stdout.splitlines(): - if b'Details: ' in line: - self.type = line.rstrip().decode().split(' ')[1].upper() - break - if process.returncode != 0: - raise OSError(EXTRACTION_ERROR) + + @cached_property + def type(self): + extraction_commands = [ + ['7z', 'l', '-y', '-p1', self.filepath], + ] + + if distro.id() == 'fedora': + extraction_commands.append( + ['unrar', 'l', '-y', '-p1', self.filepath], + ) + + for cmd in extraction_commands: + try: + process = subprocess_run(cmd, capture_output=True, check=True) + 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): if not os.path.isdir(targetdir): raise OSError('Target directory doesn\'t exist.') - try: - process = subprocess_run_silent(['tar', '-xf', self.filepath, '-C', targetdir], - stdout=PIPE, stderr=STDOUT, check=True) - return targetdir - except (FileNotFoundError, CalledProcessError): - pass - 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': - process = subprocess_run_silent(['unrar', 'x', '-y', '-x__MACOSX', '-x.DS_Store', '-xthumbs.db', '-xThumbs.db', self.filepath, targetdir] - , stdout=PIPE, stderr=STDOUT) - if process.returncode != 0: - raise OSError(EXTRACTION_ERROR) - elif process.returncode != 0: + + missing = [] + + extraction_commands = [ + ['tar', '-xf', self.filepath, '-C', targetdir], + ['7z', 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath], + ] + + if platform.system() == 'Darwin': + extraction_commands.append( + ['unar', self.filepath, '-f', '-o', targetdir] + ) + + 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 specialized extraction software. ') + else: raise OSError(EXTRACTION_ERROR) - return targetdir def addFile(self, sourcefile): if self.type in ['RAR', 'RAR5']: 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) if process.returncode != 0: raise OSError('Failed to add the file.') 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) if process.returncode != 0: raise OSError(EXTRACTION_ERROR) diff --git a/kindlecomicconverter/shared.py b/kindlecomicconverter/shared.py index 73a2488d..65196a27 100644 --- a/kindlecomicconverter/shared.py +++ b/kindlecomicconverter/shared.py @@ -137,7 +137,7 @@ def dependencyCheck(level): print('ERROR: ' + ', '.join(missing) + ' is not installed!') sys.exit(1) -def subprocess_run_silent(command, **kwargs): +def subprocess_run(command, **kwargs): if (os.name == 'nt'): kwargs.setdefault('creationflags', subprocess.CREATE_NO_WINDOW) return subprocess.run(command, **kwargs)