From b528dab7111ca3b1e36f8da6d82d13ae3d5c669e Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Tue, 28 Nov 2023 08:20:14 -0800 Subject: [PATCH] Revert "fix files with ' or " by using arg lists instead of strings (#581)" (#628) This reverts commit 431862a2e9ba2353829e6c9ee32189ed2ffda74e. --- kindlecomicconverter/KCC_gui.py | 21 +++++++----- kindlecomicconverter/comic2ebook.py | 17 ++++++---- kindlecomicconverter/comicarchive.py | 49 ++++++++++++++++------------ 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/kindlecomicconverter/KCC_gui.py b/kindlecomicconverter/KCC_gui.py index 4d668e47..f14fbc48 100644 --- a/kindlecomicconverter/KCC_gui.py +++ b/kindlecomicconverter/KCC_gui.py @@ -29,7 +29,7 @@ # noinspection PyUnresolvedReferences from PyQt5 import QtGui, QtCore, QtWidgets, QtNetwork from xml.sax.saxutils import escape -from psutil import Process +from psutil import Popen, Process from copy import copy from distutils.version import StrictVersion from raven import Client @@ -839,22 +839,24 @@ def detectKindleGen(self, startup=False): os.chmod('/usr/local/bin/kindlegen', 0o755) except Exception: pass - kindleGenExitCode = subprocess.run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8') + kindleGenExitCode = Popen('kindlegen -locale en', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + kindleGenExitCode.communicate() if kindleGenExitCode.returncode == 0: self.kindleGen = True - versionCheck = subprocess.run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8') - for line in versionCheck.stdout.splitlines(): + versionCheck = Popen('kindlegen -locale en', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + for line in versionCheck.stdout: + line = line.decode("utf-8") if 'Amazon kindlegen' in line: versionCheck = line.split('V')[1].split(' ')[0] if StrictVersion(versionCheck) < StrictVersion('2.9'): self.addMessage('Your KindleGen' ' is outdated! MOBI conversion might fail.', 'warning') break - where_command = ['where', 'kindlegen.exe'] + where_command = 'where kindlegen.exe' if os.name == 'posix': - where_command = ['which', 'kindlegen'] - process = subprocess.run(where_command, stdout=PIPE, stderr=STDOUT, encoding='UTF-8') - locations = process.stdout.splitlines() + where_command = 'which kindlegen' + process = subprocess.run(where_command, stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + locations = process.stdout.decode('utf-8').split('\n') self.addMessage(f"KindleGen Found: {locations[0]}", 'info') else: self.kindleGen = False @@ -1037,7 +1039,8 @@ def __init__(self, kccapp, kccwindow): self.addMessage('Since you are a new user of KCC please see few ' 'important tips.', 'info') - process = subprocess.run(['7z'], stdout=PIPE, stderr=STDOUT) + process = Popen('7z', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + process.communicate() if process.returncode == 0 or process.returncode == 7: self.sevenzip = True else: diff --git a/kindlecomicconverter/comic2ebook.py b/kindlecomicconverter/comic2ebook.py index 78e4eebb..35c78a35 100755 --- a/kindlecomicconverter/comic2ebook.py +++ b/kindlecomicconverter/comic2ebook.py @@ -19,7 +19,6 @@ # import os -import subprocess import sys from argparse import ArgumentParser from time import strftime, gmtime @@ -36,7 +35,7 @@ from slugify import slugify as slugify_ext from PIL import Image from subprocess import STDOUT, PIPE -from psutil import virtual_memory, disk_usage +from psutil import Popen, virtual_memory, disk_usage from html import escape as hescape try: from PyQt5 import QtCore @@ -1103,12 +1102,14 @@ def checkTools(source): source = source.upper() if source.endswith('.CB7') or source.endswith('.7Z') or source.endswith('.RAR') or source.endswith('.CBR') or \ source.endswith('.ZIP') or source.endswith('.CBZ'): - process = subprocess.run(['7z'], stdout=PIPE, stderr=STDOUT) + process = Popen('7z', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + process.communicate() if process.returncode != 0 and process.returncode != 7: print('ERROR: 7z is missing!') sys.exit(1) if options.format == 'MOBI': - kindleGenExitCode = subprocess.run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT) + kindleGenExitCode = Popen('kindlegen -locale en', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + kindleGenExitCode.communicate() if kindleGenExitCode.returncode != 0: print('ERROR: KindleGen is missing!') sys.exit(1) @@ -1265,9 +1266,10 @@ def makeMOBIWorker(item): kindlegenError = '' try: if os.path.getsize(item) < 629145600: - output = subprocess.run(['kindlegen', '-dont_append_source', '-locale', 'en', item], - stdout=PIPE, stderr=STDOUT, encoding='UTF-8') - for line in output.stdout.splitlines(): + output = Popen('kindlegen -dont_append_source -locale en "' + item + '"', + stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + for line in output.stdout: + line = line.decode('utf-8') # ERROR: Generic error if "Error(" in line: kindlegenErrorCode = 1 @@ -1278,6 +1280,7 @@ def makeMOBIWorker(item): if kindlegenErrorCode > 0: break if ":I1036: Mobi file built successfully" in line: + output.communicate() break else: # ERROR: EPUB too big diff --git a/kindlecomicconverter/comicarchive.py b/kindlecomicconverter/comicarchive.py index 9fd05b87..325713ef 100644 --- a/kindlecomicconverter/comicarchive.py +++ b/kindlecomicconverter/comicarchive.py @@ -22,6 +22,7 @@ import platform import subprocess import distro +from psutil import Popen from shutil import move from subprocess import STDOUT, PIPE from xml.dom.minidom import parseString @@ -34,37 +35,41 @@ 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, encoding='UTF-8') - for line in process.stdout.splitlines(): - if 'Type =' in line: - self.type = line.rstrip().split(' = ')[1].upper() + process = Popen('7z l -y -p1 "' + self.filepath + '"', stderr=STDOUT, stdout=PIPE, stdin=PIPE, shell=True) + for line in process.stdout: + if b'Type =' in line: + self.type = line.rstrip().decode().split(' = ')[1].upper() break + process.communicate() if process.returncode != 0 and distro.id() == 'fedora': - process = subprocess.run(['unrar', 'l', '-y', '-p1', self.filepath], stderr=STDOUT, stdout=PIPE, encoding='UTF-8') - for line in process.stdout.splitlines(): - if 'Details: ' in line: - self.type = line.rstrip().split(' ')[1].upper() + process = Popen('unrar l -y -p1 "' + self.filepath + '"', stderr=STDOUT, stdout=PIPE, stdin=PIPE, shell=True) + for line in process.stdout: + if b'Details: ' in line: + self.type = line.rstrip().decode().split(' ')[1].upper() break + process.communicate() if process.returncode != 0: raise OSError(process.stdout.strip()) 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], - stdout=PIPE, stderr=STDOUT, encoding='UTF-8') + process = Popen('7z x -y -xr!__MACOSX -xr!.DS_Store -xr!thumbs.db -xr!Thumbs.db -o"' + targetdir + '" "' + + self.filepath + '"', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + process.communicate() 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] - , stdout=PIPE, stderr=STDOUT) + process = Popen('unrar x -y -x__MACOSX -x.DS_Store -xthumbs.db -xThumbs.db "' + self.filepath + '" "' + + targetdir + '"', stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + process.communicate() if process.returncode != 0: raise OSError('Failed to extract archive.') elif process.returncode != 0 and platform.system() == 'Darwin': - process = subprocess.run(['unar', self.filepath, '-f', '-o', targetdir], - stdout=PIPE, stderr=STDOUT, encoding='UTF-8') + process = subprocess.run(f"unar '{self.filepath}' -f -o '{targetdir}'", + stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) if process.returncode != 0: - raise Exception(process.stdout) + raise Exception(process.stdout.decode("utf-8")) elif process.returncode != 0: - raise OSError(process.stdout.strip()) + raise OSError('Failed to extract archive. Check if p7zip-rar is installed.') tdir = os.listdir(targetdir) if 'ComicInfo.xml' in tdir: tdir.remove('ComicInfo.xml') @@ -77,17 +82,19 @@ 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], - stdout=PIPE, stderr=STDOUT) + process = Popen('7z a -y "' + self.filepath + '" "' + sourcefile + '"', + stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + process.communicate() 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'], - stdout=PIPE, stderr=STDOUT, encoding='UTF-8') + process = Popen('7z x -y -so "' + self.filepath + '" ComicInfo.xml', + stdout=PIPE, stderr=STDOUT, stdin=PIPE, shell=True) + xml = process.communicate() if process.returncode != 0: raise OSError('Failed to extract archive.') try: - return parseString(process.stdout) + return parseString(xml[0]) except ExpatError: return None