From 9e098989862295c443fb083e7fa227f4ee3b320e Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Sun, 4 Aug 2024 16:13:22 -0700 Subject: [PATCH 1/6] refactor archive code --- kindlecomicconverter/KCC_gui.py | 8 +-- kindlecomicconverter/comic2ebook.py | 8 +-- kindlecomicconverter/comicarchive.py | 90 ++++++++++++++++------------ kindlecomicconverter/shared.py | 2 +- 4 files changed, 61 insertions(+), 47 deletions(-) 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..683f1bb5 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -18,15 +18,13 @@ # 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 +32,72 @@ 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: + + 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], + ] + missing = [] + + 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, try downloading {missing}') + 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) From 3e90dd66c3c28e5456339017d18e156b48429921 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Sun, 4 Aug 2024 21:29:50 -0700 Subject: [PATCH 2/6] simplify code --- kindlecomicconverter/comicarchive.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/kindlecomicconverter/comicarchive.py b/kindlecomicconverter/comicarchive.py index 683f1bb5..d6d71804 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -52,9 +52,7 @@ def type(self): for line in process.stdout.splitlines(): if b'Type =' in line: return line.rstrip().decode().split(' = ')[1].upper() - except FileNotFoundError: - pass - except CalledProcessError: + except (FileNotFoundError, CalledProcessError): pass raise OSError(EXTRACTION_ERROR) @@ -67,7 +65,6 @@ def extract(self, targetdir): ['tar', '-xf', self.filepath, '-C', targetdir], ['7z', 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath], ] - missing = [] if distro.id() == 'fedora': extraction_commands.append( @@ -78,15 +75,10 @@ def extract(self, targetdir): try: subprocess_run(cmd, capture_output=True, check=True) return targetdir - except FileNotFoundError: - missing.append(cmd[0]) - except CalledProcessError: + except (FileNotFoundError, CalledProcessError): pass - - if missing: - raise OSError(f'Extraction failed, try downloading {missing}') - else: - raise OSError(EXTRACTION_ERROR) + + raise OSError(EXTRACTION_ERROR) def addFile(self, sourcefile): if self.type in ['RAR', 'RAR5']: From 5c81d5e1b8f57cc2a91154f2c782bb8c83aba2c0 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Fri, 9 Aug 2024 16:40:57 -0700 Subject: [PATCH 3/6] Revert "simplify code" This reverts commit 3e90dd66c3c28e5456339017d18e156b48429921. --- kindlecomicconverter/comicarchive.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/kindlecomicconverter/comicarchive.py b/kindlecomicconverter/comicarchive.py index d6d71804..683f1bb5 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -52,7 +52,9 @@ def type(self): for line in process.stdout.splitlines(): if b'Type =' in line: return line.rstrip().decode().split(' = ')[1].upper() - except (FileNotFoundError, CalledProcessError): + except FileNotFoundError: + pass + except CalledProcessError: pass raise OSError(EXTRACTION_ERROR) @@ -65,6 +67,7 @@ def extract(self, targetdir): ['tar', '-xf', self.filepath, '-C', targetdir], ['7z', 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath], ] + missing = [] if distro.id() == 'fedora': extraction_commands.append( @@ -75,10 +78,15 @@ def extract(self, targetdir): try: subprocess_run(cmd, capture_output=True, check=True) return targetdir - except (FileNotFoundError, CalledProcessError): + except FileNotFoundError: + missing.append(cmd[0]) + except CalledProcessError: pass - - raise OSError(EXTRACTION_ERROR) + + if missing: + raise OSError(f'Extraction failed, try downloading {missing}') + else: + raise OSError(EXTRACTION_ERROR) def addFile(self, sourcefile): if self.type in ['RAR', 'RAR5']: From 0832533c847413470209ebd019b9d83a5d127012 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Fri, 9 Aug 2024 16:56:10 -0700 Subject: [PATCH 4/6] add link to missing extraction software --- kindlecomicconverter/comicarchive.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/kindlecomicconverter/comicarchive.py b/kindlecomicconverter/comicarchive.py index 683f1bb5..76d70923 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -20,6 +20,7 @@ from functools import cached_property import os +import platform import distro from subprocess import STDOUT, PIPE, CalledProcessError from xml.dom.minidom import parseString @@ -63,11 +64,17 @@ def extract(self, targetdir): if not os.path.isdir(targetdir): raise OSError('Target directory doesn\'t exist.') + missing = [] + extraction_commands = [ - ['tar', '-xf', self.filepath, '-C', targetdir], + ['tard', '-xf', self.filepath, '-C', targetdir], ['7z', 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath], ] - missing = [] + + if platform.system() == 'Darwin': + extraction_commands.append( + ['unar', self.filepath, '-f', '-o', targetdir] + ) if distro.id() == 'fedora': extraction_commands.append( @@ -84,7 +91,7 @@ def extract(self, targetdir): pass if missing: - raise OSError(f'Extraction failed, try downloading {missing}') + raise OSError(f'Extraction failed, try downloading additional extraction software. ') else: raise OSError(EXTRACTION_ERROR) From 0c4464e91f46ab32817d00e89e452c4d8fb80c5b Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Fri, 9 Aug 2024 17:01:20 -0700 Subject: [PATCH 5/6] fix tar --- kindlecomicconverter/comicarchive.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kindlecomicconverter/comicarchive.py b/kindlecomicconverter/comicarchive.py index 76d70923..74cc9804 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -67,7 +67,7 @@ def extract(self, targetdir): missing = [] extraction_commands = [ - ['tard', '-xf', self.filepath, '-C', targetdir], + ['tar', '-xf', self.filepath, '-C', targetdir], ['7z', 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath], ] @@ -91,7 +91,7 @@ def extract(self, targetdir): pass if missing: - raise OSError(f'Extraction failed, try downloading additional extraction software. ') + raise OSError(f'Extraction failed, download specialized extraction software. ') else: raise OSError(EXTRACTION_ERROR) From 66487d4c6d495f60b133dfa2e106ec0f0ddf423b Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Fri, 9 Aug 2024 17:02:06 -0700 Subject: [PATCH 6/6] fix wording --- kindlecomicconverter/comicarchive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kindlecomicconverter/comicarchive.py b/kindlecomicconverter/comicarchive.py index 74cc9804..a56d6d50 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -91,7 +91,7 @@ def extract(self, targetdir): pass if missing: - raise OSError(f'Extraction failed, download specialized extraction software. ') + raise OSError(f'Extraction failed, install specialized extraction software. ') else: raise OSError(EXTRACTION_ERROR)