Skip to content

Commit

Permalink
hide subprocess consoles on Windows (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
VampiroMedicado authored Jan 2, 2024
1 parent 169a41e commit e8502f0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions kindlecomicconverter/KCC_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -1026,7 +1026,7 @@ def __init__(self, kccapp, kccwindow):
'<a href="https://github.com/ciromattia/kcc/wiki/Important-tips">important tips</a>.',
'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
Expand Down
8 changes: 4 additions & 4 deletions kindlecomicconverter/comic2ebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions kindlecomicconverter/comicarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'

Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions kindlecomicconverter/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit e8502f0

Please sign in to comment.