mimic parent arguments during spawn process (#99)

This commit is contained in:
2023-05-30 18:09:35 +03:00
committed by GitHub
parent d99091a3b4
commit 54a68279be
6 changed files with 113 additions and 16 deletions

View File

@ -79,5 +79,6 @@ class UnsafeCommands(Handler):
"""
# should never fail
# pylint: disable=protected-access
subparser = next(action for action in parser._actions if isinstance(action, argparse._SubParsersAction))
return sorted(action_name for action_name, action in subparser.choices.items() if action.get_default("unsafe"))
subparser = next((action for action in parser._actions if isinstance(action, argparse._SubParsersAction)), None)
actions = subparser.choices if subparser is not None else {}
return sorted(action_name for action_name, action in actions.items() if action.get_default("unsafe"))

View File

@ -19,6 +19,8 @@
#
import argparse
from collections.abc import Generator
from ahriman.application.handlers import Handler
from ahriman.core.configuration import Configuration
from ahriman.core.spawn import Spawn
@ -31,6 +33,7 @@ class Web(Handler):
ALLOW_AUTO_ARCHITECTURE_RUN = False
ALLOW_MULTI_ARCHITECTURE_RUN = False # required to be able to spawn external processes
COMMAND_ARGS_WHITELIST = ["force", "log_handler", ""]
@classmethod
def run(cls, args: argparse.Namespace, architecture: str, configuration: Configuration, *,
@ -48,7 +51,8 @@ class Web(Handler):
# we are using local import for optional dependencies
from ahriman.web.web import run_server, setup_service
spawner = Spawn(args.parser(), architecture, configuration)
spawner_args = Web.extract_arguments(args, architecture, configuration)
spawner = Spawn(args.parser(), architecture, list(spawner_args))
spawner.start()
application = setup_service(architecture, configuration, spawner)
@ -57,3 +61,33 @@ class Web(Handler):
# terminate spawn process at the last
spawner.stop()
spawner.join()
@staticmethod
def extract_arguments(args: argparse.Namespace, architecture: str,
configuration: Configuration) -> Generator[str, None, None]:
"""
extract list of arguments used for current command, except for command specific ones
Args:
args(argparse.Namespace): command line args
architecture(str): repository architecture
configuration(Configuration): configuration instance
Returns:
Generator[str, None, None]: command line arguments which were used for this specific command
"""
# read architecture from the same argument list
yield from ["--architecture", architecture]
# read configuration path from current settings
if (configuration_path := configuration.path) is not None:
yield from ["--configuration", str(configuration_path)]
# arguments from command line
if args.force:
yield "--force"
if args.log_handler is not None:
yield from ["--log-handler", args.log_handler.value]
if args.quiet:
yield "--quiet"
if args.unsafe:
yield "--unsafe"

View File

@ -26,7 +26,6 @@ from collections.abc import Callable, Iterable
from multiprocessing import Process, Queue
from threading import Lock, Thread
from ahriman.core.configuration import Configuration
from ahriman.core.log import LazyLogging
from ahriman.models.package_source import PackageSource
@ -39,23 +38,24 @@ class Spawn(Thread, LazyLogging):
Attributes:
active(dict[str, Process]): map of active child processes required to avoid zombies
architecture(str): repository architecture
configuration(Configuration): configuration instance
command_arguments(list[str]): base command line arguments
queue(Queue[tuple[str, bool]]): multiprocessing queue to read updates from processes
"""
def __init__(self, args_parser: argparse.ArgumentParser, architecture: str, configuration: Configuration) -> None:
def __init__(self, args_parser: argparse.ArgumentParser, architecture: str, command_arguments: list[str]) -> None:
"""
default constructor
Args:
args_parser(argparse.ArgumentParser): command line parser for the application
architecture(str): repository architecture
configuration(Configuration): configuration instance
command_arguments(list[str]): base command line arguments
"""
Thread.__init__(self, name="spawn")
self.architecture = architecture
self.args_parser = args_parser
self.configuration = configuration
self.command_arguments = command_arguments
self.lock = Lock()
self.active: dict[str, Process] = {}
@ -88,9 +88,7 @@ class Spawn(Thread, LazyLogging):
**kwargs(str): named command arguments
"""
# default arguments
arguments = ["--architecture", self.architecture]
if self.configuration.path is not None:
arguments.extend(["--configuration", str(self.configuration.path)])
arguments = self.command_arguments[:]
# positional command arguments
arguments.append(command)
arguments.extend(args)