E9.2 Run Python from Groovy

louisphicc

Member
Hello List!

So I was struggling to run some Python scripts from the Orchestrator...
I finally got it right and I thought I could share the general method with you.

I have (so far) 2 use cases for this :
  1. Format the csv output from a JDE Report into a *.xlsx file.
  2. Communicate with Sharepoint (download/upload and read files).
Prerequisites:
  1. Download and install Python 3.xx on your AIS server (https://www.python.org/downloads/). In my case, my AIS server is running under Windows Server 2016 and the installation had to be done for all users using the "custom install" feature. I also manually chose the install folder as C:\Programs\Python\. This is important for later, in the Groovy code below.
  2. Pip install any library you want to use in your scripts. To manipulate CSV into Excel files, I used openpyxl (https://openpyxl.readthedocs.io/en/stable/). To communicate with Sharepoint, I used Office365-REST-Python-Client.
  3. Make sure that all users have access to the Python installation folder (C:\Programs\Python\ in my case).
Ok, now onto the groovy script that will execute your Python scripts:
Java:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
 
    // 2 strings to get the command output and error message
    def cmd_out = new StringBuilder();
    def cmd_err = new StringBuilder();
 
    // The command to execute
    cmd = ['C:/Programs/Python/Python.exe', 'C:/MyScript.py', 'arg1', 'arg2'];
 
    // Execution
    def proc = cmd.execute();
    proc.consumeProcessOutput(cmd_out, cmd_err);
 
    // To wait until the command is done
    proc.waitFor();
 
 
    // Return the command outputs
    returnMap.put("command_output", cmd_out);
    returnMap.put("error_message", cmd_err);

    return returnMap;
}


That's all.
I hope you find it usefull and please share your use cases if any!
 
Top