updateWandbox.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. import json
  3. import os
  4. import urllib.request
  5. import urllib.parse
  6. from scriptCommon import catchPath
  7. def upload(options):
  8. # request_blah = urllib.request.Request('https://
  9. request = urllib.request.Request('https://melpon.org/wandbox/api/compile.json', method='POST')
  10. json_bytes = json.dumps(options).encode('utf-8')
  11. request.add_header('Content-Type', 'application/json; charset=utf-8')
  12. request.add_header('Content-Length', len(json_bytes))
  13. response = urllib.request.urlopen(request, json_bytes)
  14. return json.loads(response.read().decode('utf-8'))
  15. main_file = '''
  16. #define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
  17. #include "catch.hpp"
  18. unsigned int Factorial( unsigned int number ) {
  19. return number <= 1 ? number : Factorial(number-1)*number;
  20. }
  21. TEST_CASE( "Factorials are computed", "[factorial]" ) {
  22. REQUIRE( Factorial(1) == 1 );
  23. REQUIRE( Factorial(2) == 2 );
  24. REQUIRE( Factorial(3) == 6 );
  25. REQUIRE( Factorial(10) == 3628800 );
  26. }
  27. '''
  28. def uploadFiles():
  29. response = upload({
  30. 'compiler': 'gcc-head',
  31. 'code': main_file,
  32. 'codes': [{
  33. 'file': 'catch.hpp',
  34. 'code': open(os.path.join(catchPath, 'single_include', 'catch2', 'catch.hpp')).read()
  35. }],
  36. 'options': 'c++11,cpp-no-pedantic,boost-nothing',
  37. 'compiler-option-raw': '-DCATCH_CONFIG_FAST_COMPILE',
  38. 'save': True
  39. })
  40. if 'url' in response and 'compiler_error' not in response:
  41. return True, response['url']
  42. else:
  43. return False, response