visitor_objects.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from visitor_mvs import VisitorMvS
  2. import urllib
  3. import urllib2
  4. class VisitorObjects(VisitorMvS):
  5. def __init__(self, args):
  6. self.username = args[0]
  7. self.obj_file = args[1]
  8. self.real_file = args[2]
  9. self.mode = args[3]
  10. VisitorMvS.__init__(self, [None])
  11. with open(self.real_file, 'r') as f:
  12. import hashlib
  13. md5 = hashlib.md5()
  14. md5.update(f.read())
  15. self.hash_file = md5.hexdigest()
  16. # Check if file is already compiled (with same hash) in Modelverse
  17. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.username, "username": "user_manager"}))).read()
  18. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
  19. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"is_defined"', "username": self.username}))).read()
  20. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
  21. v = urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "get_output", "username": self.username}))).read()
  22. v = v.split("=", 2)[2]
  23. if v == "None":
  24. # Not defined, so recompile
  25. print("[CC] %s" % self.real_file)
  26. else:
  27. # Is defined already, so let's compare hashes
  28. if v != self.hash_file:
  29. print("[RC] %s" % self.real_file)
  30. # Remove in Modelverse and recompile
  31. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
  32. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"remove_obj"', "username": self.username}))).read()
  33. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
  34. else:
  35. self.visit = lambda i: i
  36. self.dump = lambda: True
  37. print("[SO] %s" % self.real_file)
  38. def dump(self):
  39. v = VisitorMvS.dump(self)
  40. import json
  41. # Set up interface
  42. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '3', "username": self.username}))).read()
  43. # Start uploading the code
  44. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"upload"', "username": self.username}))).read()
  45. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.obj_file, "username": self.username}))).read()
  46. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": '"%s"' % self.hash_file, "username": self.username}))).read()
  47. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'false', "username": self.username}))).read() # Use old interface
  48. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": json.dumps(v), "username": self.username}))).read()
  49. # Upload symbol table
  50. data = []
  51. for e, v in self.object_symbols.iteritems():
  52. data.append(["V", "true"])
  53. data.append(["V", '"%s"' % e])
  54. data.append(["V", "true" if v else "false"])
  55. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": self.username}))).read()
  56. # Finish the symbol table
  57. urllib2.urlopen(urllib2.Request("http://localhost:8001/", urllib.urlencode({"op": "set_input", "element_type": "V", "value": 'false', "username": self.username}))).read()
  58. return True