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. ๐Ÿ˜•