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