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

Revision 3474, 11.9 kB (checked in by jerome, 15 years ago)

Changed copyright years.

  • 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
[3436]2# -*- coding: utf-8 -*-
[496]3#
4# pkpgcounter : a generic Page Description Language parser
5#
[3474]6# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
[496]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.
[3436]11#
[496]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.
[3436]16#
[496]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
[505]29import md5
[496]30import tempfile
[535]31import time
[496]32
33MEGABYTE = 1024 * 1024
34
[535]35class TestSuite :
36    """A class for the testsuite."""
37    def __init__(self, inputfile) :
38        """Initializes the testsuite."""
39        self.tmp = None
40        self.inputfile = inputfile
41        self.results = {}
42        self.supportedpct = self.failedpct = self.unsupportedpct = None
43        self.md5sum = self.computeChecksum()
44        self.mastersize = None
[3436]45
46    def __del__(self) :
[535]47        """Remove temporary file, if any."""
48        if self.tmp is not None :
49            self.tmp.close()
[3436]50
51    def computeChecksum(self) :
[535]52        """Computes an MD5 checksum for the input file's content."""
53        checksum = md5.new()
54        istemp = False
55        if self.inputfile == "-" :
56            # Input is standard input, so we must use a temporary
57            # file to be able to loop over all available devices.
58            self.tmp = tempfile.NamedTemporaryFile(mode="w+b")
59            self.inputfile = self.tmp.name
60            infile = sys.stdin
61            istemp = True
[3436]62        else :
[535]63            infile = open(self.inputfile, "rb")
[3436]64
[535]65        while True :
66            data = infile.read(MEGABYTE)
67            if not data :
68                break
[3436]69            if istemp :
[535]70                self.tmp.write(data)
71            checksum.update(data)
[3436]72
73        if istemp :
74            self.tmp.flush()
[535]75        else :
76            infile.close()
[3436]77
78        return checksum.hexdigest()
79
[535]80    def getAvailableDevices(self) :
81        """Returns a list of available GhostScript devices.
[3436]82
[535]83           The list is returned without any x11, bbox, nor ijs related device.
84        """
85        answerfd = os.popen('/bin/echo "devicenames ==" | gs -dBATCH -dQUIET -dNOPAUSE -dPARANOIDSAFER -sDEVICE=nullpage -', "r")
86        answer = answerfd.readline().strip()
87        if not answerfd.close() :
88            if answer.startswith("[/") and answer.endswith("]") :
89                devices = [ dev[1:] for dev in answer[1:-1].split() \
90                                        if dev.startswith("/") \
91                                           and (not dev.startswith("/x11")) \
92                                           and (not dev == "/ijs") \
93                                           and (not dev == "/nullpage") \
94                                           and (not dev == "/bbox") ]
[3436]95                devices.sort()
[535]96                return devices
97        return []
[3436]98
[535]99    def getAvailableIJSPrintClasses(self) :
100        """Returns a list of available IJS Print Classes.
[3436]101
[535]102           Currently the list is a static one and doesn't contain all the available print classes.
103        """
[3436]104        return [ "DJ3600", "DJ3320", "DJ9xx", "DJGenericVIP", "LJColor",
[535]105                 "DJ850", "DJ890", "DJ9xxVIP", "DJ8xx", "DJ540", "DJ660",
106                 "DJ6xx", "DJ350", "DJ6xxPhoto", "DJ630", "DJ8x5", "DJ4100",
107                 "AP21xx", "AP2560", "AP2xxx", "PSP100", "PSP470", "Undefined",
108                 "Postscript", "LJJetReady", "LJMono", "LJFastRaster",
109                 "LJZjsMono", ]
[3436]110
[535]111    def batchGeneration(self, infilename, devices, root, command) :
112        """Loops over a set of devices calling a particular command."""
113        parts = root.split(".")
114        if (len(parts) > 1) and (parts[-1] == "hpijs") :
115            devprefix = parts[-1] + "/"
116        else :
117            devprefix = ""
118        for device in devices :
119            outfilename = "%(root)s.%(device)s" % locals()
120            cmd = command % locals()
121            if os.path.exists(outfilename) and os.stat(outfilename).st_size :
122                sys.stdout.write("Skipping %(outfilename)s : already exists.\n" % locals())
[3436]123            else :
[535]124                sys.stdout.write("Generating %(outfilename)s " % locals())
125                sys.stdout.flush()
126                os.system(cmd)
127                sys.stdout.write("\n")
[3436]128
[535]129            if not os.path.exists(outfilename) :
130                sys.stderr.write("ERROR : During the generation of %(outfilename)s\n" % locals())
[3436]131            elif not os.stat(outfilename).st_size :
[535]132                sys.stderr.write("ERROR : Unsupported driver, impossible to generate %(outfilename)s\n" % locals())
133                os.remove(outfilename)
[3436]134            else :
[535]135                self.results[outfilename] = { "command" : cmd,
136                                              "device" : "%s" % (devprefix + device),
137                                              "result" : None,
138                                              "details" : None,
139                                            }
[3436]140
[535]141    def genTestSuite(self) :
142        """Generate the testsuite."""
143        root = "testsuite.%s" % self.md5sum
[3436]144        self.batchGeneration(self.inputfile, self.getAvailableDevices(),
145                                        root,
[535]146                                        'gs -dQUIET -dBATCH -dNOPAUSE -dPARANOIDSAFER -sOutputFile="%(outfilename)s" -sDEVICE="%(device)s" "%(infilename)s"')
[3436]147
148        self.batchGeneration(self.inputfile, self.getAvailableIJSPrintClasses(),
149                                        "%(root)s.hpijs" % locals(),
[535]150                                        'gs -dBATCH -dQUIET -dPARANOIDSAFER -dNOPAUSE -sDEVICE=ijs -sIjsServer=hpijs -dIjsUseOutputFD -sDeviceManufacturer="HEWLETT-PACKARD" -sDeviceModel="%(device)s" -sOutputFile="%(outfilename)s" "%(infilename)s"')
[3436]151
152    def runPipe(self, cmd) :
[544]153        """Runs a command in a pipe, returns the command's output as a string."""
154        answerfd = os.popen(cmd, "r")
[496]155        try :
[544]156            return answerfd.read().strip()
[3436]157        finally :
[535]158            answerfd.close()
[3436]159
160    def computeSize(self, filename) :
[544]161        """Computes the size in pages of a file in the testsuite."""
162        answer = self.runPipe('pkpgcounter "%(filename)s" 2>/dev/null' % locals())
163        try :
164            return int(answer)
[3436]165        except (ValueError, TypeError) :
[544]166            return 0
[3436]167
[535]168    def runTests(self) :
169        """Launches the page counting tests against the testsuite."""
170        masterfilename = self.inputfile
171        self.mastersize = mastersize = self.computeSize(masterfilename)
172        if not mastersize :
173            raise RuntimeError, "Unable to compute the size of the testsuite's master file %(masterfilename)s" % locals()
[3436]174        else :
[535]175            sys.stdout.write("Master file's contains %(mastersize)i pages.\n" % locals())
176        testsuite = glob.glob("testsuite.*")
177        testsuite.sort()
178        nbtests = len(testsuite)
179        for testfname in testsuite :
180            parts = testfname.split(".")
181            if len(parts) > 3 :
182                devname = ".".join(parts[2:])
[3436]183            else :
[535]184                devname = parts[-1]
[3436]185            result = self.results.setdefault(testfname, { "command" : "See above",
186                                                          "device" : devname,
187                                                          "result" : None,
[535]188                                                          "details" : None })
189            sys.stdout.write("Testing %(testfname)s ... " % locals())
190            sys.stdout.flush()
191            size = self.computeSize(testfname)
192            if size != mastersize :
193                if not size :
194                    result["result"] = "UNSUPPORTED"
195                    result["details"] = "Unsupported file format"
[3436]196                else :
[535]197                    result["result"] = "FAILED"
198                    result["details"] = "Found %(size)i pages instead of %(mastersize)i\n" % locals()
[3436]199            else :
[535]200                result["result"] = "SUPPORTED"
201                result["details"] = None
[3436]202            sys.stdout.write("%s\n" % result["result"])
[535]203        self.supportedpct = 100.0 * len([True for r in self.results.values() if r["result"] == "SUPPORTED"]) / nbtests
204        self.failedpct = 100.0 * len([True for r in self.results.values() if r["result"] == "FAILED"]) / nbtests
205        self.unsupportedpct = 100.0 * len([True for r in self.results.values() if r["result"] == "UNSUPPORTED"]) / nbtests
[3436]206
[535]207    def genHTMLReport(self, filename) :
208        """Generates an HTML report."""
209        reportdate = "%s (UTC)" % time.asctime(time.gmtime(time.time()))
[544]210        title = "pkpgcounter v%s report for testsuite %s generated on %s" \
211                        % (self.runPipe("pkpgcounter --version"), \
212                           self.md5sum, \
213                           reportdate)
[535]214        out = open(filename, "w")
215        out.write("<html><head><title>%s</title></head><body>\n" % title)
216        out.write("<h3>%s</h3>\n" % title)
217        out.write("<ul>\n")
218        out.write("<li>Testsuite's MD5 checksum : <strong>%s</strong></li>\n" % self.md5sum)
219        out.write("<li>Testsuite contains : <strong>%i pages</strong></li>\n" % self.mastersize)
[544]220        out.write("<li>Ghostscript used to generate testsuite : <strong>v%s</strong></li>\n" % self.runPipe("gs --version"))
[535]221        out.write("<li>Supported : <strong>%.2f%%</strong></li>\n" % self.supportedpct)
222        out.write("<li>Failed : <strong>%.2f%%</strong></li>\n" % self.failedpct)
223        out.write("<li>Unsupported : <strong>%.2f%%</strong></li>\n" % self.unsupportedpct)
224        out.write("</ul>\n")
[544]225        out.write("<p><strong>Green</strong> means that pkpgcounter obtained the expected result.</p>\n")
[559]226        out.write("<p><strong>Orange</strong> means that pkpgcounter obtained an incorrect result. <em>IMPORTANT : if only 1 page is found, this is often due to image formats which don't support multiple pages anyway.</em></p>\n")
[544]227        out.write("<p><strong>Red</strong> means that pkpgcounter doesn't recognize the input file's format.</p>\n")
[535]228        out.write('<table border="1"><tr bgcolor="gold"><th width="15%">Device</th><th width="25%">Details</th><th width="60%">Command line</th></tr>\n')
229        linecount = 0
230        keys = self.results.keys()
231        keys.sort()
232        for key in keys :
233            value = self.results[key]
[3436]234            linecount += 1
235            if not (linecount % 2) :
[535]236                linecolor = "#DEDEDE"
[3436]237            else :
[535]238                linecolor = "#FFFFFF"
[3436]239            out.write('<tr bgcolor="%s">\n' % linecolor)
[535]240            if value["result"] == "SUPPORTED" :
241                color = "#00FF00"
[3436]242            elif value["result"] == "UNSUPPORTED" :
[535]243                color = "#FF0000"
[3436]244            else :
[535]245                color = "orange"
246            out.write('<td bgcolor="%s"><strong>%s</strong></td>\n' % (color, value["device"]))
247            out.write('<td>%s</td>\n' % (value["details"] or "&nbsp;"))
248            out.write('<td><em>%s</em></td>\n' % value["command"])
249            out.write("</tr>\n")
250        out.write("</table></body></html>\n")
251        out.close()
[3436]252
253def main() :
[496]254    """Main function."""
[537]255    try :
256        if len(sys.argv) == 1 :
257            sys.argv.append("-")
258        if len(sys.argv) != 2 :
259            sys.stderr.write("usage : %s [inputfile.ps]\n" % sys.argv[0])
260            sys.exit(-1)
[3436]261        else :
[537]262            testsuite = TestSuite(sys.argv[1])
263            testsuite.genTestSuite()
264            testsuite.runTests()
265            testsuite.genHTMLReport("%s.html" % testsuite.md5sum)
[3436]266    except KeyboardInterrupt :
[537]267        sys.stderr.write("Interrupted at user's request !\n")
[3436]268
[496]269if __name__ == "__main__" :
270    sys.exit(main())
Note: See TracBrowser for help on using the browser.