primitives_object_visitor.py 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return True
  60. def visit_definition(self, tree):
  61. for a in tree.get_children("ID"):
  62. name = a.get_tail()[0]
  63. self.object_symbols[name] = True
  64. return PrimitivesVisitor.visit_definition(self, tree)
  65. def visit_vardecl(self, tree):
  66. if len(tree.get_tail()) > 2:
  67. for a in tree.get_children("ID"):
  68. name = a.get_tail()[0]
  69. self.object_symbols.setdefault(name, False)
  70. return PrimitivesVisitor.visit_vardecl(self, tree)
  71. else:
  72. return PrimitivesVisitor.visit_vardecl(self, tree)
  73. def visit_funcdecl(self, tree):
  74. for a in tree.get_children("func_name"):
  75. for b in a.get_children("ID"):
  76. name = b.get_tail()[0]
  77. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  78. self.object_symbols[name] = True
  79. else:
  80. self.object_symbols.setdefault(name, False)
  81. return PrimitivesVisitor.visit_funcdecl(self, tree)