Last active
January 27, 2020 07:04
-
-
Save mehmetg/336b4bb55566933d708da3f25c803452 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import os | |
def get_user_shell(): | |
USR_BIN_BASH = "/usr/bin/bash" | |
BIN_BASH = "/bin/bash" | |
default_shell = USR_BIN_BASH if os.path.exists(USR_BIN_BASH) else BIN_BASH if os.path.exists(BIN_BASH) else None | |
shell = os.environ.get("DEFAULT_SHELL", os.environ.get("SHELL", default_shell)) | |
if shell is None: | |
raise RuntimeError("Cannot find suitable shell") | |
return shell | |
def shell_with_env(shell_env): | |
env = os.environ.copy() | |
env.update(shell_env) | |
return subprocess.Popen( | |
args=[get_user_shell()], | |
shell=True, | |
env=env, | |
start_new_session=True | |
).wait() | |
def main(): | |
new_env = { | |
"hello": "world" | |
} | |
result = shell_with_env(new_env) | |
print("Exited with: %s" % result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment