Salome HOME
Join modifications from branch BR_PR_V320b1
[modules/yacs.git] / bin / killSalomeWithPort.py
1 #!/usr/bin/env python
2 import os, sys, pickle, signal, commands
3
4 def getPiDict(port,appname='salome',full=True):
5     from Utils_Identity import getShortHostName
6
7     host = os.getenv("HOSTNAME")
8     if not host:
9         host = os.getenv("HOST")
10     if not host:
11         host = getShortHostName()
12
13     filedict = []
14     filedict.append( os.getenv('USER') )          # user name
15     filedict.append( host )                       # host name
16     filedict.append( str(port) )                  # port number
17     filedict.append( appname.upper() )            # application name
18     filedict.append( 'pidict' )                   # constant part
19
20     filedict = '_'.join(filedict)
21     if full:
22         filedict = os.getenv("HOME") + '/' + filedict
23     return filedict
24
25 def appliCleanOmniOrbConfig(port):
26     """
27     remove omniorb config files related to the port in SALOME application:
28     - ${HOME}/${APPLI}/.omniORB_${HOSTNAME}_${NSPORT}.cfg
29     - ${HOME}/${APPLI}/.omniORB_${HOSTNAME}_last.cfg
30     the last is removed only if the link points to the first file.
31     """
32     from Utils_Identity import getShortHostName
33     appli=os.environ.get("APPLI")
34     if appli is None:
35         #Run outside application context
36         pass
37     else:
38         home = os.environ['HOME']
39         home='%s/%s'%(home,appli)
40         hostname=getShortHostName()
41         omniorb_config = '%s/.omniORB_%s_%s.cfg'%(home,hostname, str(port))
42         last_running_config = '%s/.omniORB_%s_last.cfg'%(home, hostname)
43         if os.access(last_running_config,os.F_OK):
44             pointedPath = os.readlink(last_running_config)
45             if pointedPath[0] != '/':
46                 pointedPath=os.path.join(os.path.dirname(last_running_config), pointedPath)
47             if pointedPath == omniorb_config:
48                 os.unlink(last_running_config)
49                 pass
50             pass
51         if os.access(omniorb_config,os.F_OK):
52             os.remove(omniorb_config)
53             pass
54         pass
55
56 ########## kills all salome processes with the given port ##########
57
58 def killMyPort(port):
59     filedict=getPiDict(port)
60     found = 0
61     try:
62         fpid=open(filedict, 'r')
63         found = 1
64     except:
65         print "file %s giving SALOME process id is not readable"% filedict
66         pass
67         
68     if found:
69         cmd = 'pid=`ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'"` ; echo $pid > /tmp/logs/'+os.getenv('USER')+"/_"+port+'_Pid_omniNames.log'
70         a = os.system(cmd)
71         try:
72             fpidomniNames=open('/tmp/logs/'+os.getenv('USER')+"/_"+port+'_Pid_omniNames.log')
73             prc = fpidomniNames.read()
74             fpidomniNames.close()
75             if prc != None :
76                 for field in prc.split(" ") :
77                     if field == "omniNames" :
78                         if pidfield != "egrep" :
79                             print 'stop process '+pidfield+' : omniNames'
80                             os.system('kill -9 '+pidfield)
81                     pidfield = field
82         except:
83             pass
84         
85         try:
86             process_ids=pickle.load(fpid)
87             fpid.close()
88             for process_id in process_ids:
89
90                 for pid, cmd in process_id.items():
91                     print "stop process %s : %s"% (pid, cmd[0])
92                     try:
93                         os.kill(int(pid),signal.SIGKILL)
94                     except:
95                         print "  ------------------ process %s : %s not found"% (pid, cmd[0])
96                         pass
97                 pass
98         except:
99             pass
100         
101         os.remove(filedict)
102         cmd='ps -eo pid,command | egrep "[0-9] omniNames -start '+str(port)+'" | sed -e "s%[^0-9]*\([0-9]*\) .*%\\1%g"'
103         pid = commands.getoutput(cmd)
104         a = ""
105         while pid != "" and len(a.split(" ")) < 2:
106             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")
107             pid = commands.getoutput("ps -eo pid,command | egrep \"[0-9] omniNames -start "+str(port)+"\" | sed -e \"s%[^0-9]*\([0-9]*\) .*%\\1%g\"")
108             print pid
109             
110         pass
111
112     appliCleanOmniOrbConfig(port)
113     pass
114             
115
116 if __name__ == "__main__":
117     for port in sys.argv[1:]:
118         killMyPort(port)