utils.py 9.9 KB

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