summaryrefslogtreecommitdiff
blob: f5a68f5e80cee01639fe935e2c43f04b95a17a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python

# Copyright (C) 2001-2019 Artifex Software, Inc.
# All Rights Reserved.
#
# This software is provided AS-IS with no warranty, either express or
# implied.
#
# This software is distributed under license and may not be copied,
# modified or distributed except as expressly authorized under the terms
# of the license contained in the file LICENSE in this distribution.
#
# Refer to licensing information at http://www.artifex.com or contact
# Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
# CA 94945, U.S.A., +1(415)492-9861, for further information.
#


#
# gscheck_pdfwrite.py
#
# compares Ghostscript against a baseline made from file->pdf->raster->md5sum.
# this test tries to detect Ghostscript changes that affect the pdfwrite driver.

myself="gscheck_pdfwrite.py"

import os, stat
import calendar, string, time
import gstestutils
import gsconf, gstestgs, gsparamsets, gssum, gsutil
import shutil

class GSPDFWriteCompareTestCase(gstestgs.GhostscriptTestCase):
    def shortDescription(self):
        file = "%s.pdf.%s.%d.%d" % (self.file[string.rindex(self.file, '/') + 1:], self.device, self.dpi, self.band)
	rasterfilename = gsconf.rasterdbdir + file + ".gz"

        if self.band:
            banded = "banded"
        else:
            banded = "noband"
        filename_base= os.path.basename(self.file)
        filename_details= "%s (%s/%ddpi/%s)" % (filename_base, self.device, self.dpi,banded)

        message="pdfwrite testing "+filename_details

	if not os.access(rasterfilename, os.F_OK):
		message="ERROR \ncannot find "+rasterfilename+" for "+filename_details
		print myself,message
		self.skip = 1
        else:
            ct = time.localtime(os.stat(rasterfilename)[stat.ST_MTIME])
            baseline_date = "%s %d, %4d %02d:%02d" % (calendar.month_abbr[ct[1]], ct[2], ct[0], ct[3], ct[4])

            message="Checking pdfwrite of %s against baseline set on %s" % (filename_details,baseline_date)

        return message
	
    def runTest(self):
        if hasattr(self, "skip") and self.skip:
	    self.assert_(True)
	    return

        file1 = '%s.%s.%d.%d.pdf' % (self.file[string.rindex(self.file, '/') + 1:], 'pdf', self.dpi, self.band)
	file2 = '%s.pdf.%s.%d.%d' % (self.file[string.rindex(self.file, '/') + 1:], self.device, self.dpi, self.band)

	gs = gstestgs.Ghostscript()

	gs.gsroot = self.gsroot
	gs.dpi = self.dpi
	gs.band = self.band
	gs.infile = self.file
	if self.log_stdout:
	    gs.log_stdout = self.log_stdout
	if self.log_stderr:
	    gs.log_stderr = self.log_stderr

	# do file->PDF conversion
	gs.device = 'pdfwrite'
        gs.dpi = None
	gs.outfile = file1
	if not gs.process():
	    self.fail("non-zero exit code trying to create pdf file from " + self.file)

	# do PDF->device (pbmraw, pgmraw, ppmraw, pkmraw)
	gs.device = self.device
        gs.dpi = self.dpi
	gs.infile = file1
	gs.outfile = file2
	if not gs.process():
	    self.fail("non-zero exit code trying to rasterize " + file1)

        if os.path.exists(file1):
            shutil.move(file1, gsconf.datadir+"/raster.daily")
#           os.unlink(file1)
        else:
	    self.fail("output file "+file1+" was not created for input file: " + file1)
            
        if os.path.exists(file2):
            sum = gssum.make_sum(file2)
            if not sum:
                self.fail("no checksum for output file "+file2+" was not created for input file: " + self.file)
            shutil.move(file2, gsconf.datadir+"/raster.daily")
#           os.unlink(file2)
        else:
	    self.fail("output file "+file2+" was not created for input file: " + file2)
	
	# add test result to daily database
	if self.track_daily:
            if gsconf.__dict__.has_key("checksumdb") and gsconf.checksumdb:
                dbname=gsconf.dailydir+gsconf.checksumdb # mhw+".db"
            else:
                dbname=gsconf.get_dailydb_name()
            gssum.add_file(file2, dbname=dbname, sum=sum)

        else:
            outputfile=file2
            if gssum.exists(outputfile,gsconf.baselinedb):
                sum_baseline=gssum.get_sum(outputfile,gsconf.baselinedb)
                message=myself+' checksum did not match baseline (' + outputfile + ') for input file: ' + self.file
                self.assertEqual(sum,sum_baseline,message)
            else:
                message = myself+" no baseline checksum (" + outputfile + ") for file: " + self.file
                self.fail(message)

# Add the tests defined in this file to a suite

def add_compare_test(suite, gsroot, testfile, device, dpi, band, track, now=None):

    logdir=gsconf.logdir
    if now == None:
        now=time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime())
    prefix=logdir+now+"."

    log_stdout=prefix+gsconf.gs_stdout
    log_stderr=prefix+gsconf.gs_stderr

    suite.addTest(GSPDFWriteCompareTestCase(gsroot=gsroot,
					    file=gsconf.comparefiledir + testfile,
					    device=device,dpi=dpi,band=band, 
                                            log_stdout=log_stdout,
                                            log_stderr=log_stderr,
                                            track_daily=track,now=now)
                  )

def addTests(suite,gsroot,now,options=None, **args):
    if options:
        pass # future implementation possible
    
    if args.has_key('track'):
        track = args['track']
    else:
        track = 0
    
    # get a list of test files
    comparefiles = os.listdir(gsconf.comparefiledir)
    comparefiles.sort()

#    for testfile in comparefiles:
#        print myself,testfile

    for testfile in comparefiles:
        if gsutil.check_extension(testfile):
	    for params in gsparamsets.pdftestparamsets:
	        add_compare_test(suite,
                                 gsroot,testfile,params.device,params.resolution,params.banding,track)

if __name__ == "__main__":
    gstestutils.gsRunTestsMain(addTests)