cancel
Showing results for 
Search instead for 
Did you mean: 

How do I debug a python script in the script snap

wpenfold
New Contributor III

I am using a script snap to use SSH to call a procedure on another server. The pipeline completes without error, but it’s not calling the procedure–and it’s not giving me any error messages, so I don’t know what is failing. Is there a way to view the log that is produced and/or some other method to debug the script?

from com.snaplogic.scripting.language import ScriptHook
from com.snaplogic.scripting.language.ScriptHook import *
from subprocess import Popen, PIPE
import subprocess as subprocess

class TransformScript(ScriptHook):
def init(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log

def execute(self):
    self.log.info("Executing Transform script")
    
    while self.input.hasNext():
        data = self.input.next()
        cmdstr = " " + str(data['cmd']) + " "
        # args = [cmdstr]
        # proc = Popen(["ssh", "banjobs@server01.campus.ithaca.lan", " pwd "], shell=False, stdout=PIPE, stderr=PIPE)
        proc = Popen(["ssh", "banjobs@server01.campus.ithaca.lan", " touch /u01/appworx/wjptest.txt "], shell=False, stdout=PIPE, stderr=PIPE)
        # proc = Popen(["ssh", "banjobs@server01.campus.ithaca.lan", cmdstr ], shell=False, stdout=PIPE, stderr=PIPE)
        out, err = proc.communicate()
        exitcode = proc.returncode
    proc.kill()
    
    self.output.write(out)
    self.log.info("Finished executing the Transform script")

hook = TransformScript(input, output, error, log)

4 REPLIES 4

ptaylor
Employee
Employee

Try wrapping your code in a try/catch where the catch writes the error to the error view, like in the default template for a Python script.

wpenfold
New Contributor III

Ah…missed a few lines of code. Now I’m getting error info in the error view…helpful, thank you.

wpenfold
New Contributor III

I’m using the default template for the python script, but nothing is being written to the error view.

ForbinCSD
Contributor

Script snaps seem notoriously difficult to debug.
One technique that helps is very old-school… 1960’s old-school.
Try instrumenting your code.

In the SnapLogic environment, that could take a couple of forms. You could print a set of trace messages to a file (caveat: I have not tried this yet from a script snap). The other way is to create “debug” properties in your output docstream.

For example, you might create a global array named “Debug” or “Trace” in your script, to be added to the document you’re outputting, and any time you want to “print” a trace message with the line number you’re at and the values of some of your variables, you push a message string onto the end of the array.

I won’t claim this is not painful. If your script crashes without producing output, it is of no use. And interpreting your output consists of validating the pipeline and grabbing the JSON that comes out of the Script Snap, pasting it into a JSON formatter/viewer, and interpreting the results.

But at least it doesn’t leave you blindly scratching your head. 😕