Refactor subprocess calls

pull/823/head
Ozzieisaacs 6 years ago
parent 7f34073955
commit 36229076f7

@ -19,17 +19,19 @@
import os import os
import subprocess # import subprocess
import ub import ub
import re import re
from flask_babel import gettext as _ from flask_babel import gettext as _
from subproc_wrapper import process_open
def versionKindle(): def versionKindle():
versions = _(u'not installed') versions = _(u'not installed')
if os.path.exists(ub.config.config_converterpath): if os.path.exists(ub.config.config_converterpath):
try: 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() p.wait()
for lines in p.stdout.readlines(): for lines in p.stdout.readlines():
if isinstance(lines, bytes): if isinstance(lines, bytes):
@ -45,7 +47,8 @@ def versionCalibre():
versions = _(u'not installed') versions = _(u'not installed')
if os.path.exists(ub.config.config_converterpath): if os.path.exists(ub.config.config_converterpath):
try: 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() p.wait()
for lines in p.stdout.readlines(): for lines in p.stdout.readlines():
if isinstance(lines, bytes): if isinstance(lines, bytes):

@ -22,7 +22,6 @@
import db import db
import ub import ub
from flask import current_app as app from flask import current_app as app
# import logging
from tempfile import gettempdir from tempfile import gettempdir
import sys import sys
import os import os
@ -36,18 +35,15 @@ from flask_babel import gettext as _
from flask_login import current_user from flask_login import current_user
from babel.dates import format_datetime from babel.dates import format_datetime
from datetime import datetime from datetime import datetime
# import threading
import shutil import shutil
import requests import requests
# import zipfile
try: try:
import gdriveutils as gd import gdriveutils as gd
except ImportError: except ImportError:
pass pass
import web import web
# import server
import random import random
import subprocess from subproc_wrapper import process_open
try: try:
import unidecode import unidecode
@ -496,7 +492,7 @@ def check_unrar(unrarLocation):
try: try:
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
unrarLocation = unrarLocation.encode(sys.getfilesystemencoding()) unrarLocation = unrarLocation.encode(sys.getfilesystemencoding())
p = subprocess.Popen(unrarLocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = process_open(unrarLocation)
p.wait() p.wait()
for lines in p.stdout.readlines(): for lines in p.stdout.readlines():
if isinstance(lines, bytes): if isinstance(lines, bytes):

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

@ -84,7 +84,6 @@ from flask_dance.consumer import oauth_authorized, oauth_error
from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import NoResultFound
from oauth import OAuthBackend from oauth import OAuthBackend
import hashlib import hashlib
>>>>>>> master
try: try:
from googleapiclient.errors import HttpError from googleapiclient.errors import HttpError

@ -31,7 +31,9 @@ import web
from flask_babel import gettext as _ from flask_babel import gettext as _
import re import re
import gdriveutils as gd import gdriveutils as gd
import subprocess # import subprocess
from subproc_wrapper import process_open
try: try:
from StringIO import StringIO from StringIO import StringIO
@ -271,36 +273,37 @@ class WorkerThread(threading.Thread):
# check which converter to use kindlegen is "1" # check which converter to use kindlegen is "1"
if format_old_ext == '.epub' and format_new_ext == '.mobi': if format_old_ext == '.epub' and format_new_ext == '.mobi':
if web.ub.config.config_ebookconverter == 1: 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"' command = web.ub.config.config_converterpath + u' "' + file_path + u'.epub"'
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
command = command.encode(sys.getfilesystemencoding()) command = command.encode(sys.getfilesystemencoding())
else: else:'''
command = [web.ub.config.config_converterpath, file_path + u'.epub'] command = [web.ub.config.config_converterpath, file_path + u'.epub']
if sys.version_info < (3, 0): quotes = (1)
command = [x.encode(sys.getfilesystemencoding()) for x in command]
if web.ub.config.config_ebookconverter == 2: if web.ub.config.config_ebookconverter == 2:
# Linux py2.7 encode as list without quotes no empty element for parameters # 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 # 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 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 # windows py 3.x no encode and as string with quotes empty element for parameters is okay
# separate handling for windows and linux # 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'" "' + \ command = web.ub.config.config_converterpath + u' "' + file_path + format_old_ext + u'" "' + \
file_path + format_new_ext + u'" ' + web.ub.config.config_calibre file_path + format_new_ext + u'" ' + web.ub.config.config_calibre
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
command = command.encode(sys.getfilesystemencoding()) command = command.encode(sys.getfilesystemencoding())
else: else:'''
command = [web.ub.config.config_converterpath, (file_path + format_old_ext), command = [web.ub.config.config_converterpath, (file_path + format_old_ext),
(file_path + format_new_ext)] (file_path + format_new_ext)]
index = 3
if web.ub.config.config_calibre: if web.ub.config.config_calibre:
parameters = web.ub.config.config_calibre.split(" ") parameters = web.ub.config.config_calibre.split(" ")
for param in parameters: for param in parameters:
command.append(param) command.append(param)
if sys.version_info < (3, 0): quotes.append(index)
command = [x.encode(sys.getfilesystemencoding()) for x in command] index += 1
p = process_open(command, quotes)
p = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True) # p = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True)
except OSError as e: except OSError as e:
self._handleError(_(u"Ebook-converter failed: %(error)s", error=e)) self._handleError(_(u"Ebook-converter failed: %(error)s", error=e))
return return

Loading…
Cancel
Save