early python port of the FIT test framework (http://fit.zwiki.org/)

root / FileRunner.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
"""
Python translation of fit..
which is copyright (c) 2002 Cunningham & Cunningham, Inc.
Released under the terms of the GNU General Public License version 2 or later.
"""

import string, re, time, sys, os, stat
from Parse import Parse
from Fixture import Fixture

class FileRunner:
    input = None
    tables = None
    fixture = Fixture()
    output = None
    outfile = None

    def __init__(self,argv):
        if len(argv) != 3:
            sys.stderr.write(
                "usage: python FileRunner.py input-file output-file\n")
            sys.exit(-1)
        if argv[1] == '-':
            infile = sys.stdin
            modtime = 'none (stdin)'
        else:
            infile = open(argv[1],'r')
            modtime = time.ctime(os.fstat(infile.fileno())[stat.ST_MTIME])
        if argv[2] == '-':
            self.outfile = sys.stdout
        else:
            self.outfile = open(argv[2],'w')
        Fixture.summary["input file"] = os.path.abspath(argv[1])
        Fixture.summary["input update"] = modtime
        Fixture.summary["output file"] = os.path.abspath(argv[2])
        self.input = infile.read()
        if infile != sys.stdin: infile.close()
        self.output = self.outfile

    def run(self):
        self.process()
        self.exit()

    __call__ = run

    def process(self):
        try:
            self.tables = Parse(self.input)
            self.fixture.doTables(self.tables)
        except Exception, e:
            self.exception(e)
        self.output.write(str(self.tables))
       
    def exception(self,e):
        tables = Parse(tag="body",
                       body="Unable to parse input. Input ignored.",
                       parts=None,
                       more=None)
        Fixture.exception(self.fixture, tables, e)

    def exit(self):
        if self.outfile != sys.stdout: self.outfile.close()
        sys.stderr.write(Fixture.counts(self.fixture)+'\n')
        sys.exit(Fixture.wrongs + Fixture.exceptions)
    
if __name__ == '__main__':
    FileRunner(sys.argv).run()