sarugaku / shellingham

Tool to Detect Surrounding Shell
ISC License
222 stars 33 forks source link

Doesn't detect powershell on win32 #65

Closed dopry closed 1 year ago

dopry commented 1 year ago
PS C:\Users\dopry> python
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import shellingham
>>> shellingham.detect_shell()
('cmd', 'C:\\Windows\\System32\\cmd.exe')
>>>
dopry commented 1 year ago

I've dug into this a bit more... I'm using pyenv which has a shim that wraps the call to python in a .bat script which uses cmd.exe.

For nt shells it may be better to not stop at the first shell you see, but to use the last shell in the stack, at least if you're looking for the interactive shell, maybe there should be a detect_interactive_shell(), the uses cases I'm looking at are poetry env shell and pipenv shell...

>>> import psutil
>>> import os
>>> proc = psutil.Process()
>>> parent_1 = psutil.Process(proc.ppid())
>>> parent_1
psutil.Process(pid=25652, name='cmd.exe', status='running', started='08:43:04')
>>> parent_2 = psutil.Process(parent_1.ppid())
>>> parent_2
psutil.Process(pid=3680, name='powershell.exe', status='running', started='07:37:02')
>>> parent_3 = psutil.Process(parent_2.ppid())
>>> parent_3
psutil.Process(pid=3664, name='WindowsTerminal.exe', status='running', started='07:36:59')
>>> parent_4 =  psutil.Process(parent_3.ppid())
>>> parent_4
psutil.Process(pid=23448, name='explorer.exe', status='running', started='2022-11-11 08:00:07')
uranusjr commented 1 year ago

I would categorise this as a bug in pyenv-win. They should not use Batch files for shims. Shims should be exe.

dopry commented 1 year ago

@uranusjr while I agree and find pyenv-win's shims and rehash are annoying, I'm not sure that it is possible with an exe. Do you know of an exe shim example you can point me to, maybe I can contribute some time to working on that.

uranusjr commented 1 year ago

From pyenv-win’s issue tracker https://github.com/pyenv-win/pyenv-win/issues/352

Scoop also has a shim implementation that I personally like a lot, although it being in C# is probably a hurdle for a project adding a compilation step (I don’t think pyenv-win currently has any such dependencies).

dopry commented 1 year ago

@uranusjr thanks!!