-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell.py
57 lines (48 loc) · 1.73 KB
/
shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys, os, subprocess, signal
from subprocess import Popen
from killableprocess import Popen as KillablePopen
remove_vars = (
"PYTHONHOME", "PYTHONPATH", "VERSIONER_PYTHON_PREFER_32_BIT",
"EXECUTABLEPATH", "RESOURCEPATH", "ARGVZERO",
)
def make_environment(env=None, cwd=None):
if env is None:
env = os.environ
env = env.copy()
for var in remove_vars:
if var in env:
del env[var]
env["PYTHONUNBUFFERED"] = "1"
env["PYTHONIOENCODING"] = "UTF-8"
if cwd is None:
env["PWD"] = os.getcwd()
else:
try:
env["PWD"] = os.path.realpath(cwd).encode(sys.getfilesystemencoding())
except (LookupError, UnicodeEncodeError):
env["PWD"] = os.path.realpath(cwd).encode("utf-8")
return env
shell_prelude = {
"darwin": """\
source ~/.bash_profile\n' \
export PATH="${PATH}:/usr/local/bin:/usr/X11/bin"
%s
""",
}
def run_shell_command(cmdline, pipe_output=True, combine_stderr=True, env=None, cwd=None, killable=True, **kwargs):
if sys.platform == "win32":
args = " && ".join(command.strip() for command in cmdline.split("\n") if command.strip())
else:
cmdline = shell_prelude.get(sys.platform, "%s") % cmdline
args = [os.environ.get("SHELL", "/bin/sh"), "-c", cmdline]
return (KillablePopen if killable else Popen)(
args = args,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE if pipe_output else None,
stderr = (subprocess.STDOUT if combine_stderr else subprocess.PIPE) if pipe_output else None,
bufsize = 1,
close_fds = (sys.platform != "win32"),
shell = (sys.platform == "win32"),
cwd = cwd,
env = make_environment(env, cwd),
**kwargs)