2 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 ## \file killSalomeWithPort.py
24 # Stop all %SALOME servers from given sessions by killing them
26 # The sessions are indicated by their ports on the command line as in :
28 # killSalomeWithPort.py 2811 2815
31 import os, sys, pickle, signal, commands,glob
32 from launchConfigureParser import verbose
36 def getPiDict(port,appname='salome',full=True,hidden=True):
38 Get file with list of SALOME processes.
39 This file is located in the user's home directory
40 and named .<user>_<host>_<port>_SALOME_pidict
48 - appname : application name (default is 'SALOME')
49 - full : if True, full path to the file is returned, otherwise only file name is returned
50 - hidden : if True, file name is prefixed with . (dot) symbol; this internal parameter is used
51 to support compatibility with older versions of SALOME
53 from salome_utils import generateFileName, getTmpDir
56 # full path to the pidict file is requested
58 # new-style dot-prefixed pidict files
59 # are in the system-dependant temporary diretory
62 # old-style non-dot-prefixed pidict files
63 # are in the user's home directory
64 dir = os.getenv("HOME")
67 return generateFileName(dir,
73 with_app=appname.upper())
75 def appliCleanOmniOrbConfig(port):
77 Remove omniorb config files related to the port in SALOME application:
78 - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_${HOSTNAME}_${NSPORT}.cfg
79 - ${HOME}/${APPLI}/USERS/.omniORB_${USER}_last.cfg
80 the last is removed only if the link points to the first file.
82 from salome_utils import generateFileName
83 home = os.getenv("HOME")
84 appli = os.getenv("APPLI")
86 #Run outside application context
89 dir = os.path.join(home, appli,"USERS")
90 omniorb_config = generateFileName(dir, prefix="omniORB",
96 last_running_config = generateFileName(dir, prefix="omniORB",
101 if os.access(last_running_config,os.F_OK):
102 pointedPath = os.readlink(last_running_config)
103 if pointedPath[0] != '/':
104 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
105 if pointedPath == omniorb_config:
106 os.unlink(last_running_config)
109 if os.access(omniorb_config,os.F_OK):
110 os.remove(omniorb_config)
113 #try to relink last.cfg to an existing config file if any
114 files = glob.glob(os.path.join(os.environ["HOME"],Utils_Identity.getapplipath(),
115 "USERS",".omniORB_"+salome_utils.getUserName()+"_*.cfg"))
120 if stat.st_atime > current:
121 current=stat.st_atime
124 os.symlink(os.path.normpath(current_config), last_running_config)
129 ########## kills all salome processes with the given port ##########
131 def killMyPort(port):
133 Kill SALOME session running on the specified port.
137 # new-style dot-prefixed pidict file
138 filedict = getPiDict(port, hidden=True)
139 # provide compatibility with old-style pidict file (not dot-prefixed)
140 if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
143 fpid = open(filedict, 'r')
145 from salome_utils import generateFileName
146 fpidomniNames = generateFileName(os.path.join('/tmp/logs', os.getenv('USER')),
148 suffix="Pid_omniNames",
151 if not sys.platform == 'win32':
152 cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start %s"` ; echo $pid > %s' % ( str(port), fpidomniNames )
156 fpidomniNamesFile = open(fpidomniNames)
157 lines = fpidomniNamesFile.readlines()
158 fpidomniNamesFile.close()
159 os.remove(fpidomniNames)
162 pidfield = l.split()[0] # pid should be at the first position
163 if sys.platform == "win32":
165 if verbose(): print 'stop process '+pidfield+' : omniNames'
166 win32pm.killpid(int(pidfield),0)
168 if verbose(): print 'stop process '+pidfield+' : omniNames'
169 os.kill(int(pidfield),signal.SIGKILL)
180 process_ids=pickle.load(fpid)
182 for process_id in process_ids:
183 for pid, cmd in process_id.items():
184 if verbose(): print "stop process %s : %s"% (pid, cmd[0])
186 if sys.platform == "win32":
188 win32pm.killpid(int(pid),0)
190 os.kill(int(pid),signal.SIGKILL)
194 if verbose(): print " ------------------ process %s : %s not found"% (pid, cmd[0])
196 pass # for pid, cmd ...
197 pass # for process_id ...
203 cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
204 pid = commands.getoutput(cmd)
206 while pid and len(a.split()) < 2:
207 a = commands.getoutput("kill -9 " + pid)
208 pid = commands.getoutput(cmd)
213 print "Cannot find or open SALOME PIDs file for port", port
216 appliCleanOmniOrbConfig(port)
219 def killNotifdAndClean(port):
221 Kill notifd daemon and clean application running on the specified port.
226 filedict=getPiDict(port)
227 f=open(filedict, 'r')
230 for pid,process in d.items():
231 if 'notifd' in process:
232 cmd='kill -9 %d'% pid
237 #traceback.print_exc()
240 appliCleanOmniOrbConfig(port)
242 if __name__ == "__main__":
243 for port in sys.argv[1:]: