def application_helper(environ: Mapping) -> Mapping: method = environ.get('REQUEST_METHOD') path = environ.get('PATH_INFO') # info page if method == 'GET' and path == '/env': return create_data_response(environ) # entry point check if method != 'POST' or path != '/run': return create_error_response('Bad Request: Wrong Methods.') # get request content request_body = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])) request_json_object = json.loads(request_body) if REQUEST_VERSION_NAME not in request_json_object or request_json_object[ REQUEST_VERSION_NAME] != VERSION: return create_error_response( 'The current version of your local server (%s) does not match version of the web ' 'app ("%s"). Please download the newest version at ' 'https://github.com/FlickerSoul/Graphery/releases.' % (VERSION, request_json_object.get(REQUEST_VERSION_NAME, 'Not Exist'))) if REQUEST_CODE_NAME not in request_json_object: return create_error_response( 'No Code Snippets Embedded In The Request.') if REQUEST_GRAPH_NAME not in request_json_object: return create_error_response('No Graph Intel Embedded In The Request.') # execute program with timed out return time_out_execute(code=request_json_object[REQUEST_CODE_NAME], graph_json=request_json_object[REQUEST_GRAPH_NAME])
def time_out_execute(*args, **kwargs) -> Mapping: with Pool(processes=1) as pool: try: result = pool.apply_async(func=execute, args=args, kwds=kwargs) code_hash, exec_result = result.get(timeout=TIMEOUT_SECONDS) response_dict = create_data_response({ 'codeHash': code_hash, 'execResult': exec_result }) except TimeoutError: response_dict = create_error_response( f'Timeout: Code running timed out after {TIMEOUT_SECONDS}s.') except ExecutionException as e: if e.empty: response_dict = create_error_response( f'Unknown Exception: {e}') else: exec_info = e.related_exec_info[-1] response_dict = create_error_response(f'{e}\n' + 'At line {}: `{}`\n' 'in {}'.format( *exec_info)) except ExecutionServerException as e: response_dict = create_error_response(f'Server Exception: {e}') except Exception as e: response_dict = create_error_response(f'Unknown Exception: {e}.') print('Execution done.') return response_dict
def time_out_execute(*args, **kwargs): with Pool(processes=1) as pool: try: result = pool.apply_async(func=execute, args=args, kwds=kwargs) code_hash, exec_result = result.get(timeout=TIMEOUT_SECONDS) response_dict = create_data_response({ 'codeHash': code_hash, 'execResult': exec_result }) except TimeoutError: response_dict = create_error_response( f'Timeout: Code running timed out after {TIMEOUT_SECONDS} s.') except ExecutionException as e: # TODO try to give detailed exception location feedback response_dict = create_error_response(f'Exception: {e}.') except Exception as e: response_dict = create_error_response(f'Unknown Exception: {e}.') print('Execution done.') return response_dict
def test_create_data_response(data_obj, data): assert create_data_response(data_obj) == data
@tracer('edge') def main() -> None: for edge in graph_object.V: print(edge) ''') @pytest.mark.parametrize( 'env, response', [ pytest.param( Env(REQUEST_METHOD='GET', PATH_INFO='/env').content, create_data_response( Env(REQUEST_METHOD='GET', PATH_INFO='/env').content)), # info page pytest.param( Env(REQUEST_METHOD='GET', PATH_INFO='/venv').content, create_error_response( 'Bad Request: Wrong Methods.')), # wrong request method pytest.param( Env(REQUEST_METHOD='POST', PATH_INFO='/env').content, create_error_response( 'Bad Request: Wrong Methods.')), # wrong entry point pytest.param( Env(REQUEST_METHOD='POST', PATH_INFO='/run', CONTENT_LENGTH='1').add_content({ 'wsgi.input': FileLikeObj(json.dumps({})) }).content,