From 74030adde1d337d9616de9580934276480efc5be Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Tue, 1 Mar 2022 09:44:08 +0100 Subject: [PATCH] Avoid to kill all sessions when shutting down only one --- bin/killSalomeWithPort.py | 75 ++++++++++++++++++++------------------- bin/runSalome.py | 22 ++---------- 2 files changed, 41 insertions(+), 56 deletions(-) diff --git a/bin/killSalomeWithPort.py b/bin/killSalomeWithPort.py index 04e6939f6..7e9c52204 100755 --- a/bin/killSalomeWithPort.py +++ b/bin/killSalomeWithPort.py @@ -125,8 +125,7 @@ def appliCleanOmniOrbConfig(port): # outside application context return - if verbose(): - print("Cleaning OmniOrb config for port {}".format(port)) + logging.getLogger().debug("Cleaning OmniOrb config for port {}".format(port)) omniorb_config = generateFileName(omniorb_user_path, prefix='omniORB', @@ -141,9 +140,10 @@ def appliCleanOmniOrbConfig(port): extension='cfg', hidden=True, with_username=True) - - if os.access(last_running_config, os.F_OK): - if sys.platform == 'win32' or osp.samefile(last_running_config, omniorb_config): + logging.getLogger().debug("Omniorb_config file deduced by port : {}".format(omniorb_config)) + logging.getLogger().debug("Omniorb_config file of last : {}".format(last_running_config)) + if osp.exists(last_running_config): + if sys.platform == 'win32' or ( osp.exists(omniorb_config) and osp.samefile(last_running_config,omniorb_config) ): os.remove(last_running_config) if os.access(omniorb_config, os.F_OK): @@ -252,8 +252,7 @@ def __killPids(pids): logger.debug("Add process with PID = {} into PIDList to kill".format(pid)) processes.append(psutil.Process(pid)) except psutil.NoSuchProcess: - if verbose(): - print(" ------------------ Process {} not found".format(pid)) + logger.debug(" ------------------ Process {} not found".format(pid)) __killProcesses(processes) def __killMyPort(port, filedict): @@ -305,37 +304,39 @@ def __guessPiDictFilename(port): return None -def killMyPortSSL(*ports): - """ Called by runSalome.py after CTRL-C""" - for port in ports: - # ensure port is an integer - with suppress(ValueError): - port = int(port) - - with suppress(Exception): - # DO NOT REMOVE NEXT LINE: it tests PortManager availability! - from PortManager import releasePort - # get pidict file - filedict = getPiDict(port) - if not osp.isfile(filedict): # removed by previous call, see (1) above - if verbose(): - print("SALOME session on port {} is already stopped".format(port)) - # remove port from PortManager config file - with suppress(ImportError): - if verbose(): - print("Removing port from PortManager configuration file") - releasePort(port) - return - try: - # DO NOT REMOVE NEXT LINE: it tests PortManager availability! - import PortManager # pragma pylint: disable=unused-import - for file_path in glob('{}*'.format(getPiDict(port))): - __killMyPort(port, file_path) - except ImportError: - __killMyPort(port, __guessPiDictFilename(port)) +def killProcessSSL(port, pids_list): + """ Called by runSalome.py after CTRL-C. + This method : + - Kill all PIDS in pids + - update file of pidict + """ + __killPids(pids_list) - # clear-up omniOrb config files - appliCleanOmniOrbConfig(port) + with suppress(ValueError): + port = int(port) + + for filedict in glob('{}*'.format(getPiDict(port))): + with suppress(Exception), open(filedict, 'rb') as fpid: + logging.getLogger().debug("Removing following PIDS from file \"{}\" : {}" + .format(filedict,pids_list)) + pids_lists_in_file = pickle.load(fpid) + for dico_of_pids in pids_lists_in_file: + for pid in pids_list: + if pid in dico_of_pids: + del dico_of_pids[pid] + pids_lists_in_file = [elt for elt in pids_lists_in_file if len(elt)>0] + if len(pids_lists_in_file) == 0: + try: + logging.getLogger().debug("List of PIDS to Kill is now empty -> Remove file \"{}\"".format(filedict)) + os.remove(filedict) + except: + pass + continue + with suppress(Exception), open(filedict, 'wb') as fpid: + logging.getLogger().debug("Writing back into file \"{}\"".format(filedict)) + pickle.dump(pids_lists_in_file,fpid) + # clear-up omniOrb config files + appliCleanOmniOrbConfig(port) def killMyPort(*ports): """ diff --git a/bin/runSalome.py b/bin/runSalome.py index 422631b68..fc4c9afc7 100755 --- a/bin/runSalome.py +++ b/bin/runSalome.py @@ -171,14 +171,6 @@ def execScript(script_path): # ----------------------------------------------------------------------------- -def addToPidict(args): - global process_id - from addToKillList import addToKillList - for pid, cmd in list(process_id.items()): - addToKillList(pid, cmd) - -# ----------------------------------------------------------------------------- - def main(exeName=None): """Salome launch as a main application""" keep_env = not os.getenv('SALOME_PLEASE_SETUP_ENVIRONMENT_AS_BEFORE') @@ -279,14 +271,6 @@ def foreGround(args, ior_fakens_filename): from salome_utils import getPortNumber port = getPortNumber() # -- - server = Server({}) - if sys.platform == "win32": - server.CMD = [os.getenv("PYTHONBIN"), "-m", "killSalomeWithPort", "--spy", "%s"%(session_pid or os.getpid()), "%s"%(port)] - else: - server.CMD = ["killSalomeWithPort.py", "--spy", "%s"%(session_pid or os.getpid()), "%s"%(port)] - server.run(True) - # os.system("killSalomeWithPort.py --spy %s %s &"%(os.getpid(), port)) - # -- dt = 1.0 try: while 1: @@ -302,9 +286,9 @@ def foreGround(args, ior_fakens_filename): pass except KeyboardInterrupt: logger.debug("Keyboard requested : killing all process attached to port {}".format(port)) - from killSalomeWithPort import killMyPortSSL - killMyPortSSL(port) - pass + finally: + from killSalomeWithPort import killProcessSSL + killProcessSSL(port,[session_pid]) return # -- 2.39.2