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