Skip to content

The agent that grows with you

refactor(gateway): one unit-file Environment= reader for HERMES_HOME and LD_LIBRARY_PATH

kshitijk4poor1d7acabc138d
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index c60cc878dc3..2c0f1e072c9 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -1121,21 +1121,30 @@ def _systemctl_show(properties: tuple[str, ...], *, system: bool) -> dict[str, s
return _parse_kv_pairs(result.stdout.splitlines()) if result.returncode == 0 else {}
-def _hermes_home_pinned_by_unit(unit_path: Path) -> str | None:
- """``HERMES_HOME`` pinned by the unit file at *unit_path*, or None when absent/unreadable."""
+def _unit_environment_value(unit_path: Path, name: str) -> str | None:
+ """Value of one ``Environment="NAME=…"`` directive in the unit file at *unit_path*, with
+ systemd's ``\\"``/``\\\\``/``%%`` quoting undone; None when the file or the key is absent."""
try:
text = unit_path.read_text(encoding="utf-8")
except OSError:
return None
for line in text.splitlines():
body = line.strip()
- if body.startswith("Environment="):
- body = body[len("Environment=") :].strip().strip('"')
- if body.startswith("HERMES_HOME="):
- return body.split("=", 1)[1].strip().strip('"') or None
+ if not body.startswith("Environment="):
+ continue
+ body = body[len("Environment=") :].strip()
+ if body.startswith('"') and body.endswith('"'):
+ body = body[1:-1].replace('\\"', '"').replace("\\\\", "\\").replace("%%", "%")
+ if body.startswith(f"{name}="):
+ return body.split("=", 1)[1].strip() or None
return None
+def _hermes_home_pinned_by_unit(unit_path: Path) -> str | None:
+ """``HERMES_HOME`` pinned by the unit file at *unit_path*, or None when absent/unreadable."""
+ return _unit_environment_value(unit_path, "HERMES_HOME")
+
+
def _hermes_home_from_systemd_unit_file(system: bool = False) -> str | None:
"""``HERMES_HOME`` from the on-disk unit file - what refresh/compare already read, and reliable under ``sudo``."""
return _hermes_home_pinned_by_unit(get_systemd_unit_path(system=system))
@@ -2913,17 +2922,8 @@ def _systemd_env_line(name: str, value: str) -> str:
def _installed_unit_ld_library_path(system: bool) -> str:
- """``LD_LIBRARY_PATH`` baked into the installed unit, unescaped; ``""`` when absent."""
- import re
- unit_path = get_systemd_unit_path(system=system)
- try:
- text = unit_path.read_text(encoding="utf-8")
- except OSError:
- return ""
- match = re.search(r'^Environment="LD_LIBRARY_PATH=((?:[^"\\]|\\.)*)"$', text, flags=re.M)
- if not match:
- return ""
- return match.group(1).replace("%%", "%").replace('\\"', '"').replace("\\\\", "\\")
+ """``LD_LIBRARY_PATH`` baked into the installed unit; ``""`` when absent."""
+ return _unit_environment_value(get_systemd_unit_path(system=system), "LD_LIBRARY_PATH") or ""
def _ld_library_path_line(system: bool, target_home_dir: str | None = None) -> str:
@@ -3088,8 +3088,7 @@ def generate_systemd_unit(system: bool = False, run_as_user: str | None = None)
f'Environment="HOME={home_dir}"\n'
f'Environment="USER={username}"\n'
f'Environment="LOGNAME={username}"\n'
- f"{_ld_library_path_line(system=True, target_home_dir=home_dir)}"
- )
+ ) + _ld_library_path_line(system=True, target_home_dir=home_dir)
wanted_by = "multi-user.target"
else:
hermes_home = str(get_hermes_home().resolve())