Salome HOME
Merge branch 'V9_9_BR'
[modules/kernel.git] / bin / killSalome.py
1 #! /usr/bin/env python3
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 ## @file killSalome.py
26 #  @brief Forcibly stop all running %SALOME sessions.
27
28 """
29 Forcibly stop all running SALOME sessions.
30
31 To stop all SALOME sessions, do the following:
32
33 * in shell:
34
35     $ killSalome.py
36
37 * in Python script:
38
39     from killSalome import killAllPorts
40     killAllPorts()
41 """
42
43 # pragma pylint: disable=invalid-name
44
45 import os
46 import os.path as osp
47 import re
48 import sys
49 from contextlib import suppress
50 from glob import glob
51
52 from killSalomeWithPort import getPiDict, killMyPort, killUnkilledProcesses
53
54 def killAllPorts():
55     """
56     Stop all running SALOME sessions owned by the current user.
57     """
58     # detect ports used by SALOME session and kill processes
59     for hidden in (True, False):
60         pidict_t = getPiDict(port='#####', hidden=hidden)
61         dir_pidict_t = osp.dirname(pidict_t)
62         fn_pidict_t = osp.basename(pidict_t).replace('#####', r'(\d*)')
63         fn_rex = re.compile('^{}'.format(fn_pidict_t))
64         with suppress(IOError):
65             for fname in os.listdir(dir_pidict_t):
66                 m_res = fn_rex.match(fname)
67                 if m_res:
68                     killMyPort(m_res.group(1))
69
70     # kill possibly unkilled processes
71     killUnkilledProcesses()
72
73     # other clean-up actions
74     # - delete uri files needed by ompi-server
75     for path in glob(osp.join(osp.expanduser('~'), '.urifile_*')):
76         with suppress(IOError):
77             os.remove(path)
78
79 def main():
80     """
81     Main function
82     """
83     from argparse import ArgumentParser
84     parser = ArgumentParser(description='Forcibly stop all running SALOME sessions')
85     parser.parse_args()
86
87     try:
88         from salomeContextUtils import setOmniOrbUserPath
89         setOmniOrbUserPath()
90     except Exception as exc: # pragma pylint: disable=broad-except
91         print(exc)
92         sys.exit(1)
93
94     killAllPorts()
95
96 if __name__ == '__main__':
97     main()