mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-10 04:36:40 +00:00
Replace 8 bash env modules with a single Python generator
(cli.meta.env) that materialises env/static.env + runtime context
into a gitignored .env. Each variable lives in its own handler
under utils/env/handlers/; defaults carry inline one-line `# ...`
comments.
Rename to the INFINITO_* schema:
- workflow inputs: APPS, SERVICES_DISABLED, CMD, DEBUG, WHITELIST,
BUNDLES, FULL_CYCLE, VARIANT, RUN_FLAGS, LIMIT_HOST, PARALLEL
- inventory: INVENTORY_DIR / INVENTORY_FILE / HOST_VARS_FILE,
PW_FILE -> INVENTORY_PASSWORD_FILE
- venv: VENV_BASE / VENV_FALLBACK / VENV_NAME
- new SPOT defaults: COMPILE_SILENCE, CONTAINER_NO_CA,
WAIT_HEALTH_TIMEOUT_S, PACKAGE_CACHE_FRONTEND_INIT_IMAGE,
promoted IMAGE_TAG from GHA-only to general passthrough
compose.yml hardening:
- inline \${VAR:-default} removed; strict \${VAR:?...} everywhere
- new integration lint blocks regressions
Tooling:
- scripts/install/all.sh owns the stamp-file install logic
(Makefile target delegates)
- scripts/meta/env/load.sh is the single shell entry point;
8 bash modules deleted
- new shell lint: no INFINITO_* defaults outside env/static.env
Tests:
- 93 new unit tests for utils/env/{builder,parser,runtime,writer}
- restructure tests/lint into repository/ (file-content rules) vs
filesystem/{shell,python,env}/ (filesystem-shape rules)
- drift + smoke integration tests against the generator
Docs:
- variables.md is now a pointer; per-variable docs live next to
the key in env/static.env / env/ci.env
- compose.yml.md drops the Default column
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
"""Parse the project's `env/static.env`.
|
|
|
|
The format is the docker-compose `.env`-subset: flat `KEY=value` lines,
|
|
`#` for comments, optional surrounding double/single quotes for values
|
|
that contain whitespace or special characters. Anything more exotic
|
|
(nested mappings, multiline values, variable expansion) is rejected so
|
|
drift between the file and its consumers stays loud.
|
|
|
|
Comments are first-class: every `# ...` line directly above a key is
|
|
captured and surfaced via :func:`parse_static_env_with_comments`, so
|
|
the generator can preserve per-key documentation in the produced
|
|
``.env``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import TYPE_CHECKING
|
|
|
|
from utils.cache.files import read_text
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
_LINE_RE = re.compile(r"^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$")
|
|
|
|
|
|
def _parse_value(raw_value: str) -> str:
|
|
value = raw_value
|
|
# Strip trailing inline comment when value is not quoted.
|
|
if value and value[0] not in ('"', "'"):
|
|
value = value.split("#", 1)[0].rstrip()
|
|
# Strip a matched pair of outer quotes.
|
|
if len(value) >= 2 and value[0] == value[-1] and value[0] in ('"', "'"):
|
|
value = value[1:-1]
|
|
return value
|
|
|
|
|
|
def parse_static_env(path: Path) -> dict[str, str]:
|
|
"""Return the `KEY: value` map declared in `path`."""
|
|
values, _ = parse_static_env_with_comments(path)
|
|
return values
|
|
|
|
|
|
def parse_static_env_with_comments(
|
|
path: Path,
|
|
) -> tuple[dict[str, str], dict[str, str]]:
|
|
"""Return (`values`, `comments`) for the env-file at `path`.
|
|
|
|
A key's comment is the most recent contiguous block of `# ...`
|
|
lines directly above the `KEY=...` line, joined with single spaces.
|
|
Section headers (`# --- ... ---`) are treated as separators: they
|
|
reset the pending comment so the next key starts fresh, instead
|
|
of inheriting the heading line as its own documentation.
|
|
"""
|
|
values: dict[str, str] = {}
|
|
comments: dict[str, str] = {}
|
|
pending: list[str] = []
|
|
for lineno, raw in enumerate(read_text(str(path)).splitlines(), 1):
|
|
stripped = raw.strip()
|
|
if not stripped:
|
|
pending = []
|
|
continue
|
|
if stripped.startswith("#"):
|
|
body = stripped.lstrip("#").strip()
|
|
# Section dividers / banner blocks (`# --- ... ---`,
|
|
# multi-paragraph file header) do not become per-key docs.
|
|
if body.startswith("---") and body.endswith("---"):
|
|
pending = []
|
|
continue
|
|
pending.append(body)
|
|
continue
|
|
match = _LINE_RE.match(stripped)
|
|
if not match:
|
|
raise ValueError(f"{path}:{lineno}: cannot parse line: {raw!r}")
|
|
key, raw_value = match.group(1), match.group(2)
|
|
values[key] = _parse_value(raw_value)
|
|
if pending:
|
|
comments[key] = " ".join(pending)
|
|
pending = []
|
|
return values, comments
|