1 """Various script and configuration file templates."""
5 from string import Template
8 from .notification import notifications
11 _CMD_VARS = os.linesep.join([f"set {var}={val}" for var, val in path.env.items()])
12 _CMD_PY = "runpython" if "meson.exe" in path.PYTHON_PATH.lower() else ""
15 def _read_template(tpl_name: str, maps: T.Dict[str, T.Any]) -> str:
16 tpl_path = path.TESTLIB_ROOT.joinpath("template", tpl_name)
17 tpl = Template(tpl_path.read_text(encoding="utf-8"))
18 return tpl.substitute(maps)
21 def make_script(node: str, script: str, source: str) -> str:
22 """Create a tincd script."""
23 addr = notifications.address
24 if isinstance(addr, str):
25 addr = f'r"{addr}"' # 'r' is for Windows pipes: \\.\foo\bar
27 "AUTH_KEY": notifications.authkey,
30 "NOTIFICATIONS_ADDR": addr,
31 "PYTHON_PATH": path.PYTHON_PATH,
32 "SCRIPT_NAME": script,
33 "SCRIPT_SOURCE": source,
34 "SRC_ROOT": path.TEST_SRC_ROOT,
35 "TEST_NAME": path.TEST_NAME,
37 return _read_template("script.py.tpl", maps)
40 def make_cmd_wrap(script: str) -> str:
41 """Create a .cmd wrapper for tincd script. Only makes sense on Windows."""
43 "PYTHON_CMD": _CMD_PY,
44 "PYTHON_PATH": path.PYTHON_PATH,
45 "SCRIPT_PATH": script,
46 "VARIABLES": _CMD_VARS,
48 return _read_template("script.cmd.tpl", maps)
51 def make_netns_config(namespace: str, ip_addr: str, mask: int) -> str:
52 """Create a tincd script that does network namespace configuration."""
53 maps = {"NAMESPACE": namespace, "ADDRESS": ip_addr, "MASK": mask}
54 return _read_template("netns.py.tpl", maps)