tomerfiliba / plumbum

Plumbum: Shell Combinators
https://plumbum.readthedocs.io
MIT License
2.81k stars 183 forks source link

support for ignoring inherited environment #631

Open jesteria opened 1 year ago

jesteria commented 1 year ago

The plumbum LocalMachine default of passing the parent process environment variables to Popen makes sense; however, it is either non-obvious or non-trivial to disable this behavior for command execution without the inherited environment – in a thread-safe manner in particular.

(I've written a good bit below; but, I'm curious if I'm missing anything or if there are any thoughts on how this might be approached in plumbum.)


Generally, it now appears that this might be done as follows:

with local.env():
    local.env.clear()

    # run commands
    local['env']()

However:


Matters appear worse for disabling the inherited environment in a thread-safe manner:

Certainly, worrying about the above might be avoided by enabling LocalMachine._popen to somehow ignore its own env (local.env); though, it is not obvious how to achieve this in the "nicest" way. (E.g.: Another keyword argument: _popen(…, isolate=True)? A very special env to pass like: _popen(…, env=IsolatedEnv(…))?)


As a final note, obviously, all of this can be avoided by nesting commands under /usr/bin/env; though, this defeats the purpose of environment management in plumbum, and it is a shame when subprocess.Popen itself supports a cleared environment straight-forwardly.