constructors_object_visitor.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from constructors_visitor import ConstructorsVisitor
  2. import urllib
  3. import urllib2
  4. timeout = 100
  5. class ConstructorsObjectVisitor(ConstructorsVisitor):
  6. def __init__(self, args):
  7. ConstructorsVisitor.__init__(self)
  8. self.username = args[0]
  9. self.obj_file = args[1]
  10. self.real_file = args[2]
  11. self.object_symbols = {}
  12. with open(self.real_file, 'r') as f:
  13. import hashlib
  14. md5 = hashlib.md5()
  15. md5.update(f.read())
  16. self.hash_file = md5.hexdigest()
  17. # Check if file is already compiled (with same hash) in Modelverse
  18. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.username, "username": "user_manager"}))).read()
  19. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
  20. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"is_defined"', "username": self.username}))).read()
  21. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
  22. v = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  23. v = v.split("=", 2)[2]
  24. if v == "None":
  25. # Not defined, so recompile
  26. print("[COMPILE] %s" % self.real_file.rsplit("/", 1)[1])
  27. else:
  28. # Is defined already, so let's compare hashes
  29. if v != self.hash_file:
  30. print("[COMPILE] %s" % self.real_file.rsplit("/", 1)[1])
  31. # Remove in Modelverse and recompile
  32. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
  33. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"remove_obj"', "username": self.username}))).read()
  34. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
  35. else:
  36. self.visit = lambda i: i
  37. self.dump = lambda: True
  38. print("[CACHED] %s" % self.real_file.rsplit("/", 1)[1])
  39. def dump(self):
  40. v = ConstructorsVisitor.dump(self)
  41. import json
  42. # Set up interface
  43. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
  44. # Start uploading the code
  45. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"upload"', "username": self.username}))).read()
  46. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
  47. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.hash_file, "username": self.username}))).read()
  48. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'true', "username": self.username}))).read() # Use new interface
  49. def flush_data(data):
  50. if data:
  51. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username})), timeout=timeout).read()
  52. return []
  53. var_list = {}
  54. data = []
  55. for p in v:
  56. if isinstance(p, int):
  57. if p not in var_list:
  58. data = flush_data(data)
  59. val = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": self.username})), timeout=timeout).read()
  60. val = val.split("=", 2)[1].split("&", 1)[0]
  61. var_list[p] = val
  62. continue
  63. else:
  64. val = var_list[p]
  65. t = "R"
  66. else:
  67. val = p
  68. t = "V"
  69. data.append([t, val])
  70. data = flush_data(data)
  71. # Upload symbol table
  72. data = []
  73. for e, v in self.object_symbols.iteritems():
  74. data.append(["V", "true"])
  75. data.append(["V", '"%s"' % e])
  76. data.append(["V", "true" if v else "false"])
  77. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  78. # Finish the symbol table
  79. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'false', "username": self.username}))).read()
  80. return True
  81. def visit_definition(self, tree):
  82. for a in tree.get_children("ID"):
  83. name = a.get_tail()[0]
  84. self.object_symbols[name] = True
  85. return ConstructorsVisitor.visit_definition(self, tree)
  86. def visit_vardecl(self, tree):
  87. if len(tree.get_tail()) > 2:
  88. for a in tree.get_children("ID"):
  89. name = a.get_tail()[0]
  90. self.object_symbols.setdefault(name, False)
  91. return ConstructorsVisitor.visit_vardecl(self, tree)
  92. else:
  93. return ConstructorsVisitor.visit_vardecl(self, tree)
  94. def visit_funcdecl(self, tree):
  95. for a in tree.get_children("func_name"):
  96. for b in a.get_children("ID"):
  97. name = b.get_tail()[0]
  98. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  99. self.object_symbols[name] = True
  100. else:
  101. self.object_symbols.setdefault(name, False)
  102. return ConstructorsVisitor.visit_funcdecl(self, tree)