Refactor subprocess calls

This commit is contained in:
Ozzieisaacs 2019-02-03 18:32:27 +01:00
parent 7f34073955
commit 36229076f7
5 changed files with 71 additions and 28 deletions

View File

@ -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):

View File

@ -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):

42
cps/subproc_wrapper.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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)

View File

@ -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

View File

@ -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:
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]
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:
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)
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)
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