MCPcopy Create free account
hub / github.com/SplootCode/splootcode / executePythonFile

Function executePythonFile

python/executor.py:909–963  ·  view source on GitHub ↗
(tree, runType="COMMAND_LINE", eventData=None)

Source from the content-addressed store, hash-verified

907
908
909def 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"""
924import serverless_wsgi
925
926flask_app = None
927
928try:
929 flask_app = app
930except NameError:
931 print("Please call your Flask app 'app'")
932
933if 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
966def wrapStdout(write):

Callers 15

testWhileLoopMethod · 0.90
testForLoopMethod · 0.90
testBreakMethod · 0.90
testHelloWorldMethod · 0.90
testIfElseMethod · 0.90
testElseMethod · 0.90
testListMethod · 0.90
testExpressionParsingMethod · 0.90

Calls 4

logExceptionMethod · 0.95
toDictMethod · 0.95
SplootCaptureClass · 0.85
getStatementsFromBlockFunction · 0.70

Tested by 15

testWhileLoopMethod · 0.72
testForLoopMethod · 0.72
testBreakMethod · 0.72
testHelloWorldMethod · 0.72
testIfElseMethod · 0.72
testElseMethod · 0.72
testListMethod · 0.72
testExpressionParsingMethod · 0.72