Back to index

salome-kernel  6.5.0
killSalome.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 ## \file killSalome.py
00026 #  Stop all %SALOME servers from all sessions by killing them
00027 #
00028 
00029 import os, sys, re, signal
00030 
00031 from killSalomeWithPort import killMyPort, getPiDict
00032 #from salome_utils import getHostName, getShortHostName
00033 
00034 def killAllPorts():
00035     """
00036     Kill all SALOME sessions belonging to the user.
00037     """
00038     user = os.getenv('USER')
00039     #hostname  = getHostName()
00040     #shostname = getShortHostName()
00041     # new-style dot-prefixed pidict file
00042     #fpidict   = getPiDict('(\d*)',hidden=True)
00043     #problem with WIN32 path slashes
00044     fpidict   = getPiDict('#####',hidden=True)
00045     dirpidict = os.path.dirname(fpidict)
00046     fpidict   = os.path.basename(fpidict)
00047     #if hostname in fpidict:
00048     #    fpidict = fpidict.replace(hostname, shostname+".*")
00049     fpidict   = fpidict.replace('#####', '(\d*)')
00050     fnamere   = re.compile("^%s$" % fpidict)
00051     try:
00052         for f in os.listdir(dirpidict):
00053             mo = fnamere.match(f)
00054             try:
00055                 killMyPort(mo.group(1))
00056             except:
00057                 pass
00058             pass
00059         pass
00060     except:
00061         pass
00062     # provide compatibility with old-style pidict file (not dot-prefixed)
00063     #fpidict   = getPiDict('(\d*)',hidden=False)
00064     fpidict   = getPiDict('#####',hidden=False)
00065     dirpidict = os.path.dirname(fpidict)
00066     fpidict   = os.path.basename(fpidict)
00067     #if hostname in fpidict:
00068     #    fpidict = fpidict.replace(hostname, shostname+".*")
00069     fpidict = fpidict.replace('#####', '(\d*)')
00070     fnamere   = re.compile("^%s$" % fpidict)
00071     try:
00072         for f in os.listdir(dirpidict):
00073             mo = fnamere.match(f)
00074             try:
00075                 killMyPort(mo.group(1))
00076             except:
00077                 pass
00078             pass
00079         pass
00080     except:
00081         pass
00082     # kill other processes
00083     if sys.platform != 'win32':
00084         import commands
00085         cmd = "ps -fea | grep '%s' | grep 'ghs3d' | grep 'f /tmp/GHS3D_' | grep -v 'grep' | awk '{print $2}'" % user
00086         prc = commands.getoutput(cmd)
00087         for field in prc.split():
00088             try:
00089                 os.kill(int(field), signal.SIGKILL)
00090             except:
00091                 pass
00092             pass
00093         pass
00094     pass
00095 
00096 if __name__ == "__main__":
00097     killAllPorts()
00098     pass
00099