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

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

Ensures the testsuite is sorted in the same order it was generated.

  • 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    testsuite.sort()
91    nbtests = len(testsuite)
92    for testfname in testsuite :
93        sys.stdout.write("Testing %(testfname)s ... " % locals())
94        sys.stdout.flush()
95        size = computeSize(testfname)
96        if size != mastersize :
97            if not size :
98                sys.stdout.write("ERROR : Unsupported file format\n")
99            else :   
100                sys.stdout.write("WARN : Found %(size)i pages instead of %(mastersize)i\n" % locals())
101            failed += 1
102        else :   
103            sys.stdout.write("OK\n")
104            passed += 1
105    sys.stdout.write("Passed : %i (%.2f%%)\n" % (passed, 100.0 * passed / nbtests))
106    sys.stdout.write("Failed : %i (%.2f%%)\n" % (failed, 100.0 * failed / nbtests))
107           
108def main() :       
109    """Main function."""
110    if len(sys.argv) == 1 :
111        sys.argv.append("-")
112    if len(sys.argv) != 2 :
113        sys.stderr.write("usage : gengstests.py [inputfile.ps]\n")
114        sys.exit(-1)
115    else :   
116        checksum = md5.new() # Ensures we'll recreate a new testsuite if input is different
117        infilename = sys.argv[1]
118        istemp = False
119        if infilename == "-" :
120            # Input is standard input, so we must use a temporary
121            # file to be able to loop over all available devices.
122            tmp = tempfile.NamedTemporaryFile(mode="w+b")
123            istemp = True
124            infilename = tmp.name
125            while True :
126                data = sys.stdin.read(MEGABYTE)
127                if not data :
128                    break
129                tmp.write(data)
130                checksum.update(data)
131            tmp.flush()   
132        else :   
133            checksum.update(infilename)
134        genTestSuite(infilename, "testsuite.%s" % checksum.hexdigest())
135        runTests(infilename, "testsuite")
136           
137        if istemp :   
138            # Cleanly takes care of the temporary file
139            tmp.close()
140           
141if __name__ == "__main__" :
142    sys.exit(main())
143       
Note: See TracBrowser for help on using the browser.