|
|
|
@ -22,6 +22,7 @@ import os
|
|
|
|
|
import errno
|
|
|
|
|
import signal
|
|
|
|
|
import socket
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from gevent.pywsgi import WSGIServer
|
|
|
|
@ -136,6 +137,64 @@ class WebServer(object):
|
|
|
|
|
|
|
|
|
|
return sock, _readable_listen_address(*address)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_args_for_reloading(self):
|
|
|
|
|
"""Determine how the script was executed, and return the args needed
|
|
|
|
|
to execute it again in a new process.
|
|
|
|
|
Code from https://github.com/pyload/pyload. Author GammaC0de, voulter
|
|
|
|
|
"""
|
|
|
|
|
rv = [sys.executable]
|
|
|
|
|
py_script = sys.argv[0]
|
|
|
|
|
args = sys.argv[1:]
|
|
|
|
|
# Need to look at main module to determine how it was executed.
|
|
|
|
|
__main__ = sys.modules["__main__"]
|
|
|
|
|
|
|
|
|
|
# The value of __package__ indicates how Python was called. It may
|
|
|
|
|
# not exist if a setuptools script is installed as an egg. It may be
|
|
|
|
|
# set incorrectly for entry points created with pip on Windows.
|
|
|
|
|
if getattr(__main__, "__package__", None) is None or (
|
|
|
|
|
os.name == "nt"
|
|
|
|
|
and __main__.__package__ == ""
|
|
|
|
|
and not os.path.exists(py_script)
|
|
|
|
|
and os.path.exists(f"{py_script}.exe")
|
|
|
|
|
):
|
|
|
|
|
# Executed a file, like "python app.py".
|
|
|
|
|
py_script = os.path.abspath(py_script)
|
|
|
|
|
|
|
|
|
|
if os.name == "nt":
|
|
|
|
|
# Windows entry points have ".exe" extension and should be
|
|
|
|
|
# called directly.
|
|
|
|
|
if not os.path.exists(py_script) and os.path.exists(f"{py_script}.exe"):
|
|
|
|
|
py_script += ".exe"
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
os.path.splitext(sys.executable)[1] == ".exe"
|
|
|
|
|
and os.path.splitext(py_script)[1] == ".exe"
|
|
|
|
|
):
|
|
|
|
|
rv.pop(0)
|
|
|
|
|
|
|
|
|
|
rv.append(py_script)
|
|
|
|
|
else:
|
|
|
|
|
# Executed a module, like "python -m module".
|
|
|
|
|
if sys.argv[0] == "-m":
|
|
|
|
|
args = sys.argv
|
|
|
|
|
else:
|
|
|
|
|
if os.path.isfile(py_script):
|
|
|
|
|
# Rewritten by Python from "-m script" to "/path/to/script.py".
|
|
|
|
|
py_module = __main__.__package__
|
|
|
|
|
name = os.path.splitext(os.path.basename(py_script))[0]
|
|
|
|
|
|
|
|
|
|
if name != "__main__":
|
|
|
|
|
py_module += f".{name}"
|
|
|
|
|
else:
|
|
|
|
|
# Incorrectly rewritten by pydevd debugger from "-m script" to "script".
|
|
|
|
|
py_module = py_script
|
|
|
|
|
|
|
|
|
|
rv.extend(("-m", py_module.lstrip(".")))
|
|
|
|
|
|
|
|
|
|
rv.extend(args)
|
|
|
|
|
return rv
|
|
|
|
|
|
|
|
|
|
def _start_gevent(self):
|
|
|
|
|
ssl_args = self.ssl_args or {}
|
|
|
|
|
|
|
|
|
@ -199,11 +258,8 @@ class WebServer(object):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
log.info("Performing restart of Calibre-Web")
|
|
|
|
|
arguments = list(sys.argv)
|
|
|
|
|
arguments.insert(0, sys.executable)
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
|
arguments = ["\"%s\"" % a for a in arguments]
|
|
|
|
|
os.execv(sys.executable, arguments)
|
|
|
|
|
args = self._get_args_for_reloading()
|
|
|
|
|
subprocess.call(args, close_fds=True)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _killServer(self, __, ___):
|
|
|
|
|