浏览代码

fix slweb svc

Johan Le Baut 1 周之前
父节点
当前提交
8b2b79c48d
共有 2 个文件被更改,包括 24 次插入 和 2 次删除
  1. 18 1
      scripts/deploy.py
  2. 6 1
      tests/test_deploy.py

+ 18 - 1
scripts/deploy.py

@@ -947,6 +947,23 @@ def systemd_quote(value):
     return f'"{value}"'
 
 
+def systemd_path_value(value):
+    """Escape a filesystem path for an unquoted systemd path directive."""
+    escaped = []
+    for character in str(value):
+        if character == "%":
+            escaped.append("%%")
+        elif (
+            character.isspace()
+            or not character.isprintable()
+            or character in {"\\", '"', "'", "#"}
+        ):
+            escaped.extend(f"\\x{byte:02x}" for byte in character.encode("utf-8"))
+        else:
+            escaped.append(character)
+    return "".join(escaped)
+
+
 def render_mod_host_service(config):
     binary = ROOT / config["binary_source"]
     service_template = ROOT / config["service_source"]
@@ -1451,7 +1468,7 @@ def render_slweb_service(config, source_config, tools_digest):
     if not service_template.is_file():
         raise RuntimeError(f"SLWeb service template is missing: {service_template}")
     replacements = {
-        "@SLWEB_DIRECTORY@": systemd_quote(destination),
+        "@SLWEB_DIRECTORY@": systemd_path_value(destination),
         "@PYTHON@": systemd_quote(config["python_path"]),
         "@OSC_HOST@": source_config["osc"]["host"],
         "@OSC_PORT@": str(source_config["osc"]["port"]),

+ 6 - 1
tests/test_deploy.py

@@ -599,7 +599,8 @@ class DeploymentTests(unittest.TestCase):
         self.assertIn("Requires=rigforge-sooperlooper.service", unit)
         self.assertIn("After=rigforge-sooperlooper.service", unit)
         self.assertIn("PartOf=rigforge-sooperlooper.service", unit)
-        self.assertIn(f'WorkingDirectory="{installed_dir}"', unit)
+        self.assertIn(f"WorkingDirectory={installed_dir}", unit)
+        self.assertNotIn(f'WorkingDirectory="{installed_dir}"', unit)
         self.assertIn(
             f'ExecStart="/usr/bin/python3" tools/slweb.py --osc-host 127.0.0.1 '
             f'--osc-port {sooperlooper["osc"]["port"]} --http-host 127.0.0.1 --http-port 8080',
@@ -614,6 +615,10 @@ class DeploymentTests(unittest.TestCase):
                 ["systemctl", "--user", "is-active", "--quiet", "rigforge-slweb.service"],
             ],
         )
+        self.assertEqual(
+            deploy.systemd_path_value("/home/test user/rigforge%"),
+            "/home/test\\x20user/rigforge%%",
+        )
         self.assertIn("slweb", deploy.read_registry())
         self.assertIn("slweb_service", deploy.read_registry())