Plain is headed towards 1.0! Subscribe for development updates →

 1import os
 2import tomllib
 3from pathlib import Path
 4
 5from plain.runtime import APP_PATH, PLAIN_TEMP_PATH
 6
 7from .process import ProcessManager
 8from .utils import has_pyproject_toml
 9
10
11class ServicesProcess(ProcessManager):
12    pidfile = PLAIN_TEMP_PATH / "dev" / "services.pid"
13    log_dir = PLAIN_TEMP_PATH / "dev" / "logs" / "services"
14
15    @staticmethod
16    def get_services(root):
17        if not has_pyproject_toml(root):
18            return {}
19
20        with open(Path(root, "pyproject.toml"), "rb") as f:
21            pyproject = tomllib.load(f)
22
23        return (
24            pyproject.get("tool", {})
25            .get("plain", {})
26            .get("dev", {})
27            .get("services", {})
28        )
29
30    def run(self):
31        self.write_pidfile()
32        self.prepare_log()
33        self.init_poncho(print)
34
35        try:
36            services = self.get_services(APP_PATH.parent)
37            for name, data in services.items():
38                env = {
39                    **os.environ,
40                    "PYTHONUNBUFFERED": "true",
41                    **data.get("env", {}),
42                }
43                self.poncho.add_process(name, data["cmd"], env=env)
44
45            self.poncho.loop()
46        finally:
47            self.rm_pidfile()
48            self.close()