Skip to content

Commit

Permalink
Refactor subprocess calls
Browse files Browse the repository at this point in the history
  • Loading branch information
OzzieIsaacs committed Feb 3, 2019
1 parent 7f34073 commit 3622907
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
9 changes: 6 additions & 3 deletions cps/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
8 changes: 2 additions & 6 deletions cps/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,18 +35,15 @@
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
Expand Down Expand Up @@ -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):
Expand Down
42 changes: 42 additions & 0 deletions cps/subproc_wrapper.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion cps/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
from sqlalchemy.orm.exc import NoResultFound
from oauth import OAuthBackend
import hashlib
>>>>>>> master

try:
from googleapiclient.errors import HttpError
Expand Down
39 changes: 21 additions & 18 deletions cps/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
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
Expand Down Expand Up @@ -271,36 +273,37 @@ def _convert_ebook_format(self):
# 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
Expand Down

0 comments on commit 3622907

Please sign in to comment.