root / pkpgcounter / trunk / tests / gstests.py @ 503

Revision 503, 5.0 kB (checked in by jerome, 16 years ago)

Improved readability of results.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id Revision
RevLine 
[496]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# $Id$
21#
22#
23
24"""This script generates a testsuite from a PostScript input file and ghostscript."""
25
26import sys
27import os
28import glob
29import tempfile
30
31MEGABYTE = 1024 * 1024
32
33def getAvailableDevices() :
34    """Returns a list of available GhostScript devices.
35   
36       The list is returned without any x11, bbox, nor ijs related device.
37    """
38    answerfd = os.popen('/bin/echo "devicenames ==" | gs -dBATCH -dQUIET -dNOPAUSE -dPARANOIDSAFER -sDEVICE=nullpage -', "r")
39    answer = answerfd.readline().strip()
40    answerfd.close()
41    if answer.startswith("[/") and answer.endswith("]") :
42        devices = [ dev[1:] for dev in answer[1:-1].split() \
43                                if dev.startswith("/") \
44                                   and (not dev.startswith("/x11")) \
45                                   and (not dev == "/ijs") \
[498]46                                   and (not dev == "/nullpage") \
[496]47                                   and (not dev == "/bbox") ]
48        devices.sort()                           
49        return devices
50    else :
51        return []
52       
53def genTestSuite(infilename, root) :
54    """Generate the testsuite."""
55    for device in getAvailableDevices() :
56        outfilename = "%(root)s.%(device)s" % locals()
57        if not os.path.exists(outfilename) :
58            sys.stdout.write("Generating %(outfilename)s " % locals())
59            sys.stdout.flush()
60            os.system('gs -dQUIET -dBATCH -dNOPAUSE -dPARANOIDSAFER -sOutputFile="%(outfilename)s" -sDEVICE="%(device)s" "%(infilename)s"' % locals())
61            sys.stdout.write("\n")
62        else :   
63            sys.stdout.write("Skipping %(outfilename)s : already exists.\n" % locals())
64           
65        if not os.path.exists(outfilename) :
66            sys.stderr.write("ERROR while generating %(outfilename)s\n" % locals())
67           
68def computeSize(filename) :   
69    """Computes the size in pages of a file in the testsuite."""
70    answerfd = os.popen('pkpgcounter "%(filename)s"' % locals(), "r")
71    try :
72        try :
73            return int(answerfd.readline().strip())
74        except (ValueError, TypeError) :   
75            return 0
76    finally :       
77        answerfd.close()
78   
79def runTests(masterfilename, root) :
80    """Launches the page counting tests against the testsuite."""
81    mastersize = computeSize(masterfilename)
82    if not mastersize :
83        raise RuntimeError, "Unable to compute the size of the testsuite's master file %(masterfilename)s" % locals()
84       
85    passed = 0
86    failed = 0
87    testsuite = glob.glob("%(root)s.*" % locals())
88    nbtests = len(testsuite)
89    for testfname in testsuite :
[499]90        sys.stdout.write("Testing %(testfname)s ... " % locals())
91        sys.stdout.flush()
[496]92        size = computeSize(testfname)
93        if size != mastersize :
[503]94            if not size :
95                sys.stdout.write("ERROR : Unsupported file format\n")
96            else :   
97                sys.stdout.write("ERROR : Found %(size)i pages instead of %(mastersize)i\n" % locals())
[496]98            failed += 1
99        else :   
[499]100            sys.stdout.write("OK\n")
[496]101            passed += 1
[501]102    print "Passed : %i (%.2f%%)" % (passed, 100.0 * passed / nbtests)
103    print "Failed : %i (%.2f%%)" % (failed, 100.0 * failed / nbtests)
[496]104           
105def main() :       
106    """Main function."""
107    if len(sys.argv) == 1 :
108        sys.argv.append("-")
109    if len(sys.argv) != 2 :
110        sys.stderr.write("usage : gengstests.py [inputfile.ps]\n")
111        sys.exit(-1)
112    else :   
113        infilename = sys.argv[1]
114        istemp = False
115        if infilename == "-" :
116            # Input is standard input, so we must use a temporary
117            # file to be able to loop over all available devices.
118            tmp = tempfile.NamedTemporaryFile(mode="w+b")
119            istemp = True
120            infilename = tmp.name
121            while True :
122                data = sys.stdin.read(MEGABYTE)
123                if not data :
124                    break
125                tmp.write(data)
126            tmp.flush()   
127           
128        genTestSuite(infilename, "testsuite")
[499]129        runTests(infilename, "testsuite")
[496]130           
131        if istemp :   
132            # Cleanly takes care of the temporary file
133            tmp.close()
134           
135if __name__ == "__main__" :
136    sys.exit(main())
137       
Note: See TracBrowser for help on using the browser.