1#    Copyright (C) 2001 Artifex Software Inc.
2#    All Rights Reserved.
3#
4# This software is provided AS-IS with no warranty, either express or
5# implied.
6#
7# This software is distributed under license and may not be copied, modified
8# or distributed except as expressly authorized under the terms of that
9# license.  Refer to licensing information at http://www.artifex.com/
10# or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11# San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12
13# $Id: gstestgs.py 9206 2008-11-06 23:51:27Z marcos $
14
15# gstestgs.py
16#
17# base classes for regression testing
18
19import os, time
20import string
21import gsconf
22from gstestutils import GSTestCase
23
24myself="gstestgs.py"
25
26class Ghostscript:
27	def __init__(self):
28		self.device = ''
29		self.dpi = 72
30		self.band = 0
31		self.infile = 'input'
32		if os.name == 'nt':
33			self.nullfile = 'nul'
34		else:
35			self.nullfile = '/dev/null'
36		self.outfile = self.nullfile
37
38		# log file options
39		# NOTE: we always append to the log.  if it is desired to start a new
40		# log, it is the responsibility of the caller to clear/erase the old one.
41
42		self.log_stdout = self.nullfile
43		self.log_stderr = self.nullfile
44
45	def log_message(self,message):
46		try:
47			log = open(self.log_stdout, "a")
48			log.write(message+"\n")
49			log.close()
50		except:
51			pass
52
53	def process(self):
54		bandsize = 30000000
55		if (self.band): bandsize = 10000
56
57		gsroot=self.gsroot
58		execpath = gsroot+'bin/gs'
59
60		arguments = ' -sOutputFile=\'%s\' ' % (self.outfile,)
61
62		if gsconf.fontdir:
63			FontPath=gsconf.fontdir
64		else:
65			FontPath = ''
66		ResourcePath=gsroot+'lib/'
67		IPath = ResourcePath+':'+FontPath
68		if IPath:
69			arguments+= ' -I' + IPath
70
71
72		arguments += ' -sDEVICE=%s ' % (self.device,)
73		if self.dpi:
74			arguments += '-r%d ' % (self.dpi,)
75
76		arguments += ' -q -Z: -dNOPAUSE -dBATCH -K1000000'
77		arguments += ' -dMaxBitmap=%d' % (bandsize,)
78		arguments += ' -dNOOUTERSAVE -dJOBSERVER -c false 0 startjob pop -f'
79
80		if string.lower(self.infile[-4:]) == ".pdf" or string.lower(self.infile[-3:]) == ".ai":
81			arguments += ' -dFirstPage=1 -dLastPage=1 '
82			infile = self.infile
83		else:
84			# for some tests, input from stdin is required - use it all the time
85			infile = ' - < '+self.infile
86
87		if self.log_stdout and self.log_stderr:
88			capture=' >> %s 2>> %s' % (self.log_stdout, self.log_stderr)
89		else:
90			capture=''
91
92		fullcommand='/usr/local/bin/time -f "%U %S %E %P" '+execpath+arguments+" "+infile+" "+capture
93		# mhw fullcommand=execpath+arguments+" "+infile+" "+capture
94
95		# before we execute the command which appends to the log
96		# we output a message to record the commandline that generates the log entry.
97
98		infilename=os.path.basename(self.infile)
99		comment=' '.join((infilename,"to",self.outfile))
100
101		if len(self.log_stdout) > 0 and self.log_stdout != self.nullfile:
102			try:
103				log = open(self.log_stdout, "a")
104				log.write("=== "+comment+"\n")
105				log.write(fullcommand+"\n")
106				log.write("---\n")
107				log.close()
108			except:
109				pass
110		if len(self.log_stderr) > 0 and self.log_stderr != self.nullfile:
111			try:
112				log = open(self.log_stderr, "a")
113				log.write("==="+comment+"\n")
114				log.write(fullcommand+"\n")
115				log.write("---\n")
116				log.close()
117			except:
118				pass
119
120		if self.__dict__.has_key("verbose") and self.verbose:
121			print fullcommand
122
123		if self.log_stdout and self.log_stderr:
124			datecommand='/bin/date +%%s.%%N >> %s 2>> %s' % (self.log_stdout, self.log_stderr)
125		else:
126			datecommand=''
127
128		# os.system(datecommand)
129		gs_return=os.system(fullcommand)
130		# os.system(datecommand)
131
132		if gs_return == 0:
133			return 1
134		else:
135			return 0
136
137
138class GhostscriptTestCase(GSTestCase):
139	def __init__(self,
140		     gsroot='gsroot',
141		     dpi=72, band=0, file='test.ps', device='pdfwrite', log_stdout='', log_stderr='', track_daily=0,now=None):
142
143		self.gsroot = gsroot
144
145		self.file = file
146		self.device = device
147		self.dpi = dpi
148		self.band = band
149		self.log_stdout = log_stdout
150		self.log_stderr = log_stderr
151		self.track_daily = track_daily
152		self.now = now
153		GSTestCase.__init__(self)
154
155class GSCrashTestCase(GhostscriptTestCase):
156	def runTest(self):
157		gs = Ghostscript()
158		gs.gsroot = self.gsroot
159		gs.device = self.device
160		gs.dpi = self.dpi
161		gs.band = self.band
162		gs.infile = self.file
163		self.assert_(gs.process(), 'ghostscript failed to render file: ' + self.file)
164