I am writing a wrapper python script which needs to source ./config
before running some other tools. Excluding the extra functionality that my script provides, it basically does this:
$ source ./config
$ ./a-tool.sh --args
$ ./another-tool.py --arg1 --arg2
Translating to python, I can do something along the lines of this to call the two scripts:
subprocess.call(['./a-tool.sh', '--args'])
subprocess.call(['./another-tool.py', '--arg1', '--arg2'])
But I can't figure out how to source
first, using the modified environment for the tools. I have read Emulating Bash 'source' in Python, but it suggests some hacks to modify the environment of running script which is not what I want.
EDIT:
To clarify, my script is currently a bash script which does a series of checks, parses some arguments, and depending on the outcome runs one or more tools as described above. The source
line is not always needed, and is not always the same. However, when it is used, all the following tools need to run in the same environment (i.e. bash process). In bash, this is easy. However, I want to rewrite the script in python because some of the other functionality is just easier for me to write and maintain in python. It is purely a language choice based on preference and has nothing to do with the actual functionality. Based on the answer by @abarnert, I guess I could do something like
subprocess.call(['source', '.config', ';', './a-tool.sh', ...])
That does not look easy to debug though, so I would prefer something like:
bash = subprocess.run('bash')
bash.execute('source ./config')
bash.execute('a-tool.sh --args')
Is there any way to do this in python?