constructors_object_visitor.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from constructors_visitor import ConstructorsVisitor
  2. from cached_exception import CachedException
  3. import urllib
  4. import urllib2
  5. import json
  6. timeout = 100
  7. class ConstructorsObjectVisitor(ConstructorsVisitor):
  8. def __init__(self, args):
  9. ConstructorsVisitor.__init__(self, args)
  10. self.username = args[0]
  11. self.obj_file = args[1]
  12. self.real_file = args[2]
  13. self.address = args[3]
  14. self.object_symbols = {}
  15. with open(self.real_file, 'r') as f:
  16. import hashlib
  17. md5 = hashlib.md5()
  18. md5.update(f.read())
  19. self.hash_file = md5.hexdigest()
  20. # Check if file is already compiled (with same hash) in Modelverse
  21. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '"%s"' % self.username, "username": "user_manager"}))).read()
  22. def flush_data(data):
  23. if data:
  24. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username})), timeout=timeout).read()
  25. return []
  26. flush_data([3, "is_defined", self.obj_file])
  27. v = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  28. v = json.loads(v)
  29. simple_filename = self.real_file.rsplit("/")[-1]
  30. if v == "None":
  31. # Not defined, so recompile
  32. print("[COMPILE] %s" % simple_filename)
  33. else:
  34. # Is defined already, so let's compare hashes
  35. if v != self.hash_file:
  36. print("[COMPILE] %s" % simple_filename)
  37. else:
  38. print("[CACHED] %s" % simple_filename)
  39. raise CachedException()
  40. def dump(self):
  41. v = ConstructorsVisitor.dump(self)
  42. def flush_data(data):
  43. if data:
  44. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username})), timeout=timeout).read()
  45. return []
  46. # Set up interface
  47. flush_data([3, "upload", self.obj_file, self.hash_file, True])
  48. flush_data(v)
  49. # Upload symbol table
  50. data = []
  51. for e, v in self.object_symbols.iteritems():
  52. data.extend([True, e, v])
  53. # Finish the symbol table
  54. data.append(False)
  55. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  56. # Wait for kernel to signal that it finished
  57. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '2', "username": self.username}))).read()
  58. v = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  59. v = json.loads(v)
  60. if v == "DONE":
  61. return True
  62. else:
  63. return False
  64. def visit_definition(self, tree):
  65. for a in tree.get_children("ID"):
  66. name = a.get_tail()[0]
  67. self.object_symbols[name] = True
  68. return ConstructorsVisitor.visit_definition(self, tree)
  69. def visit_vardecl(self, tree):
  70. if len(tree.get_tail()) > 2:
  71. for a in tree.get_children("ID"):
  72. name = a.get_tail()[0]
  73. self.object_symbols.setdefault(name, False)
  74. return ConstructorsVisitor.visit_vardecl(self, tree)
  75. else:
  76. return ConstructorsVisitor.visit_vardecl(self, tree)
  77. def visit_funcdecl(self, tree):
  78. for a in tree.get_children("func_name"):
  79. for b in a.get_children("ID"):
  80. name = b.get_tail()[0]
  81. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  82. self.object_symbols[name] = True
  83. else:
  84. self.object_symbols.setdefault(name, False)
  85. return ConstructorsVisitor.visit_funcdecl(self, tree)