2 ##########################################################################
4 # Copyright 2011 Jose Fonseca
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the 'Software'), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 ##########################################################################/
27 '''Run two retrace instances in parallel, comparing generated snapshots.
39 from snapdiff import Comparer
40 from highlight import AutoHighlighter
44 # Null file, to use when we're not interested in subprocesses output
45 if platform.system() == 'Windows':
46 NULL = open('NUL:', 'wb')
48 NULL = open('/dev/null', 'wb')
53 def __init__(self, process):
54 self.process = process
56 def nextSnapshot(self):
57 image, comment = read_pnm(self.process.stdout)
61 callNo = int(comment.strip())
67 self.process.terminate()
69 # Avoid http://bugs.python.org/issue14252
75 def __init__(self, retraceExe, args, env=None):
76 self.retraceExe = retraceExe
80 def _retrace(self, args, stdout=subprocess.PIPE):
85 for name, value in self.env.iteritems():
86 sys.stderr.write('%s=%s ' % (name, value))
87 sys.stderr.write(' '.join(cmd) + '\n')
89 return subprocess.Popen(cmd, env=self.env, stdout=stdout, stderr=NULL)
91 sys.stderr.write('error: failed to execute %s: %s\n' % (cmd[0], ex.strerror))
94 def retrace(self, args):
99 def snapshot(self, call_nos):
100 process = self._retrace([
104 return RetraceRun(process)
106 def dump_state(self, call_no):
107 '''Get the state dump at the specified call no.'''
112 state = jsondiff.load(p.stdout)
114 return state.get('parameters', {})
116 def diff_state(self, ref_call_no, src_call_no, stream):
117 '''Compare the state between two calls.'''
119 ref_state = self.dump_state(ref_call_no)
120 src_state = self.dump_state(src_call_no)
123 differ = jsondiff.Differ(stream)
124 differ.visit(ref_state, src_state)
128 def read_pnm(stream):
129 '''Read a PNM from the stream, and return the image object, and the comment.'''
131 magic = stream.readline()
134 magic = magic.rstrip()
152 raise Exception('Unsupported magic `%s`' % magic)
154 line = stream.readline()
155 while line.startswith('#'):
157 line = stream.readline()
158 width, height = map(int, line.strip().split())
159 if bytesPerChannel == 1:
160 maximum = int(stream.readline().strip())
161 assert maximum == 255
162 data = stream.read(height * width * channels * bytesPerChannel)
164 # XXX: Image magic only supports single channel floating point images,
165 # so convert to 8bit RGB
166 pixels = array('f', data)
168 pixels = array('B', pixels)
169 data = pixels.tostring()
171 image = Image.frombuffer(mode, (width, height), data, 'raw', mode, 0, 1)
172 return image, comment
175 def parse_env(optparser, entries):
176 '''Translate a list of NAME=VALUE entries into an environment dictionary.'''
181 env = os.environ.copy()
182 for entry in entries:
184 name, var = entry.split('=', 1)
186 optparser.error('invalid environment entry %r' % entry)
197 # Parse command line options
198 optparser = optparse.OptionParser(
199 usage='\n\t%prog [options] -- [glretrace options] <trace>',
201 optparser.add_option(
202 '-r', '--retrace', metavar='PROGRAM',
203 type='string', dest='retrace', default='glretrace',
204 help='retrace command [default: %default]')
205 optparser.add_option(
206 '--ref-driver', metavar='DRIVER',
207 type='string', dest='ref_driver', default=None,
208 help='force reference driver')
209 optparser.add_option(
210 '--src-driver', metavar='DRIVER',
211 type='string', dest='src_driver', default=None,
212 help='force source driver')
213 optparser.add_option(
214 '--ref-arg', metavar='OPTION',
215 type='string', action='append', dest='ref_args', default=[],
216 help='pass argument to reference retrace')
217 optparser.add_option(
218 '--src-arg', metavar='OPTION',
219 type='string', action='append', dest='src_args', default=[],
220 help='pass argument to source retrace')
221 optparser.add_option(
222 '--ref-env', metavar='NAME=VALUE',
223 type='string', action='append', dest='ref_env', default=[],
224 help='add variable to reference environment')
225 optparser.add_option(
226 '--src-env', metavar='NAME=VALUE',
227 type='string', action='append', dest='src_env', default=[],
228 help='add variable to source environment')
229 optparser.add_option(
230 '--diff-prefix', metavar='PATH',
231 type='string', dest='diff_prefix', default='.',
232 help='prefix for the difference images')
233 optparser.add_option(
234 '-t', '--threshold', metavar='BITS',
235 type="float", dest="threshold", default=12.0,
236 help="threshold precision [default: %default]")
237 optparser.add_option(
238 '-S', '--snapshot-frequency', metavar='CALLSET',
239 type="string", dest="snapshot_frequency", default='draw',
240 help="calls to compare [default: %default]")
241 optparser.add_option(
243 action='store_true', dest='diff_state', default=False,
244 help='diff state between failing calls')
245 optparser.add_option(
246 '-o', '--output', metavar='FILE',
247 type="string", dest="output",
248 help="output file [default: stdout]")
250 (options, args) = optparser.parse_args(sys.argv[1:])
251 ref_env = parse_env(optparser, options.ref_env)
252 src_env = parse_env(optparser, options.src_env)
254 optparser.error("incorrect number of arguments")
256 if options.ref_driver:
257 options.ref_args.insert(0, '--driver=' + options.ref_driver)
258 if options.src_driver:
259 options.src_args.insert(0, '--driver=' + options.src_driver)
261 refRetracer = Retracer(options.retrace, options.ref_args + args, ref_env)
262 srcRetracer = Retracer(options.retrace, options.src_args + args, src_env)
265 output = open(options.output, 'wt')
269 highligher = AutoHighlighter(output)
271 highligher.write('call\tprecision\n')
275 refRun = refRetracer.snapshot(options.snapshot_frequency)
277 srcRun = srcRetracer.snapshot(options.snapshot_frequency)
280 # Get the reference image
281 refImage, refCallNo = refRun.nextSnapshot()
285 # Get the source image
286 srcImage, srcCallNo = srcRun.nextSnapshot()
290 assert refCallNo == srcCallNo
293 # Compare the two images
294 comparer = Comparer(refImage, srcImage)
295 precision = comparer.precision()
297 mismatch = precision < options.threshold
300 highligher.color(highligher.red)
302 highligher.write('%u\t%f\n' % (callNo, precision))
307 if options.diff_prefix:
308 prefix = os.path.join(options.diff_prefix, '%010u' % callNo)
309 prefix_dir = os.path.dirname(prefix)
310 if not os.path.isdir(prefix_dir):
311 os.makedirs(prefix_dir)
312 refImage.save(prefix + '.ref.png')
313 srcImage.save(prefix + '.src.png')
314 comparer.write_diff(prefix + '.diff.png')
315 if last_bad < last_good and options.diff_state:
316 srcRetracer.diff_state(last_good, callNo, output)
328 if __name__ == '__main__':