Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

57 lines
1.6KB

  1. import os
  2. import exec_helpers
  3. from flask import request, url_for
  4. from flask_api import FlaskAPI, status, exceptions
  5. from flask_cors import CORS
  6. app = FlaskAPI(__name__)
  7. CORS(app)
  8. @app.route("/compile", methods=['POST'])
  9. def compile():
  10. if request.method == 'POST':
  11. note = str(request.data.get('source', ''))
  12. flags = str(request.data.get('flags', ''))
  13. with open("/tmp/test.cpp", "w") as f:
  14. f.write(note)
  15. f.close()
  16. stdout = ''
  17. stderr = ''
  18. retcode = 0
  19. output = ''
  20. cmd = "/opt/cheerp/bin/clang {} /tmp/test.cpp -o /tmp/test.js".format(flags)
  21. with exec_helpers.Subprocess() as executor:
  22. try:
  23. ret = executor.check_call(cmd, shell=True, timeout=2)
  24. if ret:
  25. retcode = ret.exit_code
  26. output = ret.stdout_str
  27. except exec_helpers.ExecHelperTimeoutError as e:
  28. #stdout = e.stdout
  29. #stderr = e.stderr
  30. stderr = str(e)
  31. except exec_helpers.CalledProcessError as e:
  32. stdout = e.stdout
  33. stderr = e.stderr
  34. if os.path.exists("/tmp/test.js"):
  35. with open("/tmp/test.js", "r") as f:
  36. output = f.read()
  37. f.close()
  38. return {
  39. 'stdout': stdout,
  40. 'stderr': stderr,
  41. 'command': cmd,
  42. 'retcode': retcode,
  43. 'javascript': output
  44. }, status.HTTP_201_CREATED
  45. if __name__ == "__main__":
  46. app.run(host="0.0.0.0", debug=True)