Don't assume grandparent process is the shell

- Windows only (pew implementation bug)
- Traverse process tree until we find a known shell
  like cmd or powershell currently
This commit is contained in:
Dan Ryan
2018-03-08 13:40:08 -05:00
parent 4e8deda9cb
commit 46b6ab30e3
2 changed files with 22 additions and 3 deletions
+19
View File
@@ -105,3 +105,22 @@ def get_grandparent_process(pid=None):
parent = processes[ppid]
grandparent = processes[parent['parent_pid']]
return grandparent['executable']
def get_shell(pid=None):
"""Get the shell that the supplied pid or os.getpid() is running in.
"""
if not pid:
pid = os.getpid()
processes = get_all_processes()
def check_parent(pid, lvl=0):
ppid = processes[pid].get('parent_pid')
if ppid and processes[ppid]['executable'].lower().rsplit('.', 1)[0] in ['cmd', 'powershell']:
return processes[ppid]['executable']
if lvl >= 4:
return
return check_parent(ppid, lvl=lvl+1)
if processes[pid]['executable'].lower().rsplit('.', 1)[-1] in ['cmd', 'powershell']:
return processes[pid]['executable']
return check_parent(pid)
+3 -3
View File
@@ -36,7 +36,7 @@ else:
InstallCommand = ListPythons = LocatePython = UninstallCommand = \
lambda : sys.exit('Command not supported on this platform')
from ._win_utils import get_grandparent_process
from ._win_utils import get_shell
from pew._utils import (check_call, invoke, expandpath, own, env_bin_dir,
check_path, temp_environ, NamedTemporaryFile, to_unicode)
@@ -184,7 +184,7 @@ def _detect_shell():
if 'CMDER_ROOT' in os.environ:
shell = 'Cmder'
elif windows:
shell = get_grandparent_process(os.getpid())
shell = get_shell(os.getpid())
else:
shell = 'sh'
return shell
@@ -196,7 +196,7 @@ def shell(env, cwd=None):
if shell_name not in ('Cmder', 'bash', 'elvish', 'powershell', 'klingon', 'cmd'):
# On Windows the PATH is usually set with System Utility
# so we won't worry about trying to check mistakes there
shell_check = (sys.executable + ' -c "from pew.pew import '
shell_check = (sys.executable + ' -c "from pipenv.patched.pew import '
'prevent_path_errors; prevent_path_errors()"')
try:
inve(env, shell, '-c', shell_check)