All Articles

How to Execute a Python Script within Boomi

While Boomi’s runtime is Java-based, it can execute commands, and therefore, can execute any Python script. The examples below show two methods to execute a Python script without the need to install additional Java libraries (e.g. Jython). Python should be installed on the server that is hosting the Boomi runtime (atom, molecule, or cloud) and the file must have permissions set so that the service account user running the Boomi runtime can execute it.

Method 1 - Program Command and Disk v2

The first method uses a Program Command shape to execute the script and write a file to disk. Then a Disk v2 shape to retrieve the file. If no data needs to be read from the execute of the Python script, then the Disk v2 is not needed.

Program Command and Disk Overview Figure 1. Program Command and Disk v2 Process Overview

In branch one, a Program Command shape is added and the script added. Any parameters that are required can be added.

The script being execute within this first example is below. It has 2 parameters. The first parameter has a person’s name and the second is the location of a file to be written.

# Python
# hello.py

import sys
f = open(sys.argv[2], "w")
f.write("hello, " + sys.argv[1])
f.close()

Program Command Shape with Python Script to Execute Figure 2. Program Command Shape with Python Script to Execute

Below is the full command that is within the Program Command shape for Windows. The backslashes do not need to be escaped within this method.

C:\Users\AdamBedenbaugh\AppData\Local\Programs\Python\Python310\python.exe C:\Boomi\hello.py {1} {2}

The location of python.exe needs to have the full path. The location can be found be found by typing the following into PowerShell or cmd.

python3
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)

Below is the full command for Linux.

python3 /boomi/local/hello.py {1} {2}

Once the command is executed, the Disk v2 can perform a GET request to retrieve the file and process as needed.

Method 2 - Data Process Shape

The next method will execute a Python script via a Groovy script and return standard out back into the flow as a document.

Data Process Process Overview Figure 3. Data Process Process Overview

Below is the Python script that is being used in this example.

# Python
# hello.py

import sys
print("hello, " + sys.argv[1])

Data Process Shape Setup Figure 4. Data Process Shape Setup

Below example of a Groovy script that is used to execute a Python script with arguments within a Windows environment. A dynamic document property is set as an example. Notice that there is a space after the Python script name. The full path of python.exe needs to be defined. The backslashes need to be escaped within the Groovy script for the Windows directory path. The output will be written to the output document within Boomi.

// Groovy 2.4
import java.util.Properties
import java.io.InputStream

for (int i = 0; i < dataContext.getDataCount(); i++) {
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);
    
    String name = props.getProperty("document.dynamic.userdefined.DDP_NAME")
    
    Process p = Runtime.getRuntime().exec("C:\\Users\\AdamBedenbaugh\\AppData\\Local\\Programs\\Python\\Python310\\python.exe C:\\Boomi\\hello.py " + name);

    is = p.getInputStream()

    dataContext.storeStream(is, props);
    
}

Below is an example of a Groovy script that will execute a Python script with arguments within a Linux environment. It is also passing a single argument from a dynamic document property. The output will be written to the output document within Boomi.

// Groovy 2.4
import java.util.Properties
import java.io.InputStream

for (int i = 0; i < dataContext.getDataCount(); i++) {
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);
    
    String name = props.getProperty("document.dynamic.userdefined.DDP_NAME")
    
    Process p = Runtime.getRuntime().exec("python3 /boomi/local/hello.py " + name);

    is = p.getInputStream()

    dataContext.storeStream(is, props);
    
}

Article originally posted at Boomi Community.

Published Jun 4, 2022

Developing a better world.© All rights reserved.