José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ########################################################################## |
| 3 | # |
| 4 | # Copyright 2011 Jose Fonseca |
| 5 | # All Rights Reserved. |
| 6 | # |
| 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: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in |
| 15 | # all copies or substantial portions of the Software. |
| 16 | # |
| 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 |
| 23 | # THE SOFTWARE. |
| 24 | # |
| 25 | ##########################################################################/ |
| 26 | |
| 27 | '''Run two retrace instances in parallel, comparing generated snapshots. |
| 28 | ''' |
| 29 | |
| 30 | |
| 31 | import optparse |
| 32 | import os.path |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 33 | import subprocess |
| 34 | import platform |
| 35 | import sys |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 36 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 37 | from PIL import Image |
| 38 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 39 | from snapdiff import Comparer |
José Fonseca | 0190896 | 2012-03-16 09:56:09 +0000 | [diff] [blame] | 40 | from highlight import AutoHighlighter |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 41 | import jsondiff |
| 42 | |
| 43 | |
| 44 | # Null file, to use when we're not interested in subprocesses output |
| 45 | if platform.system() == 'Windows': |
| 46 | NULL = open('NUL:', 'wt') |
| 47 | else: |
| 48 | NULL = open('/dev/null', 'wt') |
| 49 | |
| 50 | |
| 51 | class Setup: |
| 52 | |
| 53 | def __init__(self, args, env=None): |
| 54 | self.args = args |
| 55 | self.env = env |
| 56 | |
José Fonseca | 2030303 | 2011-10-20 16:12:10 +0200 | [diff] [blame] | 57 | def _retrace(self, args): |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 58 | cmd = [ |
| 59 | options.retrace, |
José Fonseca | 2030303 | 2011-10-20 16:12:10 +0200 | [diff] [blame] | 60 | ] + args + self.args |
| 61 | try: |
| 62 | return subprocess.Popen(cmd, env=self.env, stdout=subprocess.PIPE, stderr=NULL) |
| 63 | except OSError, ex: |
| 64 | sys.stderr.write('error: failed to execute %s: %s\n' % (cmd[0], ex.strerror)) |
| 65 | sys.exit(1) |
| 66 | |
| 67 | def retrace(self): |
| 68 | return self._retrace([ |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 69 | '-s', '-', |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 70 | '-S', options.snapshot_frequency, |
José Fonseca | 2030303 | 2011-10-20 16:12:10 +0200 | [diff] [blame] | 71 | ]) |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 72 | |
| 73 | def dump_state(self, call_no): |
| 74 | '''Get the state dump at the specified call no.''' |
| 75 | |
José Fonseca | 2030303 | 2011-10-20 16:12:10 +0200 | [diff] [blame] | 76 | p = self._retrace([ |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 77 | '-D', str(call_no), |
José Fonseca | 2030303 | 2011-10-20 16:12:10 +0200 | [diff] [blame] | 78 | ]) |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 79 | state = jsondiff.load(p.stdout) |
| 80 | p.wait() |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame] | 81 | return state.get('parameters', {}) |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 82 | |
José Fonseca | d8ea58f | 2012-02-09 14:35:27 +0000 | [diff] [blame] | 83 | def diff_state(self, ref_call_no, src_call_no, stream): |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 84 | '''Compare the state between two calls.''' |
| 85 | |
| 86 | ref_state = self.dump_state(ref_call_no) |
| 87 | src_state = self.dump_state(src_call_no) |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame] | 88 | |
José Fonseca | d8ea58f | 2012-02-09 14:35:27 +0000 | [diff] [blame] | 89 | stream.flush() |
| 90 | differ = jsondiff.Differ(stream) |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 91 | differ.visit(ref_state, src_state) |
José Fonseca | d8ea58f | 2012-02-09 14:35:27 +0000 | [diff] [blame] | 92 | stream.write('\n') |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 93 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 94 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 95 | def read_pnm(stream): |
| 96 | '''Read a PNM from the stream, and return the image object, and the comment.''' |
| 97 | |
| 98 | magic = stream.readline() |
| 99 | if not magic: |
| 100 | return None, None |
| 101 | assert magic.rstrip() == 'P6' |
| 102 | comment = '' |
| 103 | line = stream.readline() |
| 104 | while line.startswith('#'): |
| 105 | comment += line[1:] |
| 106 | line = stream.readline() |
| 107 | width, height = map(int, line.strip().split()) |
| 108 | maximum = int(stream.readline().strip()) |
| 109 | assert maximum == 255 |
| 110 | data = stream.read(height * width * 3) |
| 111 | image = Image.frombuffer('RGB', (width, height), data, 'raw', 'RGB', 0, 1) |
| 112 | return image, comment |
| 113 | |
| 114 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 115 | def parse_env(optparser, entries): |
| 116 | '''Translate a list of NAME=VALUE entries into an environment dictionary.''' |
| 117 | |
| 118 | env = os.environ.copy() |
| 119 | for entry in entries: |
| 120 | try: |
| 121 | name, var = entry.split('=', 1) |
| 122 | except Exception: |
| 123 | optparser.error('invalid environment entry %r' % entry) |
| 124 | env[name] = var |
| 125 | return env |
| 126 | |
| 127 | |
| 128 | def main(): |
| 129 | '''Main program. |
| 130 | ''' |
| 131 | |
| 132 | global options |
| 133 | |
| 134 | # Parse command line options |
| 135 | optparser = optparse.OptionParser( |
| 136 | usage='\n\t%prog [options] -- [glretrace options] <trace>', |
| 137 | version='%%prog') |
| 138 | optparser.add_option( |
| 139 | '-r', '--retrace', metavar='PROGRAM', |
| 140 | type='string', dest='retrace', default='glretrace', |
| 141 | help='retrace command [default: %default]') |
| 142 | optparser.add_option( |
| 143 | '--ref-env', metavar='NAME=VALUE', |
| 144 | type='string', action='append', dest='ref_env', default=[], |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 145 | help='add variable to reference environment') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 146 | optparser.add_option( |
| 147 | '--src-env', metavar='NAME=VALUE', |
| 148 | type='string', action='append', dest='src_env', default=[], |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 149 | help='add variable to source environment') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 150 | optparser.add_option( |
| 151 | '--diff-prefix', metavar='PATH', |
| 152 | type='string', dest='diff_prefix', default='.', |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 153 | help='prefix for the difference images') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 154 | optparser.add_option( |
| 155 | '-t', '--threshold', metavar='BITS', |
| 156 | type="float", dest="threshold", default=12.0, |
| 157 | help="threshold precision [default: %default]") |
| 158 | optparser.add_option( |
José Fonseca | 225193d | 2012-01-26 19:08:32 +0000 | [diff] [blame] | 159 | '-S', '--snapshot-frequency', metavar='CALLSET', |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 160 | type="string", dest="snapshot_frequency", default='draw', |
José Fonseca | 225193d | 2012-01-26 19:08:32 +0000 | [diff] [blame] | 161 | help="calls to compare [default: %default]") |
José Fonseca | d8ea58f | 2012-02-09 14:35:27 +0000 | [diff] [blame] | 162 | optparser.add_option( |
| 163 | '-o', '--output', metavar='FILE', |
| 164 | type="string", dest="output", |
| 165 | help="output file [default: stdout]") |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 166 | |
| 167 | (options, args) = optparser.parse_args(sys.argv[1:]) |
| 168 | ref_env = parse_env(optparser, options.ref_env) |
| 169 | src_env = parse_env(optparser, options.src_env) |
| 170 | if not args: |
| 171 | optparser.error("incorrect number of arguments") |
| 172 | |
| 173 | ref_setup = Setup(args, ref_env) |
| 174 | src_setup = Setup(args, src_env) |
| 175 | |
José Fonseca | d8ea58f | 2012-02-09 14:35:27 +0000 | [diff] [blame] | 176 | if options.output: |
| 177 | output = open(options.output, 'wt') |
| 178 | else: |
| 179 | output = sys.stdout |
| 180 | |
José Fonseca | 0190896 | 2012-03-16 09:56:09 +0000 | [diff] [blame] | 181 | highligher = AutoHighlighter(output) |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame] | 182 | |
| 183 | highligher.write('call\tprecision\n') |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 184 | |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 185 | last_bad = -1 |
José Fonseca | 36fa87c | 2011-09-06 00:15:32 +0100 | [diff] [blame] | 186 | last_good = 0 |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 187 | ref_proc = ref_setup.retrace() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 188 | try: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 189 | src_proc = src_setup.retrace() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 190 | try: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 191 | while True: |
| 192 | # Get the reference image |
| 193 | ref_image, ref_comment = read_pnm(ref_proc.stdout) |
| 194 | if ref_image is None: |
| 195 | break |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 196 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 197 | # Get the source image |
| 198 | src_image, src_comment = read_pnm(src_proc.stdout) |
| 199 | if src_image is None: |
| 200 | break |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 201 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 202 | assert ref_comment == src_comment |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 203 | |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 204 | call_no = int(ref_comment.strip()) |
| 205 | |
| 206 | # Compare the two images |
| 207 | comparer = Comparer(ref_image, src_image) |
| 208 | precision = comparer.precision() |
| 209 | |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame] | 210 | mismatch = precision < options.threshold |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 211 | |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame] | 212 | if mismatch: |
| 213 | highligher.color(highligher.red) |
| 214 | highligher.bold() |
| 215 | highligher.write('%u\t%f\n' % (call_no, precision)) |
| 216 | if mismatch: |
| 217 | highligher.normal() |
| 218 | |
| 219 | if mismatch: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 220 | if options.diff_prefix: |
| 221 | prefix = os.path.join(options.diff_prefix, '%010u' % call_no) |
| 222 | prefix_dir = os.path.dirname(prefix) |
| 223 | if not os.path.isdir(prefix_dir): |
| 224 | os.makedirs(prefix_dir) |
| 225 | ref_image.save(prefix + '.ref.png') |
| 226 | src_image.save(prefix + '.src.png') |
| 227 | comparer.write_diff(prefix + '.diff.png') |
| 228 | if last_bad < last_good: |
José Fonseca | d8ea58f | 2012-02-09 14:35:27 +0000 | [diff] [blame] | 229 | src_setup.diff_state(last_good, call_no, output) |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 230 | last_bad = call_no |
| 231 | else: |
| 232 | last_good = call_no |
| 233 | |
José Fonseca | b96ab8e | 2011-09-06 10:22:56 +0100 | [diff] [blame] | 234 | highligher.flush() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 235 | finally: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 236 | src_proc.terminate() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 237 | finally: |
José Fonseca | bcca5f7 | 2011-09-06 00:07:41 +0100 | [diff] [blame] | 238 | ref_proc.terminate() |
José Fonseca | 0b956fd | 2011-06-04 22:51:45 +0100 | [diff] [blame] | 239 | |
| 240 | |
| 241 | if __name__ == '__main__': |
| 242 | main() |