constructors_object_visitor.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. # Create user
  21. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '"%s"' % self.username, "username": "user_manager"}))).read()
  22. simple_filename = self.real_file.rsplit("/")[-1]
  23. print("[COMPILE] %s" % simple_filename)
  24. def dump(self):
  25. v = ConstructorsVisitor.dump(self)
  26. def flush_data(data):
  27. if data:
  28. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username})), timeout=timeout).read()
  29. return []
  30. # Set up interface
  31. flush_data([3, "upload", self.obj_file, self.hash_file, True])
  32. flush_data(v)
  33. # Upload symbol table
  34. data = []
  35. for e, v in self.object_symbols.iteritems():
  36. data.extend([True, e, v])
  37. # Finish the symbol table
  38. data.append(False)
  39. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  40. # Wait for kernel to signal that it finished
  41. urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "set_input", "value": '2', "username": self.username}))).read()
  42. v = urllib2.urlopen(urllib2.Request(self.address, urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  43. v = json.loads(v)
  44. if v == "DONE":
  45. return True
  46. else:
  47. return False
  48. def visit_definition(self, tree):
  49. for a in tree.get_children("ID"):
  50. name = a.get_tail()[0]
  51. self.object_symbols[name] = True
  52. return ConstructorsVisitor.visit_definition(self, tree)
  53. def visit_vardecl(self, tree):
  54. if len(tree.get_tail()) > 2:
  55. for a in tree.get_children("ID"):
  56. name = a.get_tail()[0]
  57. self.object_symbols.setdefault(name, False)
  58. return ConstructorsVisitor.visit_vardecl(self, tree)
  59. else:
  60. return ConstructorsVisitor.visit_vardecl(self, tree)
  61. def visit_funcdecl(self, tree):
  62. for a in tree.get_children("func_name"):
  63. for b in a.get_children("ID"):
  64. name = b.get_tail()[0]
  65. if tree.get_children("func_body") or tree.get_children("ASSIGN"):
  66. self.object_symbols[name] = True
  67. else:
  68. self.object_symbols.setdefault(name, False)
  69. return ConstructorsVisitor.visit_funcdecl(self, tree)