2019-07-13 18:45:48 +00:00
|
|
|
# -*- 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 sys
|
|
|
|
import os
|
|
|
|
import subprocess
|
2021-07-11 07:19:46 +00:00
|
|
|
import re
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-05-24 15:24:54 +00:00
|
|
|
def process_open(command, quotes=(), env=None, sout=subprocess.PIPE, serr=subprocess.PIPE, newlines=True):
|
2019-07-13 18:45:48 +00:00
|
|
|
# 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)
|
|
|
|
else:
|
2021-10-04 16:26:46 +00:00
|
|
|
exc_command = [x for x in command]
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-03-15 12:48:05 +00:00
|
|
|
return subprocess.Popen(exc_command, shell=False, stdout=sout, stderr=serr, universal_newlines=newlines, env=env) # nosec
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-07-11 07:19:46 +00:00
|
|
|
def process_wait(command, serr=subprocess.PIPE, pattern=""):
|
2020-04-19 17:08:58 +00:00
|
|
|
# Run command, wait for process to terminate, and return an iterator over lines of its output.
|
2020-05-24 15:24:54 +00:00
|
|
|
newlines = os.name != 'nt'
|
2021-07-11 07:19:46 +00:00
|
|
|
ret_val = ""
|
2020-05-24 15:24:54 +00:00
|
|
|
p = process_open(command, serr=serr, newlines=newlines)
|
2019-07-13 18:45:48 +00:00
|
|
|
p.wait()
|
2020-04-19 17:08:58 +00:00
|
|
|
for line in p.stdout.readlines():
|
|
|
|
if isinstance(line, bytes):
|
2021-07-11 05:38:15 +00:00
|
|
|
line = line.decode('utf-8', errors="ignore")
|
2021-07-11 07:19:46 +00:00
|
|
|
match = re.search(pattern, line, re.IGNORECASE)
|
|
|
|
if match and ret_val == "":
|
|
|
|
ret_val = match
|
2021-07-11 05:38:15 +00:00
|
|
|
break
|
2021-07-11 07:19:46 +00:00
|
|
|
p.stdout.close()
|
|
|
|
p.stderr.close()
|
|
|
|
return ret_val
|