From 5ffd18d132d9c74f39c9a1394ac9fb7ee45f83a5 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Thu, 9 Feb 2023 20:32:35 -0500 Subject: [PATCH] machine: update install validation to use package If the "moonraker" entry point is available use it as an executable. Otherwise launch the module through the "-m" switch. Signed-off-by: Eric Callahan --- moonraker/components/machine.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/moonraker/components/machine.py b/moonraker/components/machine.py index da18319..75ded07 100644 --- a/moonraker/components/machine.py +++ b/moonraker/components/machine.py @@ -1474,14 +1474,13 @@ Type=simple User=%s SupplementaryGroups=moonraker-admin RemainAfterExit=yes -WorkingDirectory=%s EnvironmentFile=%s ExecStart=%s $MOONRAKER_ARGS Restart=always RestartSec=10 """ # noqa: E122 -ENVIRONMENT = "MOONRAKER_ARGS=\"%s/moonraker/moonraker.py %s\"" +ENVIRONMENT = "MOONRAKER_ARGS=\"%s%s\"%s" TEMPLATE_NAME = "password_request.html" class ValidationError(Exception): @@ -1656,7 +1655,6 @@ class InstallValidator: tmp_svc = pathlib.Path( tempfile.gettempdir() ).joinpath(f"{unit}-tmp.svc") - src_path = source_info.source_path() # Create local environment file sysd_data = self.data_path.joinpath("systemd") if not sysd_data.exists(): @@ -1694,13 +1692,29 @@ class InstallValidator: service_bkp = svc_bkp_path.joinpath(svc_dest.name) shutil.copy2(str(svc_dest), str(service_bkp)) # write temporary service file + src_path = source_info.source_path() + exec_path = pathlib.Path(sys.executable) + py_exec = exec_path.parent.joinpath("python") + pythonpath = "" + src_arg = "" + if exec_path.name == "python" or py_exec.is_file(): + # Default to loading via the python executable. This + # makes it possible to switch between git repos, pip + # releases and git releases without reinstalling the + # service. + exec_path = py_exec + src_arg = "-m moonraker " + if not source_info.is_dist_package(): + # This module isn't in site/dist packages, + # add PYTHONPATH env variable + pythonpath = f"\nPYTHONPATH=\"{src_path}\"" tmp_svc.write_text( SYSTEMD_UNIT - % (SERVICE_VERSION, user, src_path, env_file, sys.executable) + % (SERVICE_VERSION, user, env_file, exec_path) ) try: # write new environment - env_file.write_text(ENVIRONMENT % (src_path, cmd_args)) + env_file.write_text(ENVIRONMENT % (src_arg, cmd_args, pythonpath)) await machine.exec_sudo_command( f"cp -f {tmp_svc} {svc_dest}", tries=5, timeout=60.) await machine.exec_sudo_command(