regression.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/python
  2. import filecmp
  3. import os
  4. import sys
  5. import shlex
  6. import subprocess
  7. import json
  8. import shutil
  9. import platform as python_platform
  10. def msg(msg):
  11. print msg+"...",
  12. def result(msg):
  13. print msg;
  14. def failure(msg):
  15. type, value = sys.exc_info()[:2]
  16. if None == msg:
  17. msg=value
  18. # raise
  19. # print msg
  20. sys.exit(msg)
  21. def init(mode, type):
  22. global TARGET_MODE
  23. global TARGET_TYPE
  24. global TMP_DIR
  25. global BUILD_DIR
  26. global DOCS_DIR
  27. TARGET_MODE=mode.lower()
  28. TARGET_TYPE=type.lower()
  29. if "nt"==os.name:
  30. TMP_DIR="win32"+os.sep+mode
  31. else:
  32. try:
  33. msg("Reading configuration...")
  34. f=open("build"+os.sep+"configure.ctx", "r")
  35. ctx=eval(f.read())
  36. f.close()
  37. result("OK")
  38. except:
  39. failure("Can not open \"configure.ctx\". Try running ./configure first!")
  40. msg("Guessing platform...")
  41. if 1==len(ctx["platforms"]):
  42. platform=ctx["platforms"][0]
  43. result(platform)
  44. TMP_DIR=os.path.join("build", platform, TARGET_TYPE)
  45. else:
  46. failure("Error. Please configure with exactly one platform!")
  47. BUILD_DIR=TMP_DIR
  48. DOCS_DIR="test_docs"
  49. def tmp(filename):
  50. return os.path.join(TMP_DIR, filename)
  51. def build(filename):
  52. return os.path.join(BUILD_DIR, filename)
  53. def docs(filename):
  54. return os.path.join(DOCS_DIR, filename)
  55. def rm(filename):
  56. try:
  57. msg("remove "+filename)
  58. if os.path.exists(filename):
  59. os.remove(filename)
  60. result("OK")
  61. except:
  62. failure(None)
  63. def cp(src, dst):
  64. try:
  65. msg("copy "+src+" to "+dst)
  66. shutil.copyfile(src, dst)
  67. result("OK")
  68. except:
  69. failure(None)
  70. def ensureDir(dir):
  71. try:
  72. if not(os.path.exists(dir)):
  73. msg("creating dir "+dir)
  74. os.makedirs(dir)
  75. result("OK")
  76. except:
  77. failure(None)
  78. def call(path, pre, args, outfile, post, attr):
  79. try:
  80. env={}
  81. if TARGET_TYPE=="shared":
  82. if python_platform.system().lower()=="darwin":
  83. env["DYLD_LIBRARY_PATH"]=os.path.abspath(BUILD_DIR)
  84. elif python_platform.system().lower()=="linux":
  85. env["LD_LIBRARY_PATH"]=os.path.abspath(BUILD_DIR)
  86. msg("executing "+path)
  87. if os.path.exists(tmp("stderr.txt")):
  88. os.remove(tmp("stderr.txt"))
  89. if os.path.exists(outfile):
  90. os.remove(outfile)
  91. _args = [ path ]
  92. _args.extend(args)
  93. err = open(tmp("stderr.txt"), "w")
  94. out = open(outfile, "w")
  95. ret=subprocess.call(_args, stdout=out, stderr=err, env=env)
  96. out.close()
  97. err.close()
  98. if "return" in attr and ret!=int(attr["return"]):
  99. failure("ERROR: return code is "+str(ret)+" but "+str(attr["return"])+" is expected.")
  100. result("OK")
  101. except:
  102. failure(None)
  103. def normlinebreak(path):
  104. try:
  105. msg("normalize line breaks "+path+"...")
  106. newlines = []
  107. changed = False
  108. for line in open(path, 'rb').readlines():
  109. if line[-2:] == '\r\n':
  110. line = line[:-2] + '\n'
  111. changed = True
  112. newlines.append(line)
  113. if changed:
  114. open(path, 'wb').writelines(newlines)
  115. result("Changed")
  116. else:
  117. result("OK")
  118. except:
  119. failure(None)
  120. def regr(regrpath, path, normalize):
  121. if normalize:
  122. normlinebreak(regrpath)
  123. normlinebreak(path)
  124. msg("regression testing "+path+" against "+regrpath)
  125. eq=filecmp.cmp(regrpath, path)
  126. if (eq):
  127. result("OK")
  128. else:
  129. failure("Regression test failed!")
  130. def compile(path):
  131. try:
  132. msg("compiling "+path)
  133. if os.path.exists(tmp("stderr.txt")):
  134. os.remove(tmp("stderr.txt"))
  135. if os.path.exists(tmp("stdout.txt")):
  136. os.remove(tmp("stdout.txt"))
  137. err = open(tmp("stderr.txt"), "w")
  138. out = open(tmp("stdout.txt"), "w")
  139. exe=os.path.abspath(os.path.splitext(path)[0]+".exe")
  140. obj=os.path.abspath(os.path.splitext(path)[0]+".obj")
  141. if TARGET_MODE == "debug":
  142. rtl="/MDd"
  143. else:
  144. rtl="/MD"
  145. cl_args=["cl",
  146. os.path.abspath(path),
  147. "/Fo"+obj,
  148. "/TP", "/c", rtl,
  149. "/I..\\..\\config\\win32-msvc\\zlib-1.2.5",
  150. "/I..\\..\\third_party\\zlib-1.2.5",
  151. "/I..\\..\\config\\win32-msvc\\libxml2-2.7.7",
  152. "/I..\\..\\third_party\\libxml2-2.7.7\\include\\libxml",
  153. "/I..\\..\\third_party\\libxml2-2.7.7\\include",
  154. "/I..\\..\\plib\\config\\msvc\\plib\\include",
  155. "/I..\\..\\config",
  156. "/I..\\..",
  157. "/D", "WIN32",
  158. "/D", "LIBXML_STATIC",
  159. "/D","_UNICODE",
  160. "/D", "UNICODE" ]
  161. link_args=["link", "/OUT:"+os.path.abspath(exe), os.path.abspath(os.path.splitext(path)[0]+".obj"),"/NOLOGO", "/MACHINE:X86",
  162. "kernel32.lib", "user32.lib", "gdi32.lib", "winspool.lib", "comdlg32.lib", "advapi32.lib", "shell32.lib", "ole32.lib", "oleaut32.lib", "uuid.lib", "odbc32.lib", "odbccp32.lib",
  163. "mce.lib", "opc.lib", "plib.lib", "zlib.lib", "xml.lib" ]
  164. cwd=os.getcwd()
  165. os.chdir(tmp(""))
  166. ret=subprocess.call(cl_args, stdout=out, stderr=err)
  167. if ret==0:
  168. ret=subprocess.call(link_args, stdout=out, stderr=err)
  169. os.chdir(cwd)
  170. out.close()
  171. err.close()
  172. if ret==0:
  173. result("OK")
  174. else:
  175. out = open(tmp("stdout.txt"), "r")
  176. out_str=out.read()
  177. out.close()
  178. failure("FAIL:\n"+out_str)
  179. return exe
  180. except:
  181. failure(None)