From 100e088b0764faaa76f4a9551bd06f0221fcabc9 Mon Sep 17 00:00:00 2001 From: csimong Date: Fri, 20 Sep 2024 18:26:42 +0200 Subject: [PATCH] Introduce response from server and execute sendAPI function before printing success message --- installer/api.py | 5 ++++- installer/main.py | 14 ++++++-------- xmipp | 11 ++++++++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/installer/api.py b/installer/api.py index 07fa6e6fe..2aaacf521 100644 --- a/installer/api.py +++ b/installer/api.py @@ -59,11 +59,14 @@ def sendApiPOST(retCode: int=0): url = API_URL.split("/", maxsplit=1) path = f"/{url[1]}" url = url[0] - conn = http.client.HTTPSConnection(url, context=ssl._create_unverified_context()) # Unverified context because url does not have an ssl certificate + conn = http.client.HTTPSConnection(url, timeout=2, context=ssl._create_unverified_context()) # Unverified context because url does not have an ssl certificate # Send the POST request conn.request("POST", path, params, headers) + # Get response from server + conn.getresponse() + # Close the connection conn.close() diff --git a/installer/main.py b/installer/main.py index 5c3bf7099..7e84a1ed3 100644 --- a/installer/main.py +++ b/installer/main.py @@ -54,24 +54,19 @@ def getSources(branch: str=None): logger(f"Cloning {source}...", forceConsoleOutput=True) retCode, output = __cloneSourceRepo(REPOSITORIES[source][0], path=SOURCES_PATH, branch=branch) message = output if retCode else '' - handleRetCode(retCode, predefinedErrorCode=SOURCE_CLONE_ERROR, message=message) + handleRetCode(retCode, predefinedErrorCode=SOURCE_CLONE_ERROR, message=message, sendAPI=False) -def exitXmipp(retCode: int=0, sendAPI: bool=True): +def exitXmipp(retCode: int=0): """ ### This function exits Xmipp with the given return code, processing it as a success or an error. #### Params: - retCode (int): Optional. Error code. - - sendAPI (bool): Optional. If True, API message will be sent. """ - # Send API message - if sendAPI and os.path.exists(VERSION_FILE) and retCode != INTERRUPTED_ERROR: - sendApiPOST(retCode=retCode) - # End execution sys.exit(retCode) -def handleRetCode(realRetCode: int, predefinedErrorCode: int=0, message: str=''): +def handleRetCode(realRetCode: int, predefinedErrorCode: int=0, message: str='', sendAPI: bool=True): """ ### This function checks the given return code and handles the appropiate actions. @@ -79,11 +74,14 @@ def handleRetCode(realRetCode: int, predefinedErrorCode: int=0, message: str='') - realRetCode (int): Real return code of the called function. - predefinedErrorCode (int): Optional. Predefined error code for the caller code block in case of error. - message (str): Optional. Message that will be displayed if there is an error th + - sendAPI (bool): Optional. If True, API message will be sent. """ if realRetCode: resultCode = __getPredefinedError(realRetCode=realRetCode, desiredRetCode=predefinedErrorCode) message = message if resultCode != realRetCode else '' logger.logError(message, retCode=resultCode, addPortalLink=resultCode != realRetCode) + if sendAPI and os.path.exists(VERSION_FILE) and resultCode != INTERRUPTED_ERROR: + sendApiPOST(resultCode) exitXmipp(retCode=resultCode) else: if message: diff --git a/xmipp b/xmipp index 7129fac2b..5018333ed 100755 --- a/xmipp +++ b/xmipp @@ -34,13 +34,14 @@ from installer.constants import (MODE_ALL, MODE_COMPILE_AND_INSTALL, MODE_CONFIG CMAKE_CONFIGURE_ERROR, CMAKE_COMPILE_ERROR, CMAKE_INSTALL_ERROR, PARAM_LOGIN, PARAM_SHORT, PARAM_JOBS, PARAM_BRANCH, PARAM_GIT_COMMAND, PARAM_MODEL_PATH, PARAM_MODELS_DIRECTORY, PARAM_KEEP_OUTPUT, PARAM_SHOW_TESTS, PARAM_TEST_NAME, PARAM_UPDATE, PARAM_OVERWRITE, BUILD_PATH, INSTALL_PATH, - BUILD_TYPE, SOURCES_PATH, XMIPP_SOURCES, XMIPP, LOG_FILE, CMAKE_ERROR, MODE_GET_SOURCES) + BUILD_TYPE, SOURCES_PATH, XMIPP_SOURCES, XMIPP, LOG_FILE, CMAKE_ERROR, MODE_GET_SOURCES, VERSION_FILE) from installer.utils import runStreamingJob, runJob from installer.parser import ModeHelpFormatter, GeneralHelpFormatter, ErrorHandlerArgumentParser, getParamNames from installer.config import readConfig, writeConfig from installer.cmake import getCMake, getCMakeVarsStr from installer.main import getSources, exitXmipp, handleRetCode, getSectionMessage, getSuccessMessage, getVersionMessage from installer.logger import logger, yellow, red, blue +from installer.api import sendApiPOST ####################### EXECUTION MODES ####################### def __getProjectRootDir() -> str: @@ -294,12 +295,16 @@ def runSelectedMode(parser: ErrorHandlerArgumentParser, args: argparse.Namespace else: # If method was none of the above, exit with error logger(red(f"Mode \"{args.mode}\" not recognized. {COMMON_USAGE_HELP_MESSAGE}"), forceConsoleOutput=True) - exitXmipp(retCode=1, sendAPI=False) + exitXmipp(retCode=1) + # Send API message + if sendAPI and os.path.exists(VERSION_FILE): + sendApiPOST() + # Print success message for specific modes if args.mode == MODE_ALL or args.mode == MODE_COMPILE_AND_INSTALL: logger(getSuccessMessage(), forceConsoleOutput=True) - exitXmipp(sendAPI=sendAPI) + exitXmipp() ####################### MAIN EXECUTION THREAD ####################### if __name__ == "__main__":