constructors_object_visitor.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. simple_filename = self.real_file.rsplit("/")[-1]
  29. if v == "None":
  30. # Not defined, so recompile
  31. print("[COMPILE] %s" % simple_filename)
  32. else:
  33. # Is defined already, so let's compare hashes
  34. if v != self.hash_file:
  35. print("[COMPILE] %s" % simple_filename)
  36. else:
  37. print("[CACHED] %s" % simple_filename)
  38. raise CachedException()
  39. def dump(self):
  40. v = ConstructorsVisitor.dump(self)
  41. def flush_data(data):
  42. if data:
  43. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username})), timeout=timeout).read()
  44. return []
  45. # Set up interface
  46. flush_data([3, "upload", self.obj_file, self.hash_file, True])
  47. flush_data(v)
  48. # Upload symbol table
  49. data = []
  50. for e, v in self.object_symbols.iteritems():
  51. data.extend([True, e, v])
  52. # Finish the symbol table
  53. data.append(False)
  54. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  55. # Wait for kernel to signal that it finished
  56. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '2', "username": self.username}))).read()
  57. v = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  58. v = json.loads(v)
  59. if v == "DONE":
  60. return True
  61. else:
  62. return False
  63. def visit_definition(self, tree):
  64. for a in tree.get_children("ID"):
  65. name = a.get_tail()[0]
  66. self.object_symbols[name] = True
  67. return ConstructorsVisitor.visit_definition(self, tree)
  68. def visit_vardecl(self, tree):
  69. if len(tree.get_tail()) > 2:
  70. for a in tree.get_children("ID"):
  71. name = a.get_tail()[0]
  72. self.object_symbols.setdefault(name, False)
  73. return ConstructorsVisitor.visit_vardecl(self, tree)
  74. else:
  75. return ConstructorsVisitor.visit_vardecl(self, tree)
  76. def visit_funcdecl(self, tree):
  77. for a in tree.get_children("func_name"):
  78. for b in a.get_children("ID"):
  79. name = b.get_tail()[0]
  80. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  81. self.object_symbols[name] = True
  82. else:
  83. self.object_symbols.setdefault(name, False)
  84. return ConstructorsVisitor.visit_funcdecl(self, tree)