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