Blame regressions.py

Packit 423ecb
#!/usr/bin/python -u
Packit 423ecb
import glob, os, string, sys, thread, time
Packit 423ecb
# import difflib
Packit 423ecb
import libxml2
Packit 423ecb
Packit 423ecb
###
Packit 423ecb
#
Packit 423ecb
# This is a "Work in Progress" attempt at a python script to run the
Packit 423ecb
# various regression tests.  The rationale for this is that it should be
Packit 423ecb
# possible to run this on most major platforms, including those (such as
Packit 423ecb
# Windows) which don't support gnu Make.
Packit 423ecb
#
Packit 423ecb
# The script is driven by a parameter file which defines the various tests
Packit 423ecb
# to be run, together with the unique settings for each of these tests.  A
Packit 423ecb
# script for Linux is included (regressions.xml), with comments indicating
Packit 423ecb
# the significance of the various parameters.  To run the tests under Windows,
Packit 423ecb
# edit regressions.xml and remove the comment around the default parameter
Packit 423ecb
# "<execpath>" (i.e. make it point to the location of the binary executables).
Packit 423ecb
#
Packit 423ecb
# Note that this current version requires the Python bindings for libxml2 to
Packit 423ecb
# have been previously installed and accessible
Packit 423ecb
#
Packit 423ecb
# See Copyright for the status of this software.
Packit 423ecb
# William Brack (wbrack@mmm.com.hk)
Packit 423ecb
#
Packit 423ecb
###
Packit 423ecb
defaultParams = {}	# will be used as a dictionary to hold the parsed params
Packit 423ecb
Packit 423ecb
# This routine is used for comparing the expected stdout / stdin with the results.
Packit 423ecb
# The expected data has already been read in; the result is a file descriptor.
Packit 423ecb
# Within the two sets of data, lines may begin with a path string.  If so, the
Packit 423ecb
# code "relativises" it by removing the path component.  The first argument is a
Packit 423ecb
# list already read in by a separate thread; the second is a file descriptor.
Packit 423ecb
# The two 'base' arguments are to let me "relativise" the results files, allowing
Packit 423ecb
# the script to be run from any directory.
Packit 423ecb
def compFiles(res, expected, base1, base2):
Packit 423ecb
    l1 = len(base1)
Packit 423ecb
    exp = expected.readlines()
Packit 423ecb
    expected.close()
Packit 423ecb
    # the "relativisation" is done here
Packit 423ecb
    for i in range(len(res)):
Packit 423ecb
        j = string.find(res[i],base1)
Packit 423ecb
        if (j == 0) or ((j == 2) and (res[i][0:2] == './')):
Packit 423ecb
            col = string.find(res[i],':')
Packit 423ecb
            if col > 0:
Packit 423ecb
                start = string.rfind(res[i][:col], '/')
Packit 423ecb
                if start > 0:
Packit 423ecb
                    res[i] = res[i][start+1:]
Packit 423ecb
Packit 423ecb
    for i in range(len(exp)):
Packit 423ecb
        j = string.find(exp[i],base2)
Packit 423ecb
        if (j == 0) or ((j == 2) and (exp[i][0:2] == './')):
Packit 423ecb
            col = string.find(exp[i],':')
Packit 423ecb
            if col > 0:
Packit 423ecb
                start = string.rfind(exp[i][:col], '/')
Packit 423ecb
                if start > 0:
Packit 423ecb
                    exp[i] = exp[i][start+1:]
Packit 423ecb
Packit 423ecb
    ret = 0
Packit 423ecb
    # ideally we would like to use difflib functions here to do a
Packit 423ecb
    # nice comparison of the two sets.  Unfortunately, during testing
Packit 423ecb
    # (using python 2.3.3 and 2.3.4) the following code went into
Packit 423ecb
    # a dead loop under windows.  I'll pursue this later.
Packit 423ecb
#    diff = difflib.ndiff(res, exp)
Packit 423ecb
#    diff = list(diff)
Packit 423ecb
#    for line in diff:
Packit 423ecb
#        if line[:2] != '  ':
Packit 423ecb
#            print string.strip(line)
Packit 423ecb
#            ret = -1
Packit 423ecb
Packit 423ecb
    # the following simple compare is fine for when the two data sets
Packit 423ecb
    # (actual result vs. expected result) are equal, which should be true for
Packit 423ecb
    # us.  Unfortunately, if the test fails it's not nice at all.
Packit 423ecb
    rl = len(res)
Packit 423ecb
    el = len(exp)
Packit 423ecb
    if el != rl:
Packit 423ecb
        print 'Length of expected is %d, result is %d' % (el, rl)
Packit 423ecb
	ret = -1
Packit 423ecb
    for i in range(min(el, rl)):
Packit 423ecb
        if string.strip(res[i]) != string.strip(exp[i]):
Packit 423ecb
            print '+:%s-:%s' % (res[i], exp[i])
Packit 423ecb
            ret = -1
Packit 423ecb
    if el > rl:
Packit 423ecb
        for i in range(rl, el):
Packit 423ecb
            print '-:%s' % exp[i]
Packit 423ecb
            ret = -1
Packit 423ecb
    elif rl > el:
Packit 423ecb
        for i in range (el, rl):
Packit 423ecb
            print '+:%s' % res[i]
Packit 423ecb
            ret = -1
Packit 423ecb
    return ret
Packit 423ecb
Packit 423ecb
# Separate threads to handle stdout and stderr are created to run this function
Packit 423ecb
def readPfile(file, list, flag):
Packit 423ecb
    data = file.readlines()	# no call by reference, so I cheat
Packit 423ecb
    for l in data:
Packit 423ecb
        list.append(l)
Packit 423ecb
    file.close()
Packit 423ecb
    flag.append('ok')
Packit 423ecb
Packit 423ecb
# This routine runs the test program (e.g. xmllint)
Packit 423ecb
def runOneTest(testDescription, filename, inbase, errbase):
Packit 423ecb
    if 'execpath' in testDescription:
Packit 423ecb
        dir = testDescription['execpath'] + '/'
Packit 423ecb
    else:
Packit 423ecb
        dir = ''
Packit 423ecb
    cmd = os.path.abspath(dir + testDescription['testprog'])
Packit 423ecb
    if 'flag' in testDescription:
Packit 423ecb
        for f in string.split(testDescription['flag']):
Packit 423ecb
            cmd += ' ' + f
Packit 423ecb
    if 'stdin' not in testDescription:
Packit 423ecb
        cmd += ' ' + inbase + filename
Packit 423ecb
    if 'extarg' in testDescription:
Packit 423ecb
        cmd += ' ' + testDescription['extarg']
Packit 423ecb
Packit 423ecb
    noResult = 0
Packit 423ecb
    expout = None
Packit 423ecb
    if 'resext' in testDescription:
Packit 423ecb
        if testDescription['resext'] == 'None':
Packit 423ecb
            noResult = 1
Packit 423ecb
        else:
Packit 423ecb
            ext = '.' + testDescription['resext']
Packit 423ecb
    else:
Packit 423ecb
        ext = ''
Packit 423ecb
    if not noResult:
Packit 423ecb
        try:
Packit 423ecb
            fname = errbase + filename + ext
Packit 423ecb
            expout = open(fname, 'rt')
Packit 423ecb
        except:
Packit 423ecb
            print "Can't open result file %s - bypassing test" % fname
Packit 423ecb
            return
Packit 423ecb
Packit 423ecb
    noErrors = 0
Packit 423ecb
    if 'reserrext' in testDescription:
Packit 423ecb
        if testDescription['reserrext'] == 'None':
Packit 423ecb
            noErrors = 1
Packit 423ecb
        else:
Packit 423ecb
            if len(testDescription['reserrext'])>0:
Packit 423ecb
                ext = '.' + testDescription['reserrext']
Packit 423ecb
            else:
Packit 423ecb
                ext = ''
Packit 423ecb
    else:
Packit 423ecb
        ext = ''
Packit 423ecb
    if not noErrors:
Packit 423ecb
        try:
Packit 423ecb
            fname = errbase + filename + ext
Packit 423ecb
            experr = open(fname, 'rt')
Packit 423ecb
        except:
Packit 423ecb
            experr = None
Packit 423ecb
    else:
Packit 423ecb
        experr = None
Packit 423ecb
Packit 423ecb
    pin, pout, perr = os.popen3(cmd)
Packit 423ecb
    if 'stdin' in testDescription:
Packit 423ecb
        infile = open(inbase + filename, 'rt')
Packit 423ecb
        pin.writelines(infile.readlines())
Packit 423ecb
        infile.close()
Packit 423ecb
        pin.close()
Packit 423ecb
Packit 423ecb
    # popen is great fun, but can lead to the old "deadly embrace", because
Packit 423ecb
    # synchronizing the writing (by the task being run) of stdout and stderr
Packit 423ecb
    # with respect to the reading (by this task) is basically impossible.  I
Packit 423ecb
    # tried several ways to cheat, but the only way I have found which works
Packit 423ecb
    # is to do a *very* elementary multi-threading approach.  We can only hope
Packit 423ecb
    # that Python threads are implemented on the target system (it's okay for
Packit 423ecb
    # Linux and Windows)
Packit 423ecb
Packit 423ecb
    th1Flag = []	# flags to show when threads finish
Packit 423ecb
    th2Flag = []
Packit 423ecb
    outfile = []	# lists to contain the pipe data
Packit 423ecb
    errfile = []
Packit 423ecb
    th1 = thread.start_new_thread(readPfile, (pout, outfile, th1Flag))
Packit 423ecb
    th2 = thread.start_new_thread(readPfile, (perr, errfile, th2Flag))
Packit 423ecb
    while (len(th1Flag)==0) or (len(th2Flag)==0):
Packit 423ecb
        time.sleep(0.001)
Packit 423ecb
    if not noResult:
Packit 423ecb
        ret = compFiles(outfile, expout, inbase, 'test/')
Packit 423ecb
        if ret != 0:
Packit 423ecb
            print 'trouble with %s' % cmd
Packit 423ecb
    else:
Packit 423ecb
        if len(outfile) != 0:
Packit 423ecb
            for l in outfile:
Packit 423ecb
                print l
Packit 423ecb
            print 'trouble with %s' % cmd
Packit 423ecb
    if experr != None:
Packit 423ecb
        ret = compFiles(errfile, experr, inbase, 'test/')
Packit 423ecb
        if ret != 0:
Packit 423ecb
            print 'trouble with %s' % cmd
Packit 423ecb
    else:
Packit 423ecb
        if not noErrors:
Packit 423ecb
            if len(errfile) != 0:
Packit 423ecb
                for l in errfile:
Packit 423ecb
                    print l
Packit 423ecb
                print 'trouble with %s' % cmd
Packit 423ecb
Packit 423ecb
    if 'stdin' not in testDescription:
Packit 423ecb
        pin.close()
Packit 423ecb
Packit 423ecb
# This routine is called by the parameter decoding routine whenever the end of a
Packit 423ecb
# 'test' section is encountered.  Depending upon file globbing, a large number of
Packit 423ecb
# individual tests may be run.
Packit 423ecb
def runTest(description):
Packit 423ecb
    testDescription = defaultParams.copy()		# set defaults
Packit 423ecb
    testDescription.update(description)			# override with current ent
Packit 423ecb
    if 'testname' in testDescription:
Packit 423ecb
        print "## %s" % testDescription['testname']
Packit 423ecb
    if not 'file' in testDescription:
Packit 423ecb
        print "No file specified - can't run this test!"
Packit 423ecb
        return
Packit 423ecb
    # Set up the source and results directory paths from the decoded params
Packit 423ecb
    dir = ''
Packit 423ecb
    if 'srcdir' in testDescription:
Packit 423ecb
        dir += testDescription['srcdir'] + '/'
Packit 423ecb
    if 'srcsub' in testDescription:
Packit 423ecb
        dir += testDescription['srcsub'] + '/'
Packit 423ecb
Packit 423ecb
    rdir = ''
Packit 423ecb
    if 'resdir' in testDescription:
Packit 423ecb
        rdir += testDescription['resdir'] + '/'
Packit 423ecb
    if 'ressub' in testDescription:
Packit 423ecb
        rdir += testDescription['ressub'] + '/'
Packit 423ecb
Packit 423ecb
    testFiles = glob.glob(os.path.abspath(dir + testDescription['file']))
Packit 423ecb
    if testFiles == []:
Packit 423ecb
        print "No files result from '%s'" % testDescription['file']
Packit 423ecb
        return
Packit 423ecb
Packit 423ecb
    # Some test programs just don't work (yet).  For now we exclude them.
Packit 423ecb
    count = 0
Packit 423ecb
    excl = []
Packit 423ecb
    if 'exclfile' in testDescription:
Packit 423ecb
        for f in string.split(testDescription['exclfile']):
Packit 423ecb
            glb = glob.glob(dir + f)
Packit 423ecb
            for g in glb:
Packit 423ecb
                excl.append(os.path.abspath(g))
Packit 423ecb
Packit 423ecb
    # Run the specified test program
Packit 423ecb
    for f in testFiles:
Packit 423ecb
        if not os.path.isdir(f):
Packit 423ecb
            if f not in excl:
Packit 423ecb
                count = count + 1
Packit 423ecb
                runOneTest(testDescription, os.path.basename(f), dir, rdir)
Packit 423ecb
Packit 423ecb
#
Packit 423ecb
# The following classes are used with the xmlreader interface to interpret the
Packit 423ecb
# parameter file.  Once a test section has been identified, runTest is called
Packit 423ecb
# with a dictionary containing the parsed results of the interpretation.
Packit 423ecb
#
Packit 423ecb
Packit 423ecb
class testDefaults:
Packit 423ecb
    curText = ''	# accumulates text content of parameter
Packit 423ecb
Packit 423ecb
    def addToDict(self, key):
Packit 423ecb
        txt = string.strip(self.curText)
Packit 423ecb
#        if txt == '':
Packit 423ecb
#            return
Packit 423ecb
        if key not in defaultParams:
Packit 423ecb
            defaultParams[key] = txt
Packit 423ecb
        else:
Packit 423ecb
            defaultParams[key] += ' ' + txt
Packit 423ecb
        
Packit 423ecb
    def processNode(self, reader, curClass):
Packit 423ecb
        if reader.Depth() == 2:
Packit 423ecb
            if reader.NodeType() == 1:
Packit 423ecb
                self.curText = ''	# clear the working variable
Packit 423ecb
            elif reader.NodeType() == 15:
Packit 423ecb
                if (reader.Name() != '#text') and (reader.Name() != '#comment'):
Packit 423ecb
                    self.addToDict(reader.Name())
Packit 423ecb
        elif reader.Depth() == 3:
Packit 423ecb
            if reader.Name() == '#text':
Packit 423ecb
                self.curText += reader.Value()
Packit 423ecb
Packit 423ecb
        elif reader.NodeType() == 15:	# end of element
Packit 423ecb
            print "Defaults have been set to:"
Packit 423ecb
            for k in defaultParams.keys():
Packit 423ecb
                print "   %s : '%s'" % (k, defaultParams[k])
Packit 423ecb
            curClass = rootClass()
Packit 423ecb
        return curClass
Packit 423ecb
Packit 423ecb
Packit 423ecb
class testClass:
Packit 423ecb
    def __init__(self):
Packit 423ecb
        self.testParams = {}	# start with an empty set of params
Packit 423ecb
        self.curText = ''	# and empty text
Packit 423ecb
Packit 423ecb
    def addToDict(self, key):
Packit 423ecb
        data = string.strip(self.curText)
Packit 423ecb
        if key not in self.testParams:
Packit 423ecb
            self.testParams[key] = data
Packit 423ecb
        else:
Packit 423ecb
            if self.testParams[key] != '':
Packit 423ecb
                data = ' ' + data
Packit 423ecb
            self.testParams[key] += data
Packit 423ecb
Packit 423ecb
    def processNode(self, reader, curClass):
Packit 423ecb
        if reader.Depth() == 2:
Packit 423ecb
            if reader.NodeType() == 1:
Packit 423ecb
                self.curText = ''	# clear the working variable
Packit 423ecb
                if reader.Name() not in self.testParams:
Packit 423ecb
                    self.testParams[reader.Name()] = ''
Packit 423ecb
            elif reader.NodeType() == 15:
Packit 423ecb
                if (reader.Name() != '#text') and (reader.Name() != '#comment'):
Packit 423ecb
                    self.addToDict(reader.Name())
Packit 423ecb
        elif reader.Depth() == 3:
Packit 423ecb
            if reader.Name() == '#text':
Packit 423ecb
                self.curText += reader.Value()
Packit 423ecb
Packit 423ecb
        elif reader.NodeType() == 15:	# end of element
Packit 423ecb
            runTest(self.testParams)
Packit 423ecb
            curClass = rootClass()
Packit 423ecb
        return curClass
Packit 423ecb
Packit 423ecb
Packit 423ecb
class rootClass:
Packit 423ecb
    def processNode(self, reader, curClass):
Packit 423ecb
        if reader.Depth() == 0:
Packit 423ecb
            return curClass
Packit 423ecb
        if reader.Depth() != 1:
Packit 423ecb
            print "Unexpected junk: Level %d, type %d, name %s" % (
Packit 423ecb
                  reader.Depth(), reader.NodeType(), reader.Name())
Packit 423ecb
            return curClass
Packit 423ecb
        if reader.Name() == 'test':
Packit 423ecb
            curClass = testClass()
Packit 423ecb
            curClass.testParams = {}
Packit 423ecb
        elif reader.Name() == 'defaults':
Packit 423ecb
            curClass = testDefaults()
Packit 423ecb
        return curClass
Packit 423ecb
Packit 423ecb
def streamFile(filename):
Packit 423ecb
    try:
Packit 423ecb
        reader = libxml2.newTextReaderFilename(filename)
Packit 423ecb
    except:
Packit 423ecb
        print "unable to open %s" % (filename)
Packit 423ecb
        return
Packit 423ecb
Packit 423ecb
    curClass = rootClass()
Packit 423ecb
    ret = reader.Read()
Packit 423ecb
    while ret == 1:
Packit 423ecb
        curClass = curClass.processNode(reader, curClass)
Packit 423ecb
        ret = reader.Read()
Packit 423ecb
Packit 423ecb
    if ret != 0:
Packit 423ecb
        print "%s : failed to parse" % (filename)
Packit 423ecb
Packit 423ecb
# OK, we're finished with all the routines.  Now for the main program:-
Packit 423ecb
if len(sys.argv) != 2:
Packit 423ecb
    print "Usage: maketest {filename}"
Packit 423ecb
    sys.exit(-1)
Packit 423ecb
Packit 423ecb
streamFile(sys.argv[1])