kconfig_env.py¶
clew/kconfig_env.py
The environment one kconfiglib parse runs inside, and the functions it may call.
parse_environment
Iterator[list[str]] clew.kconfig_env.parse_environment(Path root)
Run a kconfiglib parse in a minimal environment, collecting its refusals.
Source · 42 lines
def parse_environment(root: Path) -> Iterator[list[str]]:
"""REPLACES the environment for the duration of the parse and restores it in a `finally`,
because this runs inside a longer pipeline and the MCP server is a long-lived process that
builds several targets: a leaked `srctree` would silently redirect a later parse.
The whole mapping is saved and restored rather than the three keys we set. Restoring keys
is correct only if the set of keys we touch never grows, and the point of this function is
that it touches ALL of them.
THREAD SAFETY, STATED PLAINLY: `os.environ` is process-wide, so two concurrent parses on
two threads would see each other's environment. kconfiglib offers no other route, and the
build lock the MCP server holds is per-target, so this is a real (if narrow) constraint
rather than a solved problem — recorded here because a reader will otherwise assume the
`threading.local` above covers it. It does not; it covers only the refusal record.
@brief Install the minimal parse environment, yielding the refusal record.
@return The list of refused shell commands.
@version 1
"""
saved = dict(os.environ)
refusals: list[str] = []
_state.refusals = refusals
minimal = {name: saved[name] for name in _INHERITED if name in saved}
minimal["srctree"] = str(root)
minimal["KCONFIG_FUNCTIONS"] = _FUNCTIONS_MODULE
try:
os.environ.clear()
os.environ.update(minimal)
yield refusals
finally:
os.environ.clear()
os.environ.update(saved)
_state.refusals = None
if refusals:
logger.warning(
"kconfig: refused %d `$(shell,…)` expansion(s) while parsing under %s — a "
"Kconfig may not run commands during indexing, so each expanded to the empty "
"string. Refused: %s",
len(refusals),
root,
"; ".join(refusals),
)1 more lines, not shown
Activity · 10 steps
Requirements
refuse_shell
str clew.kconfig_env.refuse_shell(object _kconf, str _name, str command)
kconfiglib's $(shell,…) , refusing instead of running.
Source · 14 lines
def refuse_shell(_kconf: object, _name: str, command: str) -> str:
"""RETURNS "" RATHER THAN RAISING. A raise here would fail the whole Kconfig parse, and
`load_kconfig` already degrades a parse failure to a recorded error with no symbols — so
refusing by exception would cost every symbol in a tree that uses `$(shell,…)` once. An
empty expansion costs that one value, and the refusal record says which.
@brief Refuse a shell expansion, recording it.
@return "".
@version 1
"""
_record(command)
return ""