Fix startup checks

This commit is contained in:
Blallo 2021-09-15 17:53:57 +02:00
parent acc966b488
commit 26181d083f
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F

View file

@ -4,6 +4,7 @@ import os.path
import sys import sys
from argparse import Action, ArgumentParser from argparse import Action, ArgumentParser
from datetime import datetime from datetime import datetime
import urllib.request
from . import forge, maint, server from . import forge, maint, server
from .config_manager import get_config from .config_manager import get_config
@ -12,18 +13,27 @@ logging.basicConfig(stream=sys.stdout)
logger = logging.getLogger("cli") logger = logging.getLogger("cli")
CWD = os.getcwd() CWD = os.getcwd()
OK_CODES = [200, 301, 302]
def is_writable(d):
return os.access(d, os.W_OK)
def pre_check_permissions(): def pre_check_permissions():
def is_writable(d): audio_input = get_config()["AUDIO_INPUT"]
return os.access(d, os.W_OK) if audio_input.startswith("http://") or audio_input.startswith("https://"):
with urllib.request.urlopen(audio_input) as req:
if is_writable(get_config()["AUDIO_INPUT"]): if req.code not in OK_CODES:
yield "Audio input '%s' writable" % get_config()["AUDIO_INPUT"] yield f"Audio input {audio_input} not accessible"
if not os.access(get_config()["AUDIO_INPUT"], os.R_OK): sys.exit(10)
yield "Audio input '%s' unreadable" % get_config()["AUDIO_INPUT"] else:
sys.exit(10) if is_writable(audio_input):
if is_writable(os.getcwd()): yield "Audio input '%s' writable" % audio_input
if not os.access(audio_input, os.R_OK):
yield "Audio input '%s' unreadable" % audio_input
sys.exit(10)
if is_writable(CWD):
yield "Code writable" yield "Code writable"
if not is_writable(get_config()["AUDIO_OUTPUT"]): if not is_writable(get_config()["AUDIO_OUTPUT"]):
yield "Audio output '%s' not writable" % get_config()["AUDIO_OUTPUT"] yield "Audio output '%s' not writable" % get_config()["AUDIO_OUTPUT"]
@ -72,7 +82,8 @@ def common_pre():
continue continue
path = os.path.realpath(conf) path = os.path.realpath(conf)
if not os.path.exists(path): if not os.path.exists(path):
logger.warn("Configuration file '%s' does not exist; skipping" % path) logger.warn(
"Configuration file '%s' does not exist; skipping" % path)
continue continue
configs.append(path) configs.append(path)
if getattr(sys, "frozen", False): if getattr(sys, "frozen", False):