Python GUI for running Bash / ksh applications and shells?

I broke into the world of Python and GUI applications and made significant progress. However, I would like to advise how to proceed as follows:

  • I created a GUI application using python (2.6.6 - cannot update the system due to its obsolescence) and gtk, which displays several buttons, for example. app1, app2, app3
  • When I click the button, it launches the bash shell script. This script will set some required environment variables and then execute another external application (which uses these env variables)

Example:
  1) use clicks on the app1 button
  2) Then the GUI launches app1.shto configure the environment variables 3) Then the GUI launchesexternal_app1

# external_app1 is an example of an application
    # that requires some environment # variables to be set before starting


Example app1.shContent:

#/bin/bash

export DIR=/some/location/
export LICENSE=/some/license/
export SOMEVAR='some value'


NOTE . Due to the fact that the environment is configured, it first runs shell scripts to configure the environment, etc., and then launches external applications. Shell scripts will be blocked, so no one can edit them as soon as I tested them.


So I thought about how to execute the python GUI and so far, I am doing the following:

  • When the user clicks on application 1, check if app1.sh is executable / readable; if not, return an error
  • script, helper1.sh, app1.sh, external_app1, python helper1.sh script :

subprocess.Popen(helper1.sh, shell=True, stdout=out, stderr=subprocess.PIPE, close_fds=True)


helper1.sh :

#!/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


, script / (app2, app3 ..).

:

  • , , ? - ?
  • , stderr stdout (, helper1.sh) / GUI? ?


.

+4
1

, n, n . , GUI . , , - .

:

Python

env Popen():

env None, , ; , .

, app1.sh, app2.sh, app3.sh .. Python , Popen(), :

env_vars = {
  1: {
    'DIR': '/some/location/',
    'LICENSE': '/some/license/'
    'SOMEVAR': 'some value'
  },
  2: ...
}

...

environ_copy = dict(os.environ)
environ_copy.update(env_vars[n])
subprocess.Popen('external_application', shell=True, env=environ_copy, ...)

script

vars , , - - , .

:

#!/usr/bin/env bash

if source "$1"; then  # Set up env variables
    external_app      # Runs the actual application
else
    echo "Error executing $1" 2>/dev/stderr
    exit 1            # Return a non-zero exit status
fi

app1.sh script, - n. , & - Popen , Python. subprocess.PIPE Popen.communicate(), 'stdout stderr.

external_process (.. ), , , . :

subprocess.Popen('external_command', '/some/location/', '/some/license/', 'some value')

shell=True script . external_command , --flags (, --dir /some/location/) . ( ), ; Python argparse .

, external_process , . , .

0

All Articles