triunenature
triunenature

Reputation: 671

Debug python that wont respect a catch statement

I am trying to run avg as part of a program. The program normally is executed automatically, so I cant see the standard output from python.

When I run the program by calling it directly, it works perfectly, however when I run it via automation, it fails.

It will say in the syslog -> "Starting scan of: xxx", but it never says "Unexpected error" OR "Scan Results". Which means, its failing, but not using the catch statement, or reporting the error in the "out" variable.

The offending function:

# Scan File for viruses
# fpath -> fullpath, tname -> filename, tpath -> path to file
def scan(fpath, tname, tpath):
    syslog("Starting scan of: " + tname)
    command = ["avgscan",
          "--report=" + tpath + "scan_result-" + tname +".txt",
          fpath]
    try:
        out = subprocess.call(command)
        syslog("Scan Results: " + str(out))
    except:
        syslog("Unexpected error: " + sys.exc_info()[0])
    finally:
        syslog("Finished scan()")

Both idea's so far are around the debugging code itself, prior to this, the scan was just a simple subprocess.call(command) with a simple syslog output. The with statement, the try catch was added to help the debugging.

Upvotes: 0

Views: 61

Answers (2)

triunenature
triunenature

Reputation: 671

So I solved it. Solved it in so much as I am no longer using AVG Scan, and using libclamscan.

By using a scanner that works directly with python, the results are faster, and errors are all gone. In case someone comes across this via a search, here is the code I am now using:

import os.path
import pyclamav.scanfile

def r_scan(fpath):
    viruslist = []
    if os.path.isfile(fpath):
        viruslist = f_scan(fpath, viruslist)
    for root, subFolders, files in os.walk(fpath):
        for filename in files:
            viruslist = f_scan(
                             os.path.join(root, filename), viruslist)
    writeReport(fpath, viruslist)

def f_scan(filename, viruslist):
    result = pyclamav.scanfile(filename)
    if result[0] > 0:
        viruslist.append([result[1], filename])
    return viruslist

def writeReport(fpath, viruslist):
    header = "Scan Results: \n"
    body = ""
    for virusname, filename in viruslist:
        body = body + "\nVirus Found: " + virusname + " : " + filename

    with open(fpath + "-SCAN_RESULTS.txt", 'w') as f:
        f.write(header+body)

Upvotes: 0

Brian
Brian

Reputation: 7654

I am suspecting the error is actually from the opening of the debug file; with statements do not prevent exceptions from being raised. In fact, they usually raise exceptions of their own.

Note the change of the scope of the try/except block.

# Scan File for viruses
# fpath -> fullpath, tname -> filename, tpath -> path to file
def scan(fpath, tname, tpath):
    syslog("Starting scan of: " + tname)
    command = ["avgscan",
        "--report=" + tpath + "scan_result-" + tname +".txt",
        fpath]
    try:
        with open(tpath + tname + "-DEBUG.txt", "w") as output:
            out = subprocess.call(command, stdout = output, stderr = output)
            syslog("Scan Results: " + str(out))
    except:
        syslog("Unexpected error: " + sys.exc_info()[0])
    finally:
        syslog("Finished scan()")

Upvotes: 1

Related Questions