jw.pkg.lib.util.get_profile_env(): Add function

Add a function get_profile_env(), a function returning environment
variables from /etc/profile. Pass add=True to add its contents to the
existing environment dictionary, overwriting old entries, or pass
False to get the pristine content.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-02-18 14:18:26 +01:00
commit 2d1beeebb0

View file

@ -298,3 +298,16 @@ async def get_password(args: Namespace|None=None, url: str|None=None, askpass_en
if parsed.password is not None:
return parsed.password
return await run_askpass(askpass_env, AskpassKey.Password)
async def get_profile_env(throw: bool=True, add=False) -> dict[str, str]: # export
env: dict[str,str]|None = None if add else {}
# Run bash, source /etc/profile, then print environment as NUL-separated key=value pairs
cmd = ['/bin/bash', '-lc', 'unset PROFILEREAD; source /etc/profile >/dev/null 2>&1; env -0']
stdout, stderr = await run_cmd(*cmd, throw=throw, output_encoding="bytes", verbose=True, env=env)
ret: dict[str, str] = {}
for entry in stdout.split(b"\0"):
if not entry:
continue
key, val = entry.split(b"=", 1)
ret[key.decode()] = val.decode()
return ret