mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 17:23:36 +02:00
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:
parent
e104fa2e46
commit
2d1beeebb0
1 changed files with 13 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue