import sys     # for argv[]
import re

'''testreport.py [TRUNK log file] [1.4.x log file]'''

renamed_tests = { "create and modify large file (seed=488019309)" : 
                   "create and modify large file (seed=2209549105)", 
                  "create and modify medium file (seed=488019309)" :
                   "create and modify medium file (seed=2209549105)",
                  "'move --force' should not lose local mods" :
                   "'move' should not lose local mods",
                  "veriy unlocking with wrong lock token" :
                   "verify unlocking with wrong lock token",
                  "test last_valid/last_valid2 (seed:488284934)" : 
                   "test last_valid/last_valid2 (seed:2210174105)",
                  "write/read props in wc only (ps, pl, pdel)" :
                   "write/read props in wc only (ps, pl, pdel, pe)",
                  "test svn_repos_dir_delta" : 
                   "test svn_repos_dir_delta2",
                  "random combine delta test, seed = 488175559" : 
                   "random combine delta test, seed = 2210033480",
                  "random delta test, seed = 488175559" :
                   "random delta test, seed = 2210033480",
                  "test svn_fs_base__canonicalize_abspath" :
                   "test svn_fs__canonicalize_abspath",
                  "merge with part already present (seed:488003684)" :
                   "merge with part already present (seed:2209361605)",
                  "random 3-way merge (seed:488003684)" :
                   "random 3-way merge (seed:2209361605)",
                  "random trivial merge (seed:488003684)" :
                   "random trivial merge (seed:2209361605)"
                   }

def testreport():
  if len(sys.argv) > 1:
    file1 = sys.argv[1]
    file2 = sys.argv[2]
  else:
    sys.exit(1)

  dict1 = filter_file(file1)
  dict2 = filter_file(file2)
#  sys.exit(1)

  print "Nr. of tests on 1.4.x: %s" % len(dict2)
  print "Nr. of tests on trunk: %s (= +%s)" % (len(dict1), len(dict1)-len(dict2))
  print

  print "Regressions: Tests now failing on trunk!!:"
  lines = []
  for test in dict1.keys():
    if dict1[test][0] != '':
      if dict2.has_key(test):
        if dict2[test][0] != dict1[test][0]:
          lines.append(" trunk: %s - %*s  %s" % (dict1[test][0], 18, dict1[test][1], test))
  sort_and_print(lines) 

  print "Existing tests fixed on trunk:"
  lines = []
  for test in dict2.keys():
    if dict2[test][0] != '':
      if dict1.has_key(test):
        if dict2[test][0] != dict1[test][0]:
          lines.append(" 1.4.x: %s - %*s  %s" % (dict2[test][0], 18, dict2[test][1], test))
  sort_and_print(lines)

  print "Existing tests not fixed on trunk:"
  lines = []
  for test in dict2.keys():
    if dict2[test][0] != '':
      if dict1.has_key(test):
        if dict2[test][0] == dict1[test][0]:
          lines.append(" %*s - %*s  %s" % (5, dict1[test][0], 18, dict1[test][1], test))
  sort_and_print(lines)

  print "Failing tests added on trunk:"
  lines = []
  for test in dict1.keys():
    if not test in renamed_tests.values():
      if dict1[test][0] != '':
        if not dict2.has_key(test):
          lines.append(" %*s - %*s  %s" % (5, dict1[test][0], 18, dict1[test][1], test))
  sort_and_print(lines)

  print "Passing tests added on trunk:"
  lines = []
  for test in dict1.keys():
    if not test in renamed_tests.values():
      if dict1[test][0] == '':
        if not dict2.has_key(test):
          lines.append("       %*s  %s" % (20, dict1[test][1], test))
  sort_and_print(lines)

  print "Tests renamed on trunk:"
  lines = []
  for test in dict2.keys():
    if not dict1.has_key(test):
      lines.append("       %*s  %s\n" % (20, dict2[test][1], test) + 
                   "       %*sto:   %s" % (17, ' ', renamed_tests[test]))
  sort_and_print(lines)

def sort_and_print(lines):
  lines.sort(strcmp)
  for line in lines:
    print line
  print

def strcmp(x, y):
  return cmp(x.strip(), y.strip())

def filter_file(file):
  '''opens a test log file, filter out the tests and return the resulting lines
     in a list'''

  fp = open(file, 'r')
  lines = fp.readlines()
  fp.close()
  dict = {}

  # filter out all lines not matching the test line pattern
  cur_file = ''
  filter_re = re.compile(r' +(\d+) .*')
  for line in lines:
    match = re.match("START: (.*)", line)
    if match:
      cur_file = match.group(1).strip()
    if re.match("END: (.*)", line):
      cur_file = ''

    # print line.strip()
    match = filter_re.match(line)
    if match:
      # print line.strip()
      test = line[15:-1].strip()
      mode = line[8:13].strip()
      dict[test] = (mode, cur_file)

  return dict

if __name__ == '__main__':
  testreport()

