From e8502f008a658211370aad001c0511150afdbdc4 Mon Sep 17 00:00:00 2001 From: VampiroMedicado <149981282+VampiroMedicado@users.noreply.github.com> Date: Mon, 1 Jan 2024 23:11:55 -0300 Subject: [PATCH] hide subprocess consoles on Windows (#656) --- kindlecomicconverter/KCC_gui.py | 6 +++--- kindlecomicconverter/comic2ebook.py | 8 ++++---- kindlecomicconverter/comicarchive.py | 15 ++++++++------- kindlecomicconverter/shared.py | 6 ++++++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/kindlecomicconverter/KCC_gui.py b/kindlecomicconverter/KCC_gui.py index b7a8dc03..e692bba9 100644 --- a/kindlecomicconverter/KCC_gui.py +++ b/kindlecomicconverter/KCC_gui.py @@ -34,7 +34,7 @@ from distutils.version import StrictVersion from raven import Client from tempfile import gettempdir -from .shared import md5Checksum, HTMLStripper, sanitizeTrace, walkLevel +from .shared import md5Checksum, HTMLStripper, sanitizeTrace, walkLevel, subprocess_run_silent from . import __version__ from . import comic2ebook from . import metadata @@ -837,7 +837,7 @@ def detectKindleGen(self, startup=False): except Exception: pass try: - versionCheck = subprocess.run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8') + versionCheck = subprocess_run_silent(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8') self.kindleGen = True for line in versionCheck.stdout.splitlines(): if 'Amazon kindlegen' in line: @@ -1026,7 +1026,7 @@ def __init__(self, kccapp, kccwindow): 'important tips.', 'info') try: - subprocess.run(['7z'], stdout=PIPE, stderr=STDOUT) + subprocess_run_silent(['7z'], stdout=PIPE, stderr=STDOUT) self.sevenzip = True except FileNotFoundError: self.sevenzip = False diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index 12791884..5f88ff82 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -44,7 +44,7 @@ from PyQt5 import QtCore except ImportError: QtCore = None -from .shared import md5Checksum, getImageFileName, walkSort, walkLevel, sanitizeTrace +from .shared import md5Checksum, getImageFileName, walkSort, walkLevel, sanitizeTrace, subprocess_run_silent from . import comic2panel from . import image from . import comicarchive @@ -1104,13 +1104,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(['7z'], stdout=PIPE, stderr=STDOUT) + subprocess_run_silent(['7z'], stdout=PIPE, stderr=STDOUT) except FileNotFoundError: print('ERROR: 7z is missing!') sys.exit(1) if options.format == 'MOBI': try: - subprocess.run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT) + subprocess_run_silent(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT) except FileNotFoundError: print('ERROR: KindleGen is missing!') sys.exit(1) @@ -1267,7 +1267,7 @@ def makeMOBIWorker(item): kindlegenError = '' try: if os.path.getsize(item) < 629145600: - output = subprocess.run(['kindlegen', '-dont_append_source', '-locale', 'en', item], + output = subprocess_run_silent(['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 e8b215a5..a2569733 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -26,6 +26,7 @@ from subprocess import STDOUT, PIPE from xml.dom.minidom import parseString from xml.parsers.expat import ExpatError +from .shared import subprocess_run_silent EXTRACTION_ERROR = 'Failed to extract archive. Try extracting file outside of KCC.' @@ -36,13 +37,13 @@ def __init__(self, filepath): self.type = None if not os.path.isfile(self.filepath): raise OSError('File not found.') - process = subprocess.run(['7z', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE) + process = subprocess_run_silent(['7z', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE) 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(['unrar', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE) + 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() @@ -53,15 +54,15 @@ def __init__(self, filepath): def extract(self, targetdir): if not os.path.isdir(targetdir): raise OSError('Target directory doesn\'t exist.') - process = subprocess.run(['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': - process = subprocess.run(['unrar', 'x', '-y', '-x__MACOSX', '-x.DS_Store', '-xthumbs.db', '-xThumbs.db', self.filepath, targetdir] + 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 and platform.system() == 'Darwin': - process = subprocess.run(['unar', self.filepath, '-f', '-o', targetdir], + process = subprocess_run_silent(['unar', self.filepath, '-f', '-o', targetdir], stdout=PIPE, stderr=STDOUT) elif process.returncode != 0: raise OSError(EXTRACTION_ERROR) @@ -73,13 +74,13 @@ def extract(self, targetdir): def addFile(self, sourcefile): if self.type in ['RAR', 'RAR5']: raise NotImplementedError - process = subprocess.run(['7z', 'a', '-y', self.filepath, sourcefile], + process = subprocess_run_silent(['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(['7z', 'x', '-y', '-so', self.filepath, 'ComicInfo.xml'], + process = subprocess_run_silent(['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 2c1a3a9c..5ec79ae0 100644 --- a/kindlecomicconverter/shared.py +++ b/kindlecomicconverter/shared.py @@ -21,6 +21,7 @@ import os from hashlib import md5 from html.parser import HTMLParser +import subprocess from distutils.version import StrictVersion from re import split import sys @@ -135,3 +136,8 @@ def dependencyCheck(level): if len(missing) > 0: print('ERROR: ' + ', '.join(missing) + ' is not installed!') sys.exit(1) + +def subprocess_run_silent(command, **kwargs): + if (os.name == 'nt'): + kwargs.setdefault('creationflags', subprocess.CREATE_NO_WINDOW) + return subprocess.run(command, **kwargs)