Salome HOME
Rollback previous integration
[modules/kernel.git] / bin / killSalome.py
1 #!/usr/bin/env python
2 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 #  This library is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU Lesser General Public
9 #  License as published by the Free Software Foundation; either
10 #  version 2.1 of the License.
11 #
12 #  This library is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public
18 #  License along with this library; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 ## \file killSalome.py
24 #  Stop all %SALOME servers from all sessions by killing them
25 #
26
27 import os, sys, re, signal
28
29 from killSalomeWithPort import killMyPort, getPiDict
30
31 def killAllPorts():
32     """
33     Kill all SALOME sessions belonging to the user.
34     """
35     user = os.getenv('USER')
36     # new-style dot-prefixed pidict file
37     #fpidict   = getPiDict('(\d*)',hidden=True)
38     #problem with WIN32 path slashes
39     fpidict   = getPiDict('#####',hidden=True)
40     dirpidict = os.path.dirname(fpidict)
41     fpidict   = os.path.basename(fpidict)
42     fpidict = fpidict.replace('#####', '(\d*)')
43     fnamere   = re.compile("^%s$" % fpidict)
44     try:
45         for f in os.listdir(dirpidict):
46             mo = fnamere.match(f)
47             try:
48                 killMyPort(mo.group(1))
49             except:
50                 pass
51             pass
52         pass
53     except:
54         pass
55     # provide compatibility with old-style pidict file (not dot-prefixed)
56     #fpidict   = getPiDict('(\d*)',hidden=False)
57     fpidict   = getPiDict('#####',hidden=True)
58     dirpidict = os.path.dirname(fpidict)
59     fpidict   = os.path.basename(fpidict)
60     fpidict = fpidict.replace('#####', '(\d*)')
61     fnamere   = re.compile("^%s$" % fpidict)
62     try:
63         for f in os.listdir(dirpidict):
64             mo = fnamere.match(f)
65             try:
66                 killMyPort(mo.group(1))
67             except:
68                 pass
69             pass
70         pass
71     except:
72         pass
73     # kill other processes
74     if sys.platform != 'win32':
75         import commands
76         cmd = "ps -fea | grep '%s' | grep 'ghs3d' | grep 'f /tmp/GHS3D_' | grep -v 'grep' | awk '{print $2}'" % user
77         prc = commands.getoutput(cmd)
78         for field in prc.split():
79             try:
80                 os.kill(int(field), signal.SIGKILL)
81             except:
82                 pass
83             pass
84         pass
85     pass
86
87 if __name__ == "__main__":
88     killAllPorts()
89     pass
90