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

Revision 534, 6.9 kB (checked in by jerome, 16 years ago)

Now also checks for some hpijs drivers.

  • 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    if not 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    return []
52       
53def getAvailableIJSPrintClasses() :
54    """Returns a list of available IJS Print Classes.
55   
56       Currently the list is a static one and doesn't contain all the available print classes.
57    """
58    return [ "DJ3600", "DJ3320", "DJ9xx", "DJGenericVIP", "LJColor", 
59             "DJ850", "DJ890", "DJ9xxVIP", "DJ8xx", "DJ540", "DJ660",
60             "DJ6xx", "DJ350", "DJ6xxPhoto", "DJ630", "DJ8x5", "DJ4100",
61             "AP21xx", "AP2560", "AP2xxx", "PSP100", "PSP470", "Undefined",
62             "Postscript", "LJJetReady", "LJMono", "LJFastRaster",
63             "LJZjsMono", ]
64       
65def batchGeneration(infilename, devices, root, command) :
66    """Loops over a set of devices calling a particular command."""
67    for device in devices :
68        outfilename = "%(root)s.%(device)s" % locals()
69        if os.path.exists(outfilename) and os.stat(outfilename).st_size :
70            sys.stdout.write("Skipping %(outfilename)s : already exists.\n" % locals())
71        else :   
72            sys.stdout.write("Generating %(outfilename)s " % locals())
73            sys.stdout.flush()
74            os.system(command % locals())
75            sys.stdout.write("\n")
76           
77        if not os.path.exists(outfilename) :
78            sys.stderr.write("ERROR while generating %(outfilename)s\n" % locals())
79   
80def genTestSuite(infilename, root) :
81    """Generate the testsuite."""
82    batchGeneration(infilename, getAvailableDevices(), 
83                                root, 
84                                'gs -dQUIET -dBATCH -dNOPAUSE -dPARANOIDSAFER -sOutputFile="%(outfilename)s" -sDEVICE="%(device)s" "%(infilename)s"')
85                               
86    batchGeneration(infilename, getAvailableIJSPrintClasses(), 
87                                "%(root)s.hpijs" % locals(), 
88                                'gs -dBATCH -dQUIET -dPARANOIDSAFER -dNOPAUSE -sDEVICE=ijs -sIjsServer=hpijs -dIjsUseOutputFD -sDeviceManufacturer="HEWLETT-PACKARD" -sDeviceModel="%(device)s" -sOutputFile="%(outfilename)s" "%(infilename)s"')
89           
90def computeSize(filename) :   
91    """Computes the size in pages of a file in the testsuite."""
92    answerfd = os.popen('pkpgcounter "%(filename)s" 2>/dev/null' % locals(), "r")
93    try :
94        try :
95            return int(answerfd.readline().strip())
96        except (ValueError, TypeError) :   
97            return 0
98    finally :       
99        answerfd.close()
100   
101def runTests(masterfilename, root) :
102    """Launches the page counting tests against the testsuite."""
103    mastersize = computeSize(masterfilename)
104    if not mastersize :
105        raise RuntimeError, "Unable to compute the size of the testsuite's master file %(masterfilename)s" % locals()
106    else :   
107        sys.stdout.write("Master file's contains %(mastersize)i pages.\n" % locals())
108    passed = failed = unsupported = 0
109    testsuite = glob.glob("%(root)s.*" % locals())
110    testsuite.sort()
111    nbtests = len(testsuite)
112    for testfname in testsuite :
113        sys.stdout.write("Testing %(testfname)s ... " % locals())
114        sys.stdout.flush()
115        size = computeSize(testfname)
116        if size != mastersize :
117            if not size :
118                sys.stdout.write("ERROR : Unsupported file format\n")
119                unsupported += 1
120            else :   
121                sys.stdout.write("WARN : Found %(size)i pages instead of %(mastersize)i\n" % locals())
122                failed += 1
123        else :   
124            sys.stdout.write("OK\n")
125            passed += 1
126    sys.stdout.write("     Passed : %i/%i (%.2f%%)\n" % (passed, nbtests, 100.0 * passed / nbtests))
127    sys.stdout.write("     Failed : %i/%i (%.2f%%)\n" % (failed, nbtests, 100.0 * failed / nbtests))
128    sys.stdout.write("Unsupported : %i/%i (%.2f%%)\n" % (unsupported, nbtests, 100.0 * unsupported / nbtests))
129           
130def main() :       
131    """Main function."""
132    if len(sys.argv) == 1 :
133        sys.argv.append("-")
134    if len(sys.argv) != 2 :
135        sys.stderr.write("usage : gengstests.py [inputfile.ps]\n")
136        sys.exit(-1)
137    else :   
138        checksum = md5.new() # Ensures we'll recreate a new testsuite if input is different
139        infilename = sys.argv[1]
140        istemp = False
141        if infilename == "-" :
142            # Input is standard input, so we must use a temporary
143            # file to be able to loop over all available devices.
144            tmp = tempfile.NamedTemporaryFile(mode="w+b")
145            istemp = True
146            infilename = tmp.name
147            while True :
148                data = sys.stdin.read(MEGABYTE)
149                if not data :
150                    break
151                tmp.write(data)
152                checksum.update(data)
153            tmp.flush()   
154        else :   
155            checksum.update(infilename)
156        genTestSuite(infilename, "testsuite.%s" % checksum.hexdigest())
157        runTests(infilename, "testsuite")
158           
159        if istemp :   
160            # Cleanly takes care of the temporary file
161            tmp.close()
162           
163if __name__ == "__main__" :
164    sys.exit(main())
165       
Note: See TracBrowser for help on using the browser.