Salome HOME
537dfaf3d8cfb9c1af36ff67772606ed2e6cc1a4
[modules/kernel.git] / bin / addToKillList.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, string, signal
23 from launchConfigureParser import verbose
24
25 ########## adds to the kill list of SALOME one more process ##########
26
27 def findFileDict():
28     if os.environ.has_key('NSPORT'):
29         my_port = os.environ['NSPORT']
30         pass
31     else:
32         my_port = 2809
33         try:
34             file = open(os.environ["OMNIORB_CONFIG"], "r")
35             s = file.read()
36             while len(s):
37                 l = string.split(s, ":")
38                 if string.split(l[0], " ")[0] == "ORBInitRef" or string.split(l[0], " ")[0] == "InitRef" :
39                     my_port = int(l[len(l)-1])
40                     pass
41                 s = file.read()
42                 pass
43             pass
44         except:
45             pass
46         pass
47     if verbose(): print "myport = ", my_port
48     return my_port
49     
50 def addToKillList(command_pid, command):
51     my_port = findFileDict()
52     from killSalomeWithPort import getPiDict
53     filedict=getPiDict(my_port)
54     try:
55         fpid=open(filedict, 'r')
56         process_ids=pickle.load(fpid)
57         fpid.close()
58     except:
59         process_ids=[{}]
60         pass
61         
62     already_in=0
63     for process_id in process_ids:
64         if verbose(): print process_id
65         for pid, cmd in process_id.items():
66             #print "see process %s : %s"% (pid, cmd[0])
67             if pid == command_pid:
68                 already_in=1
69                 pass
70             pass
71         pass
72
73     command=(command.split(" "))[0]
74     if already_in == 0:
75         try:
76             process_ids.append({command_pid: [command]})
77             fpid=open(filedict,'w')
78             pickle.dump(process_ids, fpid)
79             fpid.close()
80         except:
81             print "addToKillList: can not add command %s to the kill list"% filedict
82             pass
83         pass
84     pass
85
86 def killList():
87     my_port = findFileDict()
88     from killSalomeWithPort import getPiDict
89     filedict=getPiDict(my_port)
90     try:
91         fpid=open(filedict, 'r')
92         process_ids=pickle.load(fpid)
93         fpid.close()
94     except:
95         process_ids=[{}]
96         pass
97
98     for process_id in process_ids:
99         print process_id
100         for pid, cmd in process_id.items():
101             print "stop process %s : %s"% (pid, cmd[0])
102             try:
103                 os.kill(int(pid),signal.SIGKILL)
104             except:
105                 print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
106                 pass
107             pass
108         pass
109     os.remove(filedict)
110     pass
111   
112
113
114 if __name__ == "__main__":
115     if verbose(): print sys.argv
116     addToKillList(sys.argv[1], sys.argv[2])