Salome HOME
Bug IPAL19893 4.x: debug information is in terminal.
[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     """
29     Detect current SALOME session's port number.
30     Returns port number.
31     """
32     from salome_utilities import getPortNumber
33     port = getPortNumber()
34     if verbose(): print "myport = ", port
35     return port
36     
37 def addToKillList(command_pid, command, port=None):
38     """
39     Add the process to the SALOME processes dictionary file.
40     Parameters:
41     - command_pid : command PID
42     - command     : command (string or list of strings)
43     - [port]      : SALOME port number; if this parameter is None (default),
44     it is detected automatically
45     """
46     # retrieve current processes dictionary
47     from killSalomeWithPort import getPiDict
48     if port is None: port=findFileDict()
49     filedict=getPiDict(port)
50     try:
51         fpid=open(filedict, 'r')
52         process_ids=pickle.load(fpid)
53         fpid.close()
54     except:
55         process_ids=[]
56         pass
57     # check if PID is already in dictionary
58     already_in=False
59     for process_id in process_ids:
60         for pid, cmd in process_id.items():
61             if int(pid) == int(command_pid):
62                 already_in=True
63                 break
64             pass
65         if already_in: break
66         pass
67     # add process to the dictionary
68     if not already_in:
69         import types
70         if type(command) == types.ListType: command=" ".join([str(c) for c in command])
71         command=command.split()[0]
72         try:
73             if verbose(): print "addToKillList: %s : %s" % ( str(command_pid), command )
74             process_ids.append({int(command_pid): [command]})
75             dir = os.path.dirname(filedict)
76             if not os.path.exists(dir): os.makedirs(dir, 0777)
77             fpid = open(filedict,'w')
78             pickle.dump(process_ids, fpid)
79             fpid.close()
80         except:
81             if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
82             pass
83         pass
84     pass
85
86 def killList(port=None):
87     """
88     Kill all the processes listed in the SALOME processes dictionary file.
89     - [port]      : SALOME port number; if this parameter is None (default),
90     it is detected automatically
91     """
92     # retrieve processes dictionary
93     from killSalomeWithPort import getPiDict
94     if port is None: port=findFileDict()
95     # new-style dot-prefixed pidict file
96     filedict=getPiDict(port, hidden=True)
97     # provide compatibility with old-style pidict file (not dot-prefixed)
98     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
99     try:
100         fpid=open(filedict, 'r')
101         process_ids=pickle.load(fpid)
102         fpid.close()
103     except:
104         process_ids=[]
105         pass
106     # kill processes
107     for process_id in process_ids:
108         #print process_id
109         for pid, cmd in process_id.items():
110             print "stop process %s : %s"% (pid, cmd[0])
111             try:
112                 os.kill(int(pid),signal.SIGKILL)
113             except:
114                 print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
115                 pass
116             pass
117         pass
118     # remove processes dictionary file
119     os.remove(filedict)
120     pass
121
122 if __name__ == "__main__":
123     if verbose(): print sys.argv
124     addToKillList(sys.argv[1], sys.argv[2])
125     pass