Salome HOME
delete urifiles on killall
[modules/kernel.git] / bin / killSalome.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2016  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 #  Stop all %SALOME servers from all sessions by killing them
27 #
28
29 import os, sys, re, signal
30
31 from killSalomeWithPort import killMyPort, getPiDict
32 #from salome_utils import getHostName, getShortHostName
33 from salome_utils import getUserName
34
35 def killAllPorts():
36     """
37     Kill all SALOME sessions belonging to the user.
38     """
39     user = getUserName()
40     #hostname  = getHostName()
41     #shostname = getShortHostName()
42     # new-style dot-prefixed pidict file
43     #fpidict   = getPiDict('(\d*)',hidden=True)
44     #problem with WIN32 path slashes
45     fpidict   = getPiDict('#####',hidden=True)
46     dirpidict = os.path.dirname(fpidict)
47     fpidict   = os.path.basename(fpidict)
48     #if hostname in fpidict:
49     #    fpidict = fpidict.replace(hostname, shostname+".*")
50     fpidict   = fpidict.replace('#####', '(\d*)')
51     fnamere   = re.compile("^%s" % fpidict)
52     try:
53         for f in os.listdir(dirpidict):
54             mo = fnamere.match(f)
55             try:
56                 killMyPort(mo.group(1))
57             except:
58                 pass
59             pass
60         pass
61     except:
62         pass
63     # provide compatibility with old-style pidict file (not dot-prefixed)
64     #fpidict   = getPiDict('(\d*)',hidden=False)
65     fpidict   = getPiDict('#####',hidden=False)
66     dirpidict = os.path.dirname(fpidict)
67     fpidict   = os.path.basename(fpidict)
68     #if hostname in fpidict:
69     #    fpidict = fpidict.replace(hostname, shostname+".*")
70     fpidict = fpidict.replace('#####', '(\d*)')
71     fnamere   = re.compile("^%s$" % fpidict)
72     try:
73         for f in os.listdir(dirpidict):
74             mo = fnamere.match(f)
75             try:
76                 killMyPort(mo.group(1))
77             except:
78                 pass
79             pass
80         pass
81     except:
82         pass
83     # kill other processes
84     if sys.platform != 'win32':
85         import commands
86         cmd = "ps -fea | grep '%s' | grep 'ghs3d' | grep 'f /tmp/GHS3D_' | grep -v 'grep' | awk '{print $2}'" % user
87         prc = commands.getoutput(cmd)
88         for field in prc.split():
89             try:
90                 os.kill(int(field), signal.SIGKILL)
91             except:
92                 pass
93             pass
94         pass
95         cmd = "ps -fea | grep '%s' | grep 'ompi-server' | grep -v 'grep' | awk '{print $2}'" % user
96         prc = commands.getoutput(cmd)
97         for field in prc.split():
98             try:
99                 os.kill(int(field), signal.SIGKILL)
100             except:
101                 pass
102             pass
103         pass
104         cmd = "rm -f " + os.environ['HOME'] + "/.urifile_*"
105         commands.getoutput(cmd)
106     pass
107
108 if __name__ == "__main__":
109     try:
110         from salomeContextUtils import setOmniOrbUserPath
111         setOmniOrbUserPath()
112     except Exception, e:
113         print e
114         sys.exit(1)
115     killAllPorts()
116     pass