utils.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import unittest
  2. import sys
  3. import os
  4. import sys
  5. import time
  6. import json
  7. import urllib
  8. import urllib2
  9. import subprocess
  10. import signal
  11. import random
  12. sys.path.append("interface/HUTN")
  13. sys.path.append("scripts")
  14. from hutn_compiler.compiler import main as do_compile
  15. from check_objects import to_recompile
  16. username = "test_user"
  17. parallel_push = True
  18. ports = []
  19. def getFreePort():
  20. while 1:
  21. port = random.randint(10000, 20000)
  22. ports.append(port)
  23. exists = False
  24. for p in ports:
  25. if p == port:
  26. if not exists:
  27. # We have hopefully found our own
  28. exists = True
  29. else:
  30. # We seem to be the second entry, so chose another one
  31. ports.remove(port)
  32. break
  33. else:
  34. # Didn't find a duplicate
  35. return port
  36. def execute(scriptname, parameters=[], wait=False):
  37. if os.name not in ["nt", "posix"]:
  38. # Stop now, as we would have no clue on how to kill its subtree
  39. raise Exception("Unknown OS version: " + str(os.name))
  40. command = [sys.executable, "scripts/%s.py" % scriptname] + parameters
  41. if wait:
  42. return subprocess.call(command, shell=False)
  43. else:
  44. return subprocess.Popen(command, shell=False)
  45. def kill(process):
  46. if os.name == "nt":
  47. subprocess.call(["taskkill", "/F", "/T", "/PID", "%i" % process.pid])
  48. elif os.name == "posix":
  49. subprocess.call(["pkill", "-P", "%i" % process.pid])
  50. def flush_data(address, data):
  51. if data:
  52. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": username})), timeout=10).read()
  53. return []
  54. def compile_file(address, mod_filename, filename, mode, proc):
  55. # Load in the file required
  56. try:
  57. timeout_val = 240
  58. import random
  59. username = str(random.random())
  60. while 1:
  61. proc2 = execute("compile", [address, mod_filename, username, filename, mode], wait=False)
  62. if proc.returncode is not None:
  63. # Modelverse has already terminated, which isn't a good sign!
  64. raise Exception("Modelverse died!")
  65. while proc2.returncode is None:
  66. time.sleep(0.01)
  67. proc2.poll()
  68. timeout_val -= 0.01
  69. if timeout_val < 0:
  70. kill(proc2)
  71. print("Compilation timeout expired!")
  72. return False
  73. if proc2.returncode != 2:
  74. break
  75. # Make sure everything stopped correctly
  76. assert proc2.returncode == 0
  77. if proc2.returncode != 0:
  78. return False
  79. except:
  80. raise
  81. finally:
  82. try:
  83. kill(proc2)
  84. except UnboundLocalError:
  85. pass
  86. def run_file(files, parameters, expected, mode):
  87. # Resolve file
  88. import os.path
  89. time.sleep(0.01)
  90. port = getFreePort()
  91. address = "http://127.0.0.1:%i" % port
  92. try:
  93. # Run Modelverse server
  94. proc = execute("run_local_modelverse", [str(port)], wait=False)
  95. threads = []
  96. mod_files = []
  97. for filename in files:
  98. if os.path.isfile("integration/code/%s" % filename):
  99. mod_filename = "integration/code/%s" % filename
  100. elif os.path.isfile("bootstrap/%s" % filename):
  101. mod_filename = "bootstrap/%s" % filename
  102. else:
  103. raise Exception("File not found: %s" % filename)
  104. mod_files.append(mod_filename)
  105. to_compile = to_recompile(address, mod_files)
  106. for mod_filename in to_compile:
  107. if parallel_push:
  108. import threading
  109. threads.append(threading.Thread(target=compile_file, args=[address, mod_filename, mod_filename, mode, proc]))
  110. threads[-1].start()
  111. else:
  112. compile_file(address, mod_filename, mod_filename, mode, proc)
  113. if parallel_push:
  114. for t in threads:
  115. t.join()
  116. if mode[-1] == "O":
  117. # Fire up the linker
  118. val = execute("link_and_load", [address, username] + mod_files, wait=True)
  119. if val != 0:
  120. raise Exception("Linking error")
  121. # Send the request ...
  122. flush_data(address, parameters)
  123. # ... and wait for replies
  124. for e in expected:
  125. c = len(e) if isinstance(e, set) else 1
  126. for _ in range(c):
  127. val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username})), timeout=240).read()
  128. val = json.loads(val)
  129. if proc.returncode is not None:
  130. # Modelverse has already terminated, which isn't a good sign!
  131. raise Exception("Modelverse died!")
  132. print("Got %s, expect %s" % (val, e))
  133. if isinstance(e, set):
  134. assert val in e
  135. if val not in e:
  136. return False
  137. else:
  138. assert val == e
  139. if val != e:
  140. return False
  141. # All passed!
  142. return True
  143. except:
  144. raise
  145. finally:
  146. try:
  147. kill(proc)
  148. except UnboundLocalError:
  149. pass
  150. def run_barebone(parameters, expected, interface="0", timeout=False, wait=False, link=None, inputs=[]):
  151. port = getFreePort()
  152. address = "http://127.0.0.1:%i" % port
  153. try:
  154. # Run Modelverse server
  155. proc = execute("run_local_modelverse", [str(port)], wait=False)
  156. # Create user and set interface
  157. timeout_val = 15
  158. start = time.time()
  159. while 1:
  160. proc.poll()
  161. if proc.returncode is not None:
  162. # Modelverse has already terminated, which isn't a good sign!
  163. return False
  164. try:
  165. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % username, "username": "user_manager"})), timeout=1).read()
  166. if interface is not None:
  167. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": interface, "username": username})), timeout=1).read()
  168. break
  169. except:
  170. time.sleep(0.01)
  171. if time.time() - start > timeout_val:
  172. raise
  173. # Send the request
  174. flush_data(address, parameters)
  175. # Now do linking and loading
  176. if link is not None:
  177. # Execute linker
  178. timeout_val = 10
  179. proc2 = execute("link_and_load", [address, username] + link, wait=False)
  180. while proc2.returncode is None:
  181. time.sleep(0.01)
  182. proc2.poll()
  183. timeout_val -= 0.01
  184. if timeout_val < 0:
  185. kill(proc2)
  186. print("Linking timeout expired!")
  187. return False
  188. if proc.returncode is not None:
  189. # Modelverse has already terminated, which isn't a good sign!
  190. return False
  191. for inp in inputs:
  192. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "element_type": "V", "value": inp, "username": username})), timeout=1).read()
  193. proc.poll()
  194. if proc.returncode is not None:
  195. # Modelverse has already terminated, which isn't a good sign!
  196. return False
  197. counter = 0
  198. for e in expected:
  199. print("Expect " + str(e))
  200. c = len(e) if isinstance(e, set) else 1
  201. for _ in range(c):
  202. try:
  203. proc.poll()
  204. if proc.returncode is not None:
  205. # Modelverse has already terminated, which isn't a good sign!
  206. return False
  207. val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username})), timeout=240 if not timeout else 20).read()
  208. val = json.loads(val)
  209. except:
  210. if timeout:
  211. return True
  212. else:
  213. raise
  214. print("Got %s, expect %s" % (val, e))
  215. if isinstance(e, set):
  216. assert val in e
  217. if val not in e:
  218. return False
  219. else:
  220. assert val == e
  221. if val != e:
  222. return False
  223. # All passed!
  224. return not timeout
  225. finally:
  226. kill(proc)
  227. def get_constructor(code):
  228. with open("__constraint.al", "w") as f:
  229. f.write(code)
  230. f.flush()
  231. constructors = do_compile("__constraint.al", "interface/HUTN/grammars/actionlanguage.g", "CS")
  232. return constructors