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

Revision 505, 5.4 kB (checked in by jerome, 16 years ago)

Improved testsuite generation tool to regenerate a testsuite if the input datas
don't match that of the existing one.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id Revision
Line 
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 md5
30import tempfile
31
32MEGABYTE = 1024 * 1024
33
34def getAvailableDevices() :
35    """Returns a list of available GhostScript devices.
36   
37       The list is returned without any x11, bbox, nor ijs related device.
38    """
39    answerfd = os.popen('/bin/echo "devicenames ==" | gs -dBATCH -dQUIET -dNOPAUSE -dPARANOIDSAFER -sDEVICE=nullpage -', "r")
40    answer = answerfd.readline().strip()
41    answerfd.close()
42    if answer.startswith("[/") and answer.endswith("]") :
43        devices = [ dev[1:] for dev in answer[1:-1].split() \
44                                if dev.startswith("/") \
45                                   and (not dev.startswith("/x11")) \
46                                   and (not dev == "/ijs") \
47                                   and (not dev == "/nullpage") \
48                                   and (not dev == "/bbox") ]
49        devices.sort()                           
50        return devices
51    else :
52        return []
53       
54def genTestSuite(infilename, root) :
55    """Generate the testsuite."""
56    for device in getAvailableDevices() :
57        outfilename = "%(root)s.%(device)s" % locals()
58        if not os.path.exists(outfilename) :
59            sys.stdout.write("Generating %(outfilename)s " % locals())
60            sys.stdout.flush()
61            os.system('gs -dQUIET -dBATCH -dNOPAUSE -dPARANOIDSAFER -sOutputFile="%(outfilename)s" -sDEVICE="%(device)s" "%(infilename)s"' % locals())
62            sys.stdout.write("\n")
63        else :   
64            sys.stdout.write("Skipping %(outfilename)s : already exists.\n" % locals())
65           
66        if not os.path.exists(outfilename) :
67            sys.stderr.write("ERROR while generating %(outfilename)s\n" % locals())
68           
69def computeSize(filename) :   
70    """Computes the size in pages of a file in the testsuite."""
71    answerfd = os.popen('pkpgcounter "%(filename)s" 2>/dev/null' % locals(), "r")
72    try :
73        try :
74            return int(answerfd.readline().strip())
75        except (ValueError, TypeError) :   
76            return 0
77    finally :       
78        answerfd.close()
79   
80def runTests(masterfilename, root) :
81    """Launches the page counting tests against the testsuite."""
82    mastersize = computeSize(masterfilename)
83    if not mastersize :
84        raise RuntimeError, "Unable to compute the size of the testsuite's master file %(masterfilename)s" % locals()
85    else :   
86        sys.stdout.write("Master file's contains %(mastersize)i pages.\n" % locals())
87    passed = 0
88    failed = 0
89    testsuite = glob.glob("%(root)s.*" % locals())
90    nbtests = len(testsuite)
91    for testfname in testsuite :
92        sys.stdout.write("Testing %(testfname)s ... " % locals())
93        sys.stdout.flush()
94        size = computeSize(testfname)
95        if size != mastersize :
96            if not size :
97                sys.stdout.write("ERROR : Unsupported file format\n")
98            else :   
99                sys.stdout.write("WARN : Found %(size)i pages instead of %(mastersize)i\n" % locals())
100            failed += 1
101        else :   
102            sys.stdout.write("OK\n")
103            passed += 1
104    sys.stdout.write("Passed : %i (%.2f%%)\n" % (passed, 100.0 * passed / nbtests))
105    sys.stdout.write("Failed : %i (%.2f%%)\n" % (failed, 100.0 * failed / nbtests))
106           
107def main() :       
108    """Main function."""
109    if len(sys.argv) == 1 :
110        sys.argv.append("-")
111    if len(sys.argv) != 2 :
112        sys.stderr.write("usage : gengstests.py [inputfile.ps]\n")
113        sys.exit(-1)
114    else :   
115        checksum = md5.new() # Ensures we'll recreate a new testsuite if input is different
116        infilename = sys.argv[1]
117        istemp = False
118        if infilename == "-" :
119            # Input is standard input, so we must use a temporary
120            # file to be able to loop over all available devices.
121            tmp = tempfile.NamedTemporaryFile(mode="w+b")
122            istemp = True
123            infilename = tmp.name
124            while True :
125                data = sys.stdin.read(MEGABYTE)
126                if not data :
127                    break
128                tmp.write(data)
129                checksum.update(data)
130            tmp.flush()   
131        else :   
132            checksum.update(infilename)
133        genTestSuite(infilename, "testsuite.%s" % checksum.hexdigest())
134        runTests(infilename, "testsuite")
135           
136        if istemp :   
137            # Cleanly takes care of the temporary file
138            tmp.close()
139           
140if __name__ == "__main__" :
141    sys.exit(main())
142       
Note: See TracBrowser for help on using the browser.