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