primitives_object_visitor.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import string
  2. from primitives_visitor import PrimitivesVisitor
  3. import urllib
  4. import urllib2
  5. class PrimitivesObjectVisitor(PrimitivesVisitor):
  6. def __init__(self, args):
  7. PrimitivesVisitor.__init__(self, args)
  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 = PrimitivesVisitor.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": 'false', "username": self.username}))).read() # Use old interface
  49. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": json.dumps(v), "username": self.username}))).read()
  50. # Upload symbol table
  51. data = []
  52. for e, v in self.object_symbols.iteritems():
  53. data.append(["V", "true"])
  54. data.append(["V", '"%s"' % e])
  55. data.append(["V", "true" if v else "false"])
  56. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  57. # Finish the symbol table
  58. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'false', "username": self.username}))).read()
  59. # Wait for kernel to signal that it finished
  60. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '2', "username": self.username}))).read()
  61. v = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  62. v = v.split("=", 2)[2]
  63. if v == "DONE":
  64. return True
  65. else:
  66. return False
  67. def visit_definition(self, tree):
  68. for a in tree.get_children("ID"):
  69. name = a.get_tail()[0]
  70. self.object_symbols[name] = True
  71. return PrimitivesVisitor.visit_definition(self, tree)
  72. def visit_vardecl(self, tree):
  73. if len(tree.get_tail()) > 2:
  74. for a in tree.get_children("ID"):
  75. name = a.get_tail()[0]
  76. self.object_symbols.setdefault(name, False)
  77. return PrimitivesVisitor.visit_vardecl(self, tree)
  78. else:
  79. return PrimitivesVisitor.visit_vardecl(self, tree)
  80. def visit_funcdecl(self, tree):
  81. for a in tree.get_children("func_name"):
  82. for b in a.get_children("ID"):
  83. name = b.get_tail()[0]
  84. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  85. self.object_symbols[name] = True
  86. else:
  87. self.object_symbols.setdefault(name, False)
  88. return PrimitivesVisitor.visit_funcdecl(self, tree)