Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for ignoring inherited environment #631

Open
jesteria opened this issue Jan 17, 2023 · 0 comments
Open

support for ignoring inherited environment #631

jesteria opened this issue Jan 17, 2023 · 0 comments

Comments

@jesteria
Copy link
Contributor

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:

  • It is non-obvious that clear() must be called here – (perhaps it's worth documenting)

  • It might instead make sense, (though it also might not), that when local.env() is called without arguments that local.env.clear() is called on the user's behalf – (otherwise, why are they calling env() at all?) I.e.:

    if not args and not kwargs:
        self.clear()
  • (As a side note: the local.env() context manager constructor accepts *args, **kwargs, and documents both; yet, args are ignored.)


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

  • Because the LocalEnv of local.env is a class-level attribute of LocalMachine, a purpose-dedicated machine may be constructed only from a subclass.
  • A LocalCommand retrieved from such a subclass of LocalMachine still executes in the environment of plumbum.local – because LocalCommand.machine simply returns plumbum.local (rather than storing the machine from which its was retrieved).
    • And, the LocalMachine construction of LocalCommand (its choice of constructor) is not trivially configurable/changeable.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant