Skip to content

Commit

Permalink
stripping the path doesn't go well with some regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Qup42 committed Dec 17, 2024
1 parent 6748235 commit cb8e599
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
10 changes: 4 additions & 6 deletions src/qlever/commands/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand Down
19 changes: 12 additions & 7 deletions src/qlever/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 "
Expand Down
10 changes: 4 additions & 6 deletions src/qlever/commands/stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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",
Expand All @@ -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 "
Expand Down Expand Up @@ -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
8 changes: 0 additions & 8 deletions src/qlever/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cb8e599

Please sign in to comment.