diff --git a/src/qlever/commands/start.py b/src/qlever/commands/start.py index 4059bec3..c1613347 100644 --- a/src/qlever/commands/start.py +++ b/src/qlever/commands/start.py @@ -10,7 +10,7 @@ from qlever.commands.warmup import WarmupCommand from qlever.containerize import Containerize from qlever.log import log -from qlever.util import is_qlever_server_alive, run_command, name_from_path +from qlever.util import is_qlever_server_alive, run_command class StartCommand(QleverCommand): @@ -59,21 +59,19 @@ def additional_arguments(self, subparser) -> None: help="Do not execute the warmup command") def execute(self, args) -> bool: - server_binary = name_from_path(args.server_binary) - # Kill existing server with the same name if so desired. # # TODO: This is currently disabled because I never used it once over # the past weeks and it is not clear to me what the use case is. if False: # or args.kill_existing_with_same_name: - args.cmdline_regex = f"^{server_binary}.* -i {args.name}" + args.cmdline_regex = f"^{args.server_binary}.* -i {args.name}" args.no_containers = True StopCommand().execute(args) log.info("") # Kill existing server on the same port if so desired. if args.kill_existing_with_same_port: - args.cmdline_regex = f"^{server_binary}.* -p {args.port}" + args.cmdline_regex = f"^{args.server_binary}.* -p {args.port}" args.no_containers = True if not StopCommand().execute(args): log.error("Stopping the existing server failed") @@ -144,7 +142,7 @@ def execute(self, args) -> bool: "--kill-existing-with-same-port`") # Show output of status command. - args.cmdline_regex = f"^{server_binary}.* -p *{port}" + args.cmdline_regex = f"^{args.server_binary}.* -p *{port}" log.info("") StatusCommand().execute(args) diff --git a/src/qlever/commands/status.py b/src/qlever/commands/status.py index 7bce1f5e..cd0475a3 100644 --- a/src/qlever/commands/status.py +++ b/src/qlever/commands/status.py @@ -3,7 +3,7 @@ import psutil from qlever.command import QleverCommand -from qlever.util import show_process_info, name_from_path +from qlever.util import show_process_info class StatusCommand(QleverCommand): @@ -25,16 +25,21 @@ def relevant_qleverfile_arguments(self) -> dict[str: list[str]]: def additional_arguments(self, subparser) -> None: subparser.add_argument("--cmdline-regex", - default="^(ServerMain|IndexBuilderMain)", + default="^(%%SERVER_BINARY%%|%%INDEX_BINARY%%)", help="Show only processes where the command " "line matches this regex") def execute(self, args) -> bool: - server_binary = name_from_path(args.server_binary) - index_binary = name_from_path(args.index_binary) - cmdline_regex = (args.cmdline_regex - .replace("ServerMain", server_binary) - .replace("IndexBuilderMain", index_binary)) + cmdline_regex = args.cmdline_regex + # Other commands call status with a custom `cmdline_regex` that contains + # less or no variables. Doing the replacement on-demand has the benefit + # that only the variables that are actually used have to be provided by + # the calling command. For example: the `cmdline_regex` used by start + # has no variables and requiring the index binary for it would be strange. + if "%%SERVER_BINARY%%" in cmdline_regex: + cmdline_regex = cmdline_regex.replace("%%SERVER_BINARY%%", args.server_binary) + if "%%INDEX_BINARY%%" in cmdline_regex: + cmdline_regex = cmdline_regex.replace("%%INDEX_BINARY%%", args.index_binary) # Show action description. self.show(f"Show all processes on this machine where " diff --git a/src/qlever/commands/stop.py b/src/qlever/commands/stop.py index dedd06a7..f5e5abfa 100644 --- a/src/qlever/commands/stop.py +++ b/src/qlever/commands/stop.py @@ -8,7 +8,7 @@ from qlever.commands.status import StatusCommand from qlever.containerize import Containerize from qlever.log import log -from qlever.util import show_process_info, name_from_path +from qlever.util import show_process_info class StopCommand(QleverCommand): @@ -33,7 +33,7 @@ def relevant_qleverfile_arguments(self) -> dict[str: list[str]]: def additional_arguments(self, subparser) -> None: subparser.add_argument("--cmdline-regex", - default="ServerMain.* -i [^ ]*%%NAME%%", + default="%%SERVER_BINARY%%.* -i [^ ]*%%NAME%%", help="Show only processes where the command " "line matches this regex") subparser.add_argument("--no-containers", action="store_true", @@ -42,11 +42,9 @@ def additional_arguments(self, subparser) -> None: "native processes") def execute(self, args) -> bool: - server_binary = name_from_path(args.server_binary) - # Show action description. cmdline_regex = args.cmdline_regex.replace("%%NAME%%", args.name) - cmdline_regex = cmdline_regex.replace("ServerMain", server_binary) + cmdline_regex = cmdline_regex.replace("%%SERVER_BINARY%%", args.server_binary) description = f"Checking for processes matching \"{cmdline_regex}\"" if not args.no_containers: description += (f" and for Docker container with name " @@ -99,7 +97,7 @@ def execute(self, args) -> bool: message = "No matching process found" if args.no_containers else \ "No matching process or container found" log.error(message) - args.cmdline_regex = f"^{server_binary}.* -i [^ ]*" + args.cmdline_regex = f"^{args.server_binary}.* -i [^ ]*" log.info("") StatusCommand().execute(args) return True diff --git a/src/qlever/util.py b/src/qlever/util.py index c92ab4b1..d79a9413 100644 --- a/src/qlever/util.py +++ b/src/qlever/util.py @@ -225,11 +225,3 @@ def format_size(bytes, suffix="B"): if bytes < factor: return f"{bytes:.2f} {unit}{suffix}" bytes /= factor - -def name_from_path(path: str) -> str: - """ - Helper function that returns the name of the file from its path. - E.g. /qlever/ServerMain -> ServerMain - """ - return Path(path).name -