Salome HOME
Calcium and Palm ok
[modules/kernel.git] / bin / killSalomeWithPort.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
4 #           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either 
8 # version 2.1 of the License.
9
10 # This library is distributed in the hope that it will be useful 
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 # Lesser General Public License for more details.
14
15 # You should have received a copy of the GNU Lesser General Public  
16 # License along with this library; if not, write to the Free Software 
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20
21
22 import os, sys, pickle, signal, commands
23
24 def getPiDict(port,appname='salome',full=True):
25     from Utils_Identity import getShortHostName
26
27     host = os.getenv("HOSTNAME")
28     if not host:
29         host = os.getenv("HOST")
30     if not host:
31         host = getShortHostName()
32
33     filedict = []
34     filedict.append( os.getenv('USER') )          # user name
35     filedict.append( host )                       # host name
36     filedict.append( str(port) )                  # port number
37     filedict.append( appname.upper() )            # application name
38     filedict.append( 'pidict' )                   # constant part
39
40     filedict = '_'.join(filedict)
41     if full:
42         filedict = os.getenv("HOME") + '/' + filedict
43     return filedict
44
45 def appliCleanOmniOrbConfig(port):
46     """
47     remove omniorb config files related to the port in SALOME application:
48     - ${HOME}/${APPLI}/.omniORB_${HOSTNAME}_${NSPORT}.cfg
49     - ${HOME}/${APPLI}/.omniORB_${HOSTNAME}_last.cfg
50     the last is removed only if the link points to the first file.
51     """
52     from Utils_Identity import getShortHostName
53     appli=os.environ.get("APPLI")
54     if appli is None:
55         #Run outside application context
56         pass
57     else:
58         home = os.environ['HOME']
59         home='%s/%s'%(home,appli)
60         hostname=getShortHostName()
61         omniorb_config = '%s/.omniORB_%s_%s.cfg'%(home,hostname, str(port))
62         last_running_config = '%s/.omniORB_%s_last.cfg'%(home, hostname)
63         if os.access(last_running_config,os.F_OK):
64             pointedPath = os.readlink(last_running_config)
65             if pointedPath[0] != '/':
66                 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
67             if pointedPath == omniorb_config:
68                 os.unlink(last_running_config)
69                 pass
70             pass
71         if os.access(omniorb_config,os.F_OK):
72             os.remove(omniorb_config)
73             pass
74         pass
75
76 ########## kills all salome processes with the given port ##########
77
78 def killMyPort(port):
79     filedict=getPiDict(port)
80     found = 0
81     try:
82         fpid=open(filedict, 'r')
83         found = 1
84     except:
85         print "file %s giving SALOME process id is not readable"% filedict
86         pass
87         
88     if found:
89         cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'"` ; echo $pid > /tmp/logs/'+os.getenv('USER')+"/_"+port+'_Pid_omniNames.log'
90         a = os.system(cmd)
91         try:
92             fpidomniNames=open('/tmp/logs/'+os.getenv('USER')+"/_"+port+'_Pid_omniNames.log')
93             prc = fpidomniNames.read()
94             fpidomniNames.close()
95             if prc != None :
96                 for field in prc.split(" ") :
97                     if field == "omniNames" :
98                         if pidfield != "egrep" :
99                             print 'stop process '+pidfield+' : omniNames'
100                             os.system('kill -9 '+pidfield)
101                     pidfield = field
102         except:
103             pass
104         
105         try:
106             process_ids=pickle.load(fpid)
107             fpid.close()
108             for process_id in process_ids:
109
110                 for pid, cmd in process_id.items():
111                     print "stop process %s : %s"% (pid, cmd[0])
112                     try:
113                         os.kill(int(pid),signal.SIGKILL)
114                     except:
115                         print "  ------------------ process %s : %s not found"% (pid, cmd[0])
116                         pass
117                 pass
118         except:
119             pass
120         
121         os.remove(filedict)
122         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
123         pid = commands.getoutput(cmd)
124         a = ""
125         while pid != "" and len(a.split(" ")) < 2:
126             a = commands.getoutput("pid=`ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"`; kill -9 $pid")
127             pid = commands.getoutput("ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"")
128             print pid
129             
130         pass
131
132     appliCleanOmniOrbConfig(port)
133     pass
134             
135
136 if __name__ == "__main__":
137     for port in sys.argv[1:]:
138         killMyPort(port)