This commit is contained in:
2024-11-17 18:05:49 +01:00
parent 4983db7cad
commit 895849ed93
2 changed files with 28 additions and 12 deletions

View File

@ -11,8 +11,8 @@ import subprocess
import sys
import tempfile
import textwrap
import time
import threading
import time
from typing import (
IO,
Any,
@ -150,16 +150,21 @@ class _BaseController:
self._own_ports.remove((hostname, port))
def execute(
self, command: Sequence[Union[str, Path]], proc_name: Optional[str], **kwargs: Any
self,
command: Sequence[Union[str, Path]],
proc_name: Optional[str] = None,
**kwargs: Any,
) -> subprocess.Popen:
output_to = None if self.debug_mode else subprocess.DEVNULL
proc_name = proc_name or command[0]
proc_name = proc_name or str(command[0])
kwargs.setdefault("stdout", output_to)
kwargs.setdefault("stderr", output_to)
stream_stdout = stream_stderr = None
if kwargs["stdout"] in (None, subprocess.STDOUT):
kwargs["stdout"] = subprocess.PIPE
def stream_stdout():
def stream_stdout() -> None:
assert proc.stdout is not None # for mypy
for line in proc.stdout:
prefix = f"{time.time():.3f} {proc_name} ".encode()
try:
@ -167,16 +172,20 @@ class _BaseController:
except ValueError:
# "I/O operation on closed file"
pass
if kwargs["stderr"] in (subprocess.STDOUT, None):
kwargs["stdout"] = subprocess.PIPE
def stream_stderr():
for line in proc.stdout:
def stream_stderr() -> None:
assert proc.stderr is not None # for mypy
for line in proc.stderr:
prefix = f"{time.time():.3f} {proc_name} ".encode()
try:
sys.stdout.buffer.write(prefix + line)
except ValueError:
# "I/O operation on closed file"
pass
proc = subprocess.Popen(command, **kwargs)
if stream_stdout is not None:
threading.Thread(target=stream_stdout, name="stream_stdout").start()

View File

@ -85,7 +85,13 @@ def certs_dir() -> Path:
certs_dir = tempfile.TemporaryDirectory()
(Path(certs_dir.name) / "gen_certs.sh").write_text(GEN_CERTS)
subprocess.run(
["bash", "gen_certs.sh", "My.Little.Server", "My.Little.History", "My.Little.Services"],
[
"bash",
"gen_certs.sh",
"My.Little.Server",
"My.Little.History",
"My.Little.Services",
],
cwd=certs_dir.name,
check=True,
)
@ -585,7 +591,9 @@ class SableHistoryController(BaseServicesController):
def run(self, protocol: str, server_hostname: str, server_port: int) -> None:
assert protocol == "sable"
assert self.server_controller.directory is not None
history_db_url=os.environ.get("PIFPAF_POSTGRESQL_URL") or os.environ.get("IRCTEST_POSTGRESQL_URL")
history_db_url = os.environ.get("PIFPAF_POSTGRESQL_URL") or os.environ.get(
"IRCTEST_POSTGRESQL_URL"
)
assert history_db_url, (
"Cannot find a postgresql database to use as backend for sable_history. "
"Either set the IRCTEST_POSTGRESQL_URL env var to a libpq URL, or "
@ -594,10 +602,9 @@ class SableHistoryController(BaseServicesController):
)
with self.server_controller.open_file("configs/history_server.conf") as fd:
fd.write(HISTORY_SERVER_CONFIG % {
**self.server_controller.template_vars,
"history_db_url": history_db_url,
})
vals = dict(self.server_controller.template_vars)
vals["history_db_url"] = history_db_url
fd.write(HISTORY_SERVER_CONFIG % vals)
self.proc = self.execute(
[