I've been digging into the world of Python and GUI applications and have made some considerable progress. However, I'd like some advice on how to proceed with the following:
- I've created a GUI application using python (2.6.6 - cannot upgrade system due to it being legacy) and gtk that displays several buttons e.g. app1, app2, app3
- When I click on a button, it then runs a bash shell script. This script will set up some required environment variables and then execute another external application (that uses these env variables)
Example:
1) use clicks on button app1
2) GUI then launchesapp1.sh
to set up environment variables
3) GUI then runsexternal_app1
# external_app1 is an example application
# that requires that some environment
# variables to be set before it can launch
Example app1.sh
contents:
#/bin/bash
export DIR=/some/location/
export LICENSE=/some/license/
export SOMEVAR='some value'
NOTE: Due to the way the environment is configured, it has to launch shell scripts first to set up the environment etc, and then launch the external applications. The shell scripts will be locked down so it cannot be edited by anyone once I've tested them.
So I've thought about how to have the python GUI execute this and so far, I am doing the following:
- When user clicks on app1, check if app1.sh is executable/readable, if not, return error
- Create another helper script, let's say
helper1.sh
that will contain theapp1.sh
followed by theexternal_app1
command and then have python execute thathelper1.sh
script via the below:
subprocess.Popen(helper1.sh, shell=True, stdout=out, stderr=subprocess.PIPE, close_fds=True)
Example helper1.sh
contents:
#!/usr/bin/env bash
source app1.sh # sets up env variables
if [ $? = 0 ]; then
external_app & # Runs the actual application in background
else
echo "Error executing app1.sh" 2>/dev/stderr
fi
This is done so that the helper script executes in its own subshell and so that I can run multiple environment setup / external applications (app2, app3 etc).
So I ask:
- Is there a better perhaps more pythonic way of doing this? Can someone point me in the right direction?
- And when it comes to logging and error handling, how to effectively capture stderr or stdout from the helper scripts (e.g.
helper1.sh
) without blocking/freezing the GUI? Using threads or queues?
Thank you.