Using the Python Definition Type
Python is a widely used, multi-platform, scripting language.
note
You must assign at least one process server to run PYTHON processes in order to use the definition type.
A Python interpreter needs to be installed on the system that the platform agent runs on. Supported versions are Python 2.7, and 3.x. On UNIX systems it should be installed as /usr/bin/python
. On Microsoft Windows systems the Python interpreter will be found via the `%PATH%' environment variable available to the job-processor, and otherwise, it will try to find it via the Windows Registry path HKEY_LOCAL_MACHINE\Software\Python\PythonCore or via Cygwin's /usr/bin/python. Please see the Interpreter section below which describes how to override this behavior on a process server-level, using a parameter.
Interpreter
By default, the interpreter for PYTHON scripts defaults to /usr/bin/python
on most platforms. You can override this by specifying an interpreter on process server-level with the LocalInterpreter_PYTHON
process server parameter, like /usr/local/bin/python
, for example. You can also specify a list of allowed interpreters on process server-level using the InterpreterWhitelist_PYTHON
process server parameter and override the default interpreter on process definition-level with the parameter JCS_INTERPRETER_PYTHON
. Simply create the parameter and point it to your preferred interpreter, it must exist and be white-listed using the InterpreterWhitelist_PYTHON
process server parameter.
The InterpreterWhitelist_PYTHON
process server parameter takes a regular expression that must match the value of the JCS_INTERPRETER_PYTHON
parameter.
Variables and Parameters
- Parameters to the script are manipulated in the Python source simply as if they were variables, using the standard
parameter
syntax. - Out parameters are supported. Set the variable using any Python method. For example a simple assignment in Python is
Parameter = "This is a string!"
Returning an Error
If your script code exits with a non-zero exit code this is correctly reflected in the server. Note that the maximum value that can be returned is operating system dependent; we recommend not relying on more than one byte (0..255).
Python Examples
The following shows how to use an environment variable as well as parameter:
import os
print("Hello, " + repr(os.environ.get("USER"))) # environment variable
print("Your message was: " + message) # parameter
Match one variable to a pattern, the following process definition needs two string parameters defined: str and pattern.
str="true"
pattern="t"
if str.find(pattern) > -1:
print("Match " + pattern + " found!")
else:
print(str + "does not match pattern " + pattern + "\n")
The following example shows you how to return a non-zero exit code resulting in the process going into Error:
import sys
N = 1
print("Exiting with exitcode %d \n" % N)
sys.exit(N)