1

I'm developing a command-line application that launches a sub-shell with a pre-generated environment.

subprocess.call(subshell, shell=True, env=custom_environment)

It launches subshell.bat or subshell.sh depending on which is the current os.

subshell = "subshell.bat" if os.name == "nt" else "subshell.sh"

But I'm looking to support Cygwin, Git Bash and others running on Windows, and they each need to be given subshell.sh instead, even though the OS is Windows.

I figured that if there was a way of determining the calling executable of a Python process, I could instead check against that.

if calling_executable in ("bash", "sh"):
  subshell = "subshell.sh"
elif calling_executable in ("cmd", "powershell"):
  subshell = "subshell.bat"
else:
  sys.exit()  # unsupported shell

But I'm not sure how to do that. Any ideas or suggestions for how to go about it differently?

Usage example

$ mytool --enter
Entering subshell for bash.exe

ps. this question is phrased exactly the same, but doesn't involve a similar problem, so it didn't feel right to add an unrelated problem to it.

2 Answers 2

2

You can use psutil to do that.

import psutil, os
Parent=psutil.Process(os.getpid()).parent()
ParentName=Parent.name() # e.g. bash, cmd.exe, etc

See that answer for another example. You can get the process' name with psutil.Process(os.getpid()).parent().name() as well.

Sign up to request clarification or add additional context in comments.

1 Comment

That is interesting. I'll take a closer look at this. Thanks.
1

I would suggest adding command line parameters to indicate the version if you cannot rely on just checking the operating system. It would probably make the program more "Pythonic" because 'explicit is better than implicit'.

Python Docs argparse

4 Comments

I like the sound of that, but I'm not quite seeing how that would look. It's important the the command-line interface remain minimal, as it is an user-facing tool and they aren't required to know what shell they use. Maybe you could show an interacting example? I added one of my own to the bottom of the question.
I had thought along the lines of the example you wrote above, but I thought the difference would be instead of checking the calling_executable you would check against a command line parameter. Something like: mytool --variant=cygwin and then have your if/else check. I definitely think using the psutil package looks like a good way to go.
Ah, ok. I like that approach, it's explicit as you say, but in this case not a good fit as I'd rather not force the user to specify their shell. For automated things, this seems better, and avoids a dependency! Thanks!
I'm finding that automating things is always a little tricky. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.