Salome HOME
merge from branch BR_V5_DEV
[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 import os, sys, re, signal
24
25 from killSalomeWithPort import killMyPort, getPiDict
26
27 def killAllPorts():
28     """
29     Kill all SALOME sessions belonging to the user.
30     """
31     user = os.getenv('USER')
32     # new-style dot-prefixed pidict file
33     #fpidict   = getPiDict('(\d*)',hidden=True)
34     #problem with WIN32 path slashes
35     fpidict   = getPiDict('#####',hidden=True)
36     dirpidict = os.path.dirname(fpidict)
37     fpidict   = os.path.basename(fpidict)
38     fpidict = fpidict.replace('#####', '(\d*)')
39     fnamere   = re.compile("^%s$" % fpidict)
40     try:
41         for f in os.listdir(dirpidict):
42             mo = fnamere.match(f)
43             try:
44                 killMyPort(mo.group(1))
45             except:
46                 pass
47             pass
48         pass
49     except:
50         pass
51     # provide compatibility with old-style pidict file (not dot-prefixed)
52     #fpidict   = getPiDict('(\d*)',hidden=False)
53     fpidict   = getPiDict('#####',hidden=True)
54     dirpidict = os.path.dirname(fpidict)
55     fpidict   = os.path.basename(fpidict)
56     fpidict = fpidict.replace('#####', '(\d*)')
57     fnamere   = re.compile("^%s$" % fpidict)
58     try:
59         for f in os.listdir(dirpidict):
60             mo = fnamere.match(f)
61             try:
62                 killMyPort(mo.group(1))
63             except:
64                 pass
65             pass
66         pass
67     except:
68         pass
69     # kill other processes
70     if sys.platform != 'win32':
71         import commands
72         cmd = "ps -fea | grep '%s' | grep 'ghs3d' | grep 'f /tmp/GHS3D_' | grep -v 'grep' | awk '{print $2}'" % user
73         prc = commands.getoutput(cmd)
74         for field in prc.split():
75             try:
76                 os.kill(int(field), signal.SIGKILL)
77             except:
78                 pass
79             pass
80         pass
81     pass
82
83 if __name__ == "__main__":
84     killAllPorts()
85     pass
86