Sikuli 1.1.3: Is it possible to trap syntax errors?

Asked by Ron Turrentine

Hello fellow Sikulians!

At first glance you may be laughing at this subject line and wondering what I've been smoking.
However I actually have a novel case that I could use some help with! Let me explain...

I have a Sikuli "Control Script" that launches each night on a VM via Windows Task Scheduler. The script grabs a list of eligible Sikuli scripts from a SQL database to process (there are presently 50 in total, and the number is growing). Each script is then executed individually by the Control Script, one-by-one, and their run results are recorded into our SQL database.

If a script being ran has an error, then that error is trapped and recorded into a log file, then the Control Script moves onto the next script. However, if a script has a *syntax* error, then it halts the Control Script's execution completely and no further scripts are processed.

If possible, I need to figure out a way for my Control Script to determine if a syntax error has occurred in a script. Then, if it has, the script can ignore it, record the results, and move onto the next script.

Any help/suggestions are encouraged & appreciated!

Thanks,

Ron

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

How do you run the scripts?

Please paste the relevant script code.

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#2

RaiMan,

Thanks for replying!

Here are the relevant lines from my script.
This is a FOR loop which loops through the scripts that are returned by my database query.
Each script is executed by the "execfile" line.
This is ran within a try-except block. If an exception is through, it logs the error and exits the script.
If you need to see the rest of the script, too, please just let me know!

-----------------------------------------------------------------------------------------------------------

    # iterate through scripts in array and run each one in succession
    for sn in range(len(scriptnames)):

        if os.path.exists(scriptnames[sn]):
            # Point to where the scripts images are stored
            setBundlePath(scriptnames[sn])

            # Grab base Sikuli filename
            basename = os.path.splitext(os.path.basename(scriptnames[sn]))[0]

            #Write line to log file
            line = str(datetime.datetime.now()) + ": Processing script = " + scriptnames[sn] + "\n"
            WriteToPDALog(pda_errorlog, line)

            # Get name of relative Python script in Sikuli folder
            pyname = basename + ".py"

            joinedname = os.path.join(scriptnames[sn], pyname)

            #Execute the current script:
            retcode = execfile(joinedname)

            #Write metrics & "completed" info to log file
            line = str(datetime.datetime.now()) + ": Captured " + str(myPDA.Screenshots) + " screenshots; recorded " + str(myPDA.Errors) + " errors; downloaded " + str(myPDA.DLFiles) + " files; split " + str(myPDA.WSSplit) + " worksheets.\n"
            line += str(datetime.datetime.now()) + ": Completed processing " + basename + "script.\n"
            line += str(datetime.datetime.now()) + ": **********************************************************************************\n"

            WriteToPDALog(pda_errorlog, line)

Revision history for this message
Manfred Hampl (m-hampl) said :
#3

"This is ran within a try-except block"
Where is the try-except block?

Have you tried something like

try:
    retcode = execfile(joinedname)
except Exception, err:
    print err

Revision history for this message
RaiMan (raimund-hocke) said :
#4

IMHO no need to use try...except.

retcode = execfile(joinedname)

in case of no error retcode will be None

In was of any error, retcode will contain something like that:

[error] script [ syntaxControl ] stopped with error in line 6
[error] ZeroDivisionError ( integer division or modulo by zero )
[error] --- Traceback --- error source first
line: module ( function ) statement
1: syntax ( <module> ) 1/0
[error] --- Traceback --- end --------------

in this case the joined name script simply contains
1/0

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#5

RaiMan, thanks I will do some testing of the return code on the executed script. That may just be the key to this whole thing. I'll let you know what I discover!

Best regards,

Ron

Can you help with this problem?

Provide an answer of your own, or ask Ron Turrentine for more information if necessary.

To post a message you must log in.