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 | |
---|
26 | import sys |
---|
27 | import os |
---|
28 | import glob |
---|
29 | import md5 |
---|
30 | import tempfile |
---|
31 | |
---|
32 | MEGABYTE = 1024 * 1024 |
---|
33 | |
---|
34 | def 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 | |
---|
54 | def 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 | |
---|
69 | def 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 | |
---|
80 | def 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 = failed = unsupported = 0 |
---|
88 | testsuite = glob.glob("%(root)s.*" % locals()) |
---|
89 | testsuite.sort() |
---|
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 | unsupported += 1 |
---|
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/%i (%.2f%%)\n" % (passed, nbtests, 100.0 * passed / nbtests)) |
---|
106 | sys.stdout.write(" Failed : %i/%i (%.2f%%)\n" % (failed, nbtests, 100.0 * failed / nbtests)) |
---|
107 | sys.stdout.write("Unsupported : %i/%i (%.2f%%)\n" % (unsupported, nbtests, 100.0 * unsupported / nbtests)) |
---|
108 | |
---|
109 | def main() : |
---|
110 | """Main function.""" |
---|
111 | if len(sys.argv) == 1 : |
---|
112 | sys.argv.append("-") |
---|
113 | if len(sys.argv) != 2 : |
---|
114 | sys.stderr.write("usage : gengstests.py [inputfile.ps]\n") |
---|
115 | sys.exit(-1) |
---|
116 | else : |
---|
117 | checksum = md5.new() # Ensures we'll recreate a new testsuite if input is different |
---|
118 | infilename = sys.argv[1] |
---|
119 | istemp = False |
---|
120 | if infilename == "-" : |
---|
121 | # Input is standard input, so we must use a temporary |
---|
122 | # file to be able to loop over all available devices. |
---|
123 | tmp = tempfile.NamedTemporaryFile(mode="w+b") |
---|
124 | istemp = True |
---|
125 | infilename = tmp.name |
---|
126 | while True : |
---|
127 | data = sys.stdin.read(MEGABYTE) |
---|
128 | if not data : |
---|
129 | break |
---|
130 | tmp.write(data) |
---|
131 | checksum.update(data) |
---|
132 | tmp.flush() |
---|
133 | else : |
---|
134 | checksum.update(infilename) |
---|
135 | genTestSuite(infilename, "testsuite.%s" % checksum.hexdigest()) |
---|
136 | runTests(infilename, "testsuite") |
---|
137 | |
---|
138 | if istemp : |
---|
139 | # Cleanly takes care of the temporary file |
---|
140 | tmp.close() |
---|
141 | |
---|
142 | if __name__ == "__main__" : |
---|
143 | sys.exit(main()) |
---|
144 | |
---|