| 1 |
#!/usr/bin/python |
| 2 |
## |
| 3 |
# CU - C unit testing framework |
| 4 |
# --------------------------------- |
| 5 |
# Copyright (c)2007,2008 Daniel Fiser <[email protected]> |
| 6 |
# |
| 7 |
# |
| 8 |
# This file is part of CU. |
| 9 |
# |
| 10 |
# CU is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU Lesser General Public License as |
| 12 |
# published by the Free Software Foundation; either version 3 of |
| 13 |
# the License, or (at your option) any later version. |
| 14 |
# |
| 15 |
# CU is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU Lesser General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU Lesser General Public License |
| 21 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
# |
| 23 |
|
| 24 |
from subprocess import Popen, PIPE |
| 25 |
import os |
| 26 |
import re |
| 27 |
import sys |
| 28 |
import math |
| 29 |
from getopt import gnu_getopt, GetoptError |
| 30 |
|
| 31 |
EPS = 0.6 |
| 32 |
BASE_DIR = "." |
| 33 |
MAX_DIFF_LINES = 20 |
| 34 |
EXACT = False |
| 35 |
|
| 36 |
PROGRESS_ON = True |
| 37 |
MSG_BASE = "" |
| 38 |
|
| 39 |
class Hunk: |
| 40 |
""" This class represents one hunk from diff. """ |
| 41 |
|
| 42 |
def __init__(self): |
| 43 |
self.added = [] |
| 44 |
self.deleted = [] |
| 45 |
self.lines = [] |
| 46 |
|
| 47 |
# to identify lines with floating point numbers |
| 48 |
self.re_is_num = re.compile("^.*[0-9].*$") |
| 49 |
|
| 50 |
# pattern to match floating point number |
| 51 |
self.num_pattern = r"-?(?:(?:[0-9]+(?:\.[0-9]*)?)|(?:\.[0-9]+))(?:[eE]-?[0-9]+)?" |
| 52 |
self.re_num = re.compile(self.num_pattern) |
| 53 |
|
| 54 |
def numLines(self): |
| 55 |
return len(self.lines) |
| 56 |
def numLinesAdded(self): |
| 57 |
return len(self.added) |
| 58 |
def numLinesDeleted(self): |
| 59 |
return len(self.deleted) |
| 60 |
|
| 61 |
def addLineAdded(self, line): |
| 62 |
self.added.append(line) |
| 63 |
def addLineDeleted(self, line): |
| 64 |
self.deleted.append(line) |
| 65 |
def addLine(self, line): |
| 66 |
self.lines.append(line) |
| 67 |
|
| 68 |
def getLines(self): |
| 69 |
return self.lines |
| 70 |
def getLinesAdded(self): |
| 71 |
return self.added |
| 72 |
def getLinesDeleted(self): |
| 73 |
return self.deleted |
| 74 |
|
| 75 |
def __eq(self, num1, num2): |
| 76 |
""" Returns True if num1 equals to num2 with respect to EPS |
| 77 |
(defined above) """ |
| 78 |
return math.fabs(num1 - num2) < EPS |
| 79 |
|
| 80 |
def checkFloats(self): |
| 81 |
""" This method try to check if only difference between added and |
| 82 |
deleted lines of this hunk is different precission of floating |
| 83 |
point numbers |
| 84 |
""" |
| 85 |
|
| 86 |
# If number of added and deleted lines differs, then there is more |
| 87 |
# differences that precission of floating point numbers |
| 88 |
if self.numLinesAdded() != self.numLinesDeleted(): |
| 89 |
return False |
| 90 |
|
| 91 |
for i in xrange(0, self.numLinesAdded()): |
| 92 |
# if any line does not contain number - return False because |
| 93 |
# there must be more differences than in numbers |
| 94 |
if not self.re_is_num.match(self.added[i]) \ |
| 95 |
or not self.re_is_num.match(self.deleted[i]): |
| 96 |
return False |
| 97 |
|
| 98 |
line1 = self.added[i] |
| 99 |
line2 = self.deleted[i] |
| 100 |
|
| 101 |
# Extract all floating point numbers from each line |
| 102 |
nums1 = self.re_num.findall(line1) |
| 103 |
nums2 = self.re_num.findall(line2) |
| 104 |
# and remove all empty strings |
| 105 |
nums1 = filter(lambda x: len(x) > 0, nums1) |
| 106 |
nums2 = filter(lambda x: len(x) > 0, nums2) |
| 107 |
|
| 108 |
# if length of list nums1 does not equal to length of nums2 |
| 109 |
# return False |
| 110 |
if len(nums1) != len(nums2): |
| 111 |
return False |
| 112 |
|
| 113 |
# iterate trough all numbers |
| 114 |
for j in xrange(0, len(nums1)): |
| 115 |
# if numbers do not equal to each other return False |
| 116 |
if not self.__eq(float(nums1[j]), float(nums2[j])): |
| 117 |
return False |
| 118 |
|
| 119 |
# compare the rest of lines |
| 120 |
line1 = self.re_num.sub("", line1) |
| 121 |
line2 = self.re_num.sub("", line2) |
| 122 |
if line1 != line2: |
| 123 |
return False |
| 124 |
|
| 125 |
# If it does not fail anywhere, added and deleted lines must be |
| 126 |
# same |
| 127 |
return True |
| 128 |
|
| 129 |
|
| 130 |
class Diff: |
| 131 |
""" Represents whole diff. """ |
| 132 |
|
| 133 |
def __init__(self): |
| 134 |
self.hunks = [] |
| 135 |
self.lines = 0 |
| 136 |
self.omitted_lines = 0 |
| 137 |
|
| 138 |
def addHunk(self, hunk): |
| 139 |
self.hunks.append(hunk) |
| 140 |
self.lines += hunk.numLines() |
| 141 |
|
| 142 |
def numLines(self): |
| 143 |
return self.lines |
| 144 |
def numOmittedLines(self): |
| 145 |
return self.omitted_lines |
| 146 |
|
| 147 |
def getHunks(self): |
| 148 |
return self.hunks |
| 149 |
def numHunks(self): |
| 150 |
return len(self.hunks) |
| 151 |
|
| 152 |
def checkFloats(self): |
| 153 |
""" Will call method checkFloats on each hunk """ |
| 154 |
hks = self.hunks[:] |
| 155 |
self.hunks = [] |
| 156 |
self.lines = 0 |
| 157 |
for h in hks: |
| 158 |
if not h.checkFloats(): |
| 159 |
self.hunks.append(h) |
| 160 |
self.lines += h.numLines() |
| 161 |
else: |
| 162 |
self.omitted_lines += h.numLines() |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
class Parser: |
| 167 |
def __init__(self, fin): |
| 168 |
self.fin = fin |
| 169 |
self.line = "" |
| 170 |
self.diff = Diff() |
| 171 |
self.cur_hunk = None |
| 172 |
|
| 173 |
# to recognize beginning of hunk: |
| 174 |
self.re_hunk = re.compile(r"^[0-9]*(,[0-9]*){0,1}[a-zA-Z]?[0-9]*(,[0-9]*){0,1}$") |
| 175 |
|
| 176 |
self.re_added = re.compile(r"^> (.*)$") |
| 177 |
self.re_deleted = re.compile(r"^< (.*)$") |
| 178 |
|
| 179 |
def __readNextLine(self): |
| 180 |
self.line = self.fin.readline() |
| 181 |
if len(self.line) == 0: |
| 182 |
return False |
| 183 |
return True |
| 184 |
|
| 185 |
def parse(self): |
| 186 |
global PROGRESS_ON |
| 187 |
global MSG_BASE |
| 188 |
|
| 189 |
num_lines = 0 |
| 190 |
while self.__readNextLine(): |
| 191 |
# beggining of hunk |
| 192 |
if self.re_hunk.match(self.line): |
| 193 |
if self.cur_hunk is not None: |
| 194 |
self.diff.addHunk(self.cur_hunk) |
| 195 |
self.cur_hunk = Hunk() |
| 196 |
self.cur_hunk.addLine(self.line) |
| 197 |
|
| 198 |
# line added |
| 199 |
match = self.re_added.match(self.line) |
| 200 |
if match is not None: |
| 201 |
self.cur_hunk.addLine(self.line) |
| 202 |
self.cur_hunk.addLineAdded(match.group(1)) |
| 203 |
|
| 204 |
# line deleted |
| 205 |
match = self.re_deleted.match(self.line) |
| 206 |
if match is not None: |
| 207 |
self.cur_hunk.addLine(self.line) |
| 208 |
self.cur_hunk.addLineDeleted(match.group(1)) |
| 209 |
|
| 210 |
num_lines += 1 |
| 211 |
|
| 212 |
if PROGRESS_ON and num_lines % 50 == 0: |
| 213 |
print MSG_BASE, "[ %08d ]" % num_lines, "\r", |
| 214 |
sys.stdout.flush() |
| 215 |
|
| 216 |
# last push to list of hunks |
| 217 |
if self.cur_hunk is not None: |
| 218 |
self.diff.addHunk(self.cur_hunk) |
| 219 |
|
| 220 |
if PROGRESS_ON: |
| 221 |
print MSG_BASE, " ", "\r", |
| 222 |
sys.stdout.flush() |
| 223 |
|
| 224 |
def getDiff(self): |
| 225 |
return self.diff |
| 226 |
|
| 227 |
|
| 228 |
def regressionFilesInDir(): |
| 229 |
""" Returns sorted list of pairs of filenames where first name in pair |
| 230 |
is tmp. file and second corresponding file with saved regressions. |
| 231 |
""" |
| 232 |
|
| 233 |
re_tmp_out_file = re.compile(r"tmp\.(.*\.out)") |
| 234 |
re_tmp_err_file = re.compile(r"tmp\.(.*\.err)") |
| 235 |
files = [] |
| 236 |
|
| 237 |
all_files = os.listdir(".") |
| 238 |
all_files.sort() |
| 239 |
for file in all_files: |
| 240 |
res = re_tmp_out_file.match(file) |
| 241 |
if res is not None: |
| 242 |
fname = res.group(1) |
| 243 |
tmp = [file, ""] |
| 244 |
for file2 in all_files: |
| 245 |
if file2 == fname: |
| 246 |
tmp = [file, file2,] |
| 247 |
break |
| 248 |
files.append(tmp) |
| 249 |
|
| 250 |
res = re_tmp_err_file.match(file) |
| 251 |
if res is not None: |
| 252 |
fname = res.group(1) |
| 253 |
tmp = [file, ""] |
| 254 |
for file2 in all_files: |
| 255 |
if file2 == fname: |
| 256 |
tmp = [file, file2,] |
| 257 |
break |
| 258 |
files.append(tmp) |
| 259 |
|
| 260 |
return files |
| 261 |
|
| 262 |
|
| 263 |
def MSG(str = "", wait = False): |
| 264 |
if wait: |
| 265 |
print str, |
| 266 |
else: |
| 267 |
print str |
| 268 |
def MSGOK(prestr = "", str = "", poststr = ""): |
| 269 |
print prestr, "\033[0;32m" + str + "\033[0;0m", poststr |
| 270 |
def MSGFAIL(prestr = "", str = "", poststr = ""): |
| 271 |
print prestr, "\033[0;31m" + str + "\033[0;0m", poststr |
| 272 |
def MSGINFO(prestr = "", str = "", poststr = ""): |
| 273 |
print prestr, "\033[0;33m" + str + "\033[0;0m", poststr |
| 274 |
def dumpLines(lines, prefix = "", wait = False, max_lines = -1): |
| 275 |
line_num = 0 |
| 276 |
if wait: |
| 277 |
for line in lines: |
| 278 |
print prefix, line, |
| 279 |
line_num += 1 |
| 280 |
if max_lines >= 0 and line_num > max_lines: |
| 281 |
break |
| 282 |
else: |
| 283 |
for line in lines: |
| 284 |
print prefix, line |
| 285 |
line_num += 1 |
| 286 |
if max_lines >= 0 and line_num > max_lines: |
| 287 |
break |
| 288 |
|
| 289 |
def main(files): |
| 290 |
global MSG_BASE |
| 291 |
|
| 292 |
# As first compute length of columns |
| 293 |
len1 = 0 |
| 294 |
len2 = 0 |
| 295 |
for filenames in files: |
| 296 |
if len(filenames[0]) > len1: |
| 297 |
len1 = len(filenames[0]) |
| 298 |
if len(filenames[1]) > len2: |
| 299 |
len2 = len(filenames[1]) |
| 300 |
|
| 301 |
for filenames in files: |
| 302 |
if len(filenames[1]) == 0: |
| 303 |
MSGFAIL("", "===", "Can't compare %s %s, bacause %s does not exist!" % \ |
| 304 |
(filenames[0], filenames[0][4:], filenames[0][4:])) |
| 305 |
continue |
| 306 |
|
| 307 |
cmd = ["diff", filenames[0], filenames[1]] |
| 308 |
MSG_BASE = "Comparing %s and %s" % \ |
| 309 |
(filenames[0].ljust(len1) ,filenames[1].ljust(len2)) |
| 310 |
if not PROGRESS_ON: |
| 311 |
print MSG_BASE, |
| 312 |
sys.stdout.flush() |
| 313 |
|
| 314 |
pipe = Popen(cmd, stdout=PIPE) |
| 315 |
parser = Parser(pipe.stdout) |
| 316 |
parser.parse() |
| 317 |
diff = parser.getDiff() |
| 318 |
if not EXACT: |
| 319 |
diff.checkFloats() |
| 320 |
|
| 321 |
if PROGRESS_ON: |
| 322 |
print MSG_BASE, |
| 323 |
|
| 324 |
if diff.numHunks() == 0: |
| 325 |
MSGOK(" [", "OK", "]") |
| 326 |
if diff.numOmittedLines() > 0: |
| 327 |
MSGINFO(" -->", str(diff.numOmittedLines()) + " lines from diff omitted") |
| 328 |
else: |
| 329 |
MSGFAIL(" [", "FAILED", "]") |
| 330 |
if diff.numOmittedLines() > 0: |
| 331 |
MSGINFO(" -->", str(diff.numOmittedLines()) + " lines from diff omitted") |
| 332 |
MSGINFO(" -->", "Diff has " + str(diff.numLines()) + " lines") |
| 333 |
|
| 334 |
if diff.numLines() <= MAX_DIFF_LINES: |
| 335 |
MSGINFO(" -->", "Diff:") |
| 336 |
for h in diff.getHunks(): |
| 337 |
dumpLines(h.getLines(), " |", True) |
| 338 |
else: |
| 339 |
MSGINFO(" -->", "Printing only first " + str(MAX_DIFF_LINES) + " lines:") |
| 340 |
lines = [] |
| 341 |
for h in diff.getHunks(): |
| 342 |
lines += h.getLines() |
| 343 |
if len(lines) > MAX_DIFF_LINES: |
| 344 |
break; |
| 345 |
dumpLines(lines, " |", True, MAX_DIFF_LINES) |
| 346 |
|
| 347 |
def usage(): |
| 348 |
print "Usage: " + sys.argv[0] + " [ OPTIONS ] [ directory, [ directory, [ ... ] ] ]" |
| 349 |
print "" |
| 350 |
print " OPTIONS:" |
| 351 |
print " --help / -h none Print this help" |
| 352 |
print " --exact / -e none Switch do exact comparasion of files" |
| 353 |
print " --not-exact / -n none Switch do non exact comparasion of files (default behaviour)" |
| 354 |
print " --max-diff-lines int Maximum of lines of diff which can be printed (default " + str(MAX_DIFF_LINES) + ")" |
| 355 |
print " --eps float Precision of floating point numbers (epsilon) (default " + str(EPS) + ")" |
| 356 |
print " --no-progress none Turn off progress bar" |
| 357 |
print " --progress none Turn on progress bar (default)" |
| 358 |
print "" |
| 359 |
print " This program is able to compare files with regressions generated by CU testsuites." |
| 360 |
print " You can specify directories which are to be searched for regression files." |
| 361 |
print " In non exact copmarasion mode (which is default), this program tries to compare" |
| 362 |
print " floating point numbers in files with respect to specified precision (see --eps) and" |
| 363 |
print " those lines which differ only in precission of floating point numbers are omitted." |
| 364 |
print "" |
| 365 |
sys.exit(-1) |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
# Init: |
| 370 |
|
| 371 |
# Set up base dir |
| 372 |
BASE_DIR = os.getcwd() |
| 373 |
|
| 374 |
# Parse command line options: |
| 375 |
optlist, args = gnu_getopt(sys.argv[1:], |
| 376 |
"hen", |
| 377 |
["help", "max-diff-lines=", "eps=", \ |
| 378 |
"exact", "not-exact", \ |
| 379 |
"no-progress", "progress"]) |
| 380 |
for opt in optlist: |
| 381 |
if opt[0] == "--help" or opt[0] == "-h": |
| 382 |
usage() |
| 383 |
if opt[0] == "--exact" or opt[0] == "-e": |
| 384 |
EXACT = True |
| 385 |
if opt[0] == "--not-exact" or opt[0] == "-n": |
| 386 |
EXACT = False |
| 387 |
if opt[0] == "--max-diff-lines": |
| 388 |
MAX_DIFF_LINES = int(opt[1]) |
| 389 |
if opt[0] == "--eps": |
| 390 |
EPS = float(opt[1]) |
| 391 |
if opt[0] == "--no-progress": |
| 392 |
PROGRESS_ON = False |
| 393 |
if opt[0] == "--progress": |
| 394 |
PROGRESS_ON = True |
| 395 |
|
| 396 |
if len(args) == 0: |
| 397 |
files = regressionFilesInDir() |
| 398 |
main(files) |
| 399 |
else: |
| 400 |
for dir in args: |
| 401 |
os.chdir(BASE_DIR) |
| 402 |
|
| 403 |
MSGINFO() |
| 404 |
MSGINFO("", "Processing directory '" + dir + "':") |
| 405 |
MSGINFO() |
| 406 |
try: |
| 407 |
os.chdir(dir) |
| 408 |
except: |
| 409 |
MSGFAIL(" -->", "Directory '" + dir + "' does not exist.") |
| 410 |
files = regressionFilesInDir() |
| 411 |
main(files) |
| 412 |
|
| 413 |
sys.exit(0) |