From 36229076f7584c1b5118deb61f53effb7de02b22 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 3 Feb 2019 18:32:27 +0100 Subject: [PATCH] Refactor subprocess calls --- cps/converter.py | 9 ++++++--- cps/helper.py | 8 ++------ cps/subproc_wrapper.py | 42 ++++++++++++++++++++++++++++++++++++++++++ cps/web.py | 1 - cps/worker.py | 39 +++++++++++++++++++++------------------ 5 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 cps/subproc_wrapper.py diff --git a/cps/converter.py b/cps/converter.py index bfcf0879..29c371bb 100644 --- a/cps/converter.py +++ b/cps/converter.py @@ -19,17 +19,19 @@ import os -import subprocess +# import subprocess import ub import re from flask_babel import gettext as _ +from subproc_wrapper import process_open def versionKindle(): versions = _(u'not installed') if os.path.exists(ub.config.config_converterpath): try: - p = subprocess.Popen(ub.config.config_converterpath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = process_open(ub.config.config_converterpath) + # p = subprocess.Popen(ub.config.config_converterpath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() for lines in p.stdout.readlines(): if isinstance(lines, bytes): @@ -45,7 +47,8 @@ def versionCalibre(): versions = _(u'not installed') if os.path.exists(ub.config.config_converterpath): try: - p = subprocess.Popen([ub.config.config_converterpath, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = process_open([ub.config.config_converterpath, '--version']) + # p = subprocess.Popen([ub.config.config_converterpath, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() for lines in p.stdout.readlines(): if isinstance(lines, bytes): diff --git a/cps/helper.py b/cps/helper.py index 31d82e73..0f489942 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -22,7 +22,6 @@ import db import ub from flask import current_app as app -# import logging from tempfile import gettempdir import sys import os @@ -36,18 +35,15 @@ from flask_babel import gettext as _ from flask_login import current_user from babel.dates import format_datetime from datetime import datetime -# import threading import shutil import requests -# import zipfile try: import gdriveutils as gd except ImportError: pass import web -# import server import random -import subprocess +from subproc_wrapper import process_open try: import unidecode @@ -496,7 +492,7 @@ def check_unrar(unrarLocation): try: if sys.version_info < (3, 0): unrarLocation = unrarLocation.encode(sys.getfilesystemencoding()) - p = subprocess.Popen(unrarLocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p = process_open(unrarLocation) p.wait() for lines in p.stdout.readlines(): if isinstance(lines, bytes): diff --git a/cps/subproc_wrapper.py b/cps/subproc_wrapper.py new file mode 100644 index 00000000..64204a56 --- /dev/null +++ b/cps/subproc_wrapper.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web) +# Copyright (C) 2018-2019 OzzieIsaacs +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +import subprocess +import os +import sys + + +def process_open(command, quotes=(), env=None, sout=subprocess.PIPE): + # Linux py2.7 encode as list without quotes no empty element for parameters + # linux py3.x no encode and as list without quotes no empty element for parameters + # windows py2.7 encode as string with quotes empty element for parameters is okay + # windows py 3.x no encode and as string with quotes empty element for parameters is okay + # separate handling for windows and linux + if os.name == 'nt': + for key, element in enumerate(command): + if key in quotes: + command[key] = '"' + element + '"' + exc_command = " ".join(command) + if sys.version_info < (3, 0): + exc_command = exc_command.encode(sys.getfilesystemencoding()) + else: + if sys.version_info < (3, 0): + exc_command = [x.encode(sys.getfilesystemencoding()) for x in command] + + # return subprocess.Popen(exc_command, shell=False, stdout=subprocess.PIPE, universal_newlines=True, env=env) + return subprocess.Popen(exc_command, shell=False, stdout=sout, universal_newlines=True, env=env) diff --git a/cps/web.py b/cps/web.py index caa8415c..343d5097 100644 --- a/cps/web.py +++ b/cps/web.py @@ -84,7 +84,6 @@ from flask_dance.consumer import oauth_authorized, oauth_error from sqlalchemy.orm.exc import NoResultFound from oauth import OAuthBackend import hashlib ->>>>>>> master try: from googleapiclient.errors import HttpError diff --git a/cps/worker.py b/cps/worker.py index 65d4a850..80222f21 100644 --- a/cps/worker.py +++ b/cps/worker.py @@ -31,7 +31,9 @@ import web from flask_babel import gettext as _ import re import gdriveutils as gd -import subprocess +# import subprocess +from subproc_wrapper import process_open + try: from StringIO import StringIO @@ -271,36 +273,37 @@ class WorkerThread(threading.Thread): # check which converter to use kindlegen is "1" if format_old_ext == '.epub' and format_new_ext == '.mobi': if web.ub.config.config_ebookconverter == 1: - if os.name == 'nt': + '''if os.name == 'nt': command = web.ub.config.config_converterpath + u' "' + file_path + u'.epub"' if sys.version_info < (3, 0): command = command.encode(sys.getfilesystemencoding()) - else: - command = [web.ub.config.config_converterpath, file_path + u'.epub'] - if sys.version_info < (3, 0): - command = [x.encode(sys.getfilesystemencoding()) for x in command] + else:''' + command = [web.ub.config.config_converterpath, file_path + u'.epub'] + quotes = (1) if web.ub.config.config_ebookconverter == 2: # Linux py2.7 encode as list without quotes no empty element for parameters # linux py3.x no encode and as list without quotes no empty element for parameters # windows py2.7 encode as string with quotes empty element for parameters is okay # windows py 3.x no encode and as string with quotes empty element for parameters is okay # separate handling for windows and linux - if os.name == 'nt': + quotes = (1,2) + '''if os.name == 'nt': command = web.ub.config.config_converterpath + u' "' + file_path + format_old_ext + u'" "' + \ file_path + format_new_ext + u'" ' + web.ub.config.config_calibre if sys.version_info < (3, 0): command = command.encode(sys.getfilesystemencoding()) - else: - command = [web.ub.config.config_converterpath, (file_path + format_old_ext), - (file_path + format_new_ext)] - if web.ub.config.config_calibre: - parameters = web.ub.config.config_calibre.split(" ") - for param in parameters: - command.append(param) - if sys.version_info < (3, 0): - command = [x.encode(sys.getfilesystemencoding()) for x in command] - - p = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True) + else:''' + command = [web.ub.config.config_converterpath, (file_path + format_old_ext), + (file_path + format_new_ext)] + index = 3 + if web.ub.config.config_calibre: + parameters = web.ub.config.config_calibre.split(" ") + for param in parameters: + command.append(param) + quotes.append(index) + index += 1 + p = process_open(command, quotes) + # p = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True) except OSError as e: self._handleError(_(u"Ebook-converter failed: %(error)s", error=e)) return