Salome HOME
To see which hdf5 we're using in configure log
[modules/kernel.git] / bin / addToKillList.py
1 #!/usr/bin/env python
2 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 #  This library is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU Lesser General Public
9 #  License as published by the Free Software Foundation; either
10 #  version 2.1 of the License.
11 #
12 #  This library is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public
18 #  License along with this library; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 import os, sys, pickle, string, signal
24 from launchConfigureParser import verbose
25
26 ########## adds to the kill list of SALOME one more process ##########
27
28 def findFileDict():
29     """
30     Detect current SALOME session's port number.
31     Returns port number.
32     """
33     from salome_utils import getPortNumber
34     port = getPortNumber()
35     if verbose(): print "myport = ", port
36     return port
37     
38 def addToKillList(command_pid, command, port=None):
39     """
40     Add the process to the SALOME processes dictionary file.
41     Parameters:
42     - command_pid : command PID
43     - command     : command (string or list of strings)
44     - [port]      : SALOME port number; if this parameter is None (default),
45     it is detected automatically
46     """
47     # retrieve current processes dictionary
48     from killSalomeWithPort import getPiDict
49     if port is None: port=findFileDict()
50     filedict=getPiDict(port)
51     try:
52         fpid=open(filedict, 'r')
53         process_ids=pickle.load(fpid)
54         fpid.close()
55     except:
56         process_ids=[]
57         pass
58     # check if PID is already in dictionary
59     already_in=False
60     for process_id in process_ids:
61         for pid, cmd in process_id.items():
62             if int(pid) == int(command_pid):
63                 already_in=True
64                 break
65             pass
66         if already_in: break
67         pass
68     # add process to the dictionary
69     if not already_in:
70         import types
71         if type(command) == types.ListType: command=" ".join([str(c) for c in command])
72         command=command.split()[0]
73         try:
74             if verbose(): print "addToKillList: %s : %s" % ( str(command_pid), command )
75             process_ids.append({int(command_pid): [command]})
76             dir = os.path.dirname(filedict)
77             if not os.path.exists(dir): os.makedirs(dir, 0777)
78             fpid = open(filedict,'w')
79             pickle.dump(process_ids, fpid)
80             fpid.close()
81         except:
82             if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
83             pass
84         pass
85     pass
86
87 def killList(port=None):
88     """
89     Kill all the processes listed in the SALOME processes dictionary file.
90     - [port]      : SALOME port number; if this parameter is None (default),
91     it is detected automatically
92     """
93     # retrieve processes dictionary
94     from killSalomeWithPort import getPiDict
95     if port is None: port=findFileDict()
96     # new-style dot-prefixed pidict file
97     filedict=getPiDict(port, hidden=True)
98     # provide compatibility with old-style pidict file (not dot-prefixed)
99     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
100     try:
101         fpid=open(filedict, 'r')
102         process_ids=pickle.load(fpid)
103         fpid.close()
104     except:
105         process_ids=[]
106         pass
107     # kill processes
108     for process_id in process_ids:
109         #print process_id
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 inexistant"% (pid, cmd[0])
116                 pass
117             pass
118         pass
119     # remove processes dictionary file
120     os.remove(filedict)
121     pass
122
123 if __name__ == "__main__":
124     if verbose(): print sys.argv
125     addToKillList(sys.argv[1], sys.argv[2])
126     pass