| 907 | |
| 908 | |
| 909 | def executePythonFile(tree, runType="COMMAND_LINE", eventData=None) -> Tuple[dict, dict]: |
| 910 | global capture |
| 911 | global response |
| 912 | |
| 913 | if tree["type"] == "PYTHON_FILE": |
| 914 | statements = getStatementsFromBlock(tree["childSets"]["body"], True) |
| 915 | |
| 916 | if runType == "COMMAND_LINE" or runType == "SCHEDULE": |
| 917 | pass |
| 918 | elif runType == "HTTP_REQUEST": |
| 919 | # we need to parse our http scenario event into serverless_wsgi so |
| 920 | if not eventData: |
| 921 | raise Exception("Need an event to run a HTTP request") |
| 922 | |
| 923 | extra = ast.parse(f""" |
| 924 | import serverless_wsgi |
| 925 | |
| 926 | flask_app = None |
| 927 | |
| 928 | try: |
| 929 | flask_app = app |
| 930 | except NameError: |
| 931 | print("Please call your Flask app 'app'") |
| 932 | |
| 933 | if flask_app: |
| 934 | {SPLOOT_SET_RESPONSE_FUNC}(serverless_wsgi.handle_request(app, {SPLOOT_HANDLER_ARG}, {{}})) |
| 935 | """) |
| 936 | |
| 937 | statements.extend(extra.body) |
| 938 | else: |
| 939 | raise NotImplementedError("This run type is not implemented: " + runType) |
| 940 | |
| 941 | mods = ast.Module(body=statements, type_ignores=[]) |
| 942 | code = compile(ast.fix_missing_locations(mods), "main.py", mode="exec") |
| 943 | # Uncomment to print generated Python code |
| 944 | # print(ast.unparse(ast.fix_missing_locations(mods))) |
| 945 | # print(ast.dump(mods)) |
| 946 | |
| 947 | capture = SplootCapture() |
| 948 | response = {} |
| 949 | |
| 950 | def set_response(r): |
| 951 | global response |
| 952 | response = r |
| 953 | |
| 954 | try: |
| 955 | exec(code, {SPLOOT_KEY: capture, '__name__': '__main__', SPLOOT_HANDLER_ARG: eventData, SPLOOT_SET_RESPONSE_FUNC: set_response}) |
| 956 | except EOFError as e: |
| 957 | # This is because we don't have inputs in a rerun. |
| 958 | capture.logException(e) |
| 959 | except BaseException as e: |
| 960 | capture.logException(e) |
| 961 | traceback.print_exc() |
| 962 | |
| 963 | return (capture.toDict(), response) |
| 964 | |
| 965 | |
| 966 | def wrapStdout(write): |