linker.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import sys
  2. import urllib
  3. import urllib2
  4. import json
  5. def flush_data(address, data, username):
  6. if data:
  7. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": username})), timeout=10).read()
  8. return []
  9. def link(address, username, objects):
  10. # Read out all symbol tables that are to be linked
  11. definers = {}
  12. users = {}
  13. data = []
  14. data.append(("V", '3'))
  15. data.append(("V", '"check_symbols"'))
  16. for obj in objects:
  17. data.append(("V", '"%s"' % obj))
  18. data.append(("V", '""'))
  19. data = flush_data(address, data, username)
  20. v = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username}))).read()
  21. if "False" in v:
  22. raise Exception("Linking error")
  23. # Ok, we know that all symbols can be defined with this set of files, now link their initializers together
  24. initializers = []
  25. for obj in objects:
  26. data.append(("V", '3'))
  27. data.append(("V", '"read_initializers"'))
  28. data.append(("V", '"%s"' % obj))
  29. data = flush_data(address, data, username)
  30. for obj in objects:
  31. print("[LINK] %s" % (obj))
  32. v = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username}))).read()
  33. start = str(v.split("&", 1)[0].split("=")[1])
  34. initializers.append(start)
  35. # Bind all initializers together
  36. if definers:
  37. print("[LOAD] %s:main()" % definers["main"])
  38. else:
  39. print("[LOAD] main()")
  40. # Set interface to constructors
  41. commands = [("V", '1')]
  42. # Link all initializers together
  43. for init in initializers:
  44. commands.extend([
  45. ("V", '"call"'),
  46. ("V", '"access"'),
  47. ("V", '"resolve"'),
  48. ("V", '"exec"'),
  49. ("V", '1'),
  50. ("V", '"const"'),
  51. ("R", str(init)),
  52. ("V", 'true'),
  53. ])
  54. # Load main function
  55. commands.extend([
  56. ("V", '"return"'),
  57. ("V", 'true'),
  58. ("V", '"call"'),
  59. ("V", '"access"'),
  60. ("V", '"resolve"'),
  61. ("V", '"main"'),
  62. ("V", '0'),
  63. ("V", 'false'),
  64. ])
  65. import json
  66. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(commands), "username": username}))).read()
  67. if __name__ == "__main__":
  68. if len(sys.argv) == 1:
  69. print("No username defined")
  70. else:
  71. address = sys.argv[1]
  72. username = sys.argv[2]
  73. objects = set(sys.argv[3:])
  74. if objects:
  75. link(address, username, objects)
  76. else:
  77. print("No files to link defined")