From f80b46b6645b5f9031a5d3946295cb91498046ae Mon Sep 17 00:00:00 2001 From: nilupulmanodya Date: Thu, 16 Nov 2023 02:11:53 +0530 Subject: [PATCH] Using 'with' statement to manage subprocess --- mslib/mscolab/mscolab.py | 46 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/mslib/mscolab/mscolab.py b/mslib/mscolab/mscolab.py index 83c724fe4..f39c69aad 100644 --- a/mslib/mscolab/mscolab.py +++ b/mslib/mscolab/mscolab.py @@ -275,29 +275,29 @@ def handle_mscolab_metadata_init(repo_exists): try: command = ["python", os.path.join("mslib", "mscolab", "mscolab.py"), "start"] if repo_exists else ["mscolab", "start"] - process = subprocess.Popen(command) - - # maximum retires for connecting mscolab - max_retries = 5 - # retry interval if retry failed - retry_interval = 1 - - for _ in range(max_retries): - try: - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: - sock.connect(('localhost', 8083)) - cmd_curl = ["curl", "http://localhost:8083/metadata/localhost_test_idp", - "-o", os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "metadata_sp.xml")] - subprocess.run(cmd_curl, check=True) - process.terminate() - logging.info('mscolab metadata file generated succesfully') - return True - except socket.error: - time.sleep(retry_interval) - - if _ == max_retries: - print(f'mscolab server failed to start after {max_retries} retries') - return False + with subprocess.Popen(command) as process: + # maximum retires for connecting mscolab + max_retries = 5 + # retry interval if retry failed + retry_interval = 1 + + for _ in range(max_retries): + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.connect(('localhost', 8083)) + cmd_curl = ["curl", "http://localhost:8083/metadata/localhost_test_idp", + "-o", os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "metadata_sp.xml")] + subprocess.run(cmd_curl, check=True) + process.terminate() + logging.info('mscolab metadata file generated succesfully') + return True + except socket.error: + time.sleep(retry_interval) + + if _ == max_retries: + print(f'error while generating metadata,' + f'mscolab server failed to start after {max_retries} retries') + return False except subprocess.CalledProcessError as error: print(f"Error while generating metadata file for the mscolab server: {error}")