Salome HOME
updated copyright message
[modules/kernel.git] / bin / killSalome.py
index 814420dfa78e18443994eb6a378d3e319e9e5423..ffa63afa0bec8a791575913f31931293ec9d002b 100755 (executable)
@@ -1,40 +1,97 @@
-#!/usr/bin/env python
+#! /usr/bin/env python3
+#  -*- coding: iso-8859-1 -*-
+# Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
+#
+# Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
 
-import sys,os,pickle,signal
+## @file killSalome.py
+#  @brief Forcibly stop all running %SALOME sessions.
 
-process_id={}
+"""
+Forcibly stop all running SALOME sessions.
 
-# -----------------------------------------------------------------------------
-#
-# Fonction d'arrĂȘt de salome
-#
+To stop all SALOME sessions, do the following:
+
+* in shell:
+
+    $ killSalome.py
+
+* in Python script:
+
+    from killSalome import killAllPorts
+    killAllPorts()
+"""
+
+# pragma pylint: disable=invalid-name
+
+import os
+import os.path as osp
+import re
+import sys
+from contextlib import suppress
+from glob import glob
+
+from killSalomeWithPort import getPiDict, killMyPort, killUnkilledProcesses
+
+def killAllPorts():
+    """
+    Stop all running SALOME sessions owned by the current user.
+    """
+    # detect ports used by SALOME session and kill processes
+    for hidden in (True, False):
+        pidict_t = getPiDict(port='#####', hidden=hidden)
+        dir_pidict_t = osp.dirname(pidict_t)
+        fn_pidict_t = osp.basename(pidict_t).replace('#####', r'(\d*)')
+        fn_rex = re.compile('^{}'.format(fn_pidict_t))
+        with suppress(IOError):
+            for fname in os.listdir(dir_pidict_t):
+                m_res = fn_rex.match(fname)
+                if m_res:
+                    killMyPort(m_res.group(1))
+
+    # kill possibly unkilled processes
+    killUnkilledProcesses()
+
+    # other clean-up actions
+    # - delete uri files needed by ompi-server
+    for path in glob(osp.join(osp.expanduser('~'), '.urifile_*')):
+        with suppress(IOError):
+            os.remove(path)
+
+def main():
+    """
+    Main function
+    """
+    from argparse import ArgumentParser
+    parser = ArgumentParser(description='Forcibly stop all running SALOME sessions')
+    parser.parse_args()
 
-def killSalome():
-   print "arret des serveurs SALOME"
-   for pid, cmd in process_id.items():
-      print "arret du process %s : %s"% (pid, cmd[0])
-      try:
-         os.kill(pid,signal.SIGKILL)
-      except:
-         print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
-   print "arret du naming service"
-   os.system("killall -9 omniNames")
-   
-# -----------------------------------------------------------------------------
-
-filedict='/tmp/'+os.getenv('USER')+'_SALOME_pidict'
-#filedict='/tmp/'+os.getlogin()+'_SALOME_pidict'
-try:
-   fpid=open(filedict, 'r')
-except:
-   print "le fichier %s des process SALOME n'est pas accessible"% filedict
-   sys.exit(1)
-   
-process_id=pickle.load(fpid)
-fpid.close()
-
-killSalome()
-
-os.remove(filedict)
+    try:
+        from salomeContextUtils import setOmniOrbUserPath
+        setOmniOrbUserPath()
+    except Exception as exc: # pragma pylint: disable=broad-except
+        print(exc)
+        sys.exit(1)
 
+    killAllPorts()
 
+if __name__ == '__main__':
+    main()