diff options
Diffstat (limited to 'build')
| -rwxr-xr-x | build | 32 |
1 files changed, 26 insertions, 6 deletions
@@ -13,6 +13,7 @@ dist/udt/ untouched. Never touches /boot: root copies dist/udt/ there. """ import os import re +import shlex import shutil import struct import subprocess @@ -132,15 +133,27 @@ def font(fname, pattern, size, name): return cached -def os_name(): - for line in Path("/etc/os-release").read_text().splitlines(): - if line.startswith("NAME="): - return line[5:].strip('"') - return "GNU/Linux" +def shell_var(path, key): + """KEY's value from a KEY="value" file (machine-info, os-release), or None.""" + try: + lines = Path(path).read_text().splitlines() + except FileNotFoundError: + return None + for line in lines: + if line.startswith(f"{key}="): + return " ".join(shlex.split(line[len(key) + 1:])) or None + return None + + +def title(): + """The machine's pretty name if root has set one, else the OS name.""" + name = (shell_var("/etc/machine-info", "PRETTY_HOSTNAME") + or shell_var("/etc/os-release", "NAME") or "GNU/Linux") + return name.replace('"', "'") # it lands inside a quoted theme.txt string def assemble(stage, text, c, wallpaper, fonts): - (stage / "theme.txt").write_text(text.replace("%TITLE%", os_name())) + (stage / "theme.txt").write_text(text.replace("%TITLE%", title())) slices = { "panel.png": (8, 8, (*c["bg_alt"], PANEL_ALPHA)), # The selected item: a 4px accent bar on the left, surface behind. @@ -183,6 +196,13 @@ def selftest(): assert set(re.findall(r"@[A-Z_]+@", tpl)) <= PLACEHOLDERS, "template token udt does not fill" for _, _, name in FONTS.values(): assert f'"{name}"' in tpl, f"template does not use font {name}" + with tempfile.NamedTemporaryFile("w") as f: + f.write('PRETTY_HOSTNAME="A B\'s C"\nEMPTY=\n') + f.flush() + assert shell_var(f.name, "PRETTY_HOSTNAME") == "A B's C" + assert shell_var(f.name, "EMPTY") is None + assert shell_var(f.name, "MISSING") is None + assert shell_var("/nonexistent", "X") is None print("selftest ok") |
