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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 import os, sys, pickle, string, signal
23 from launchConfigureParser import verbose
25 ########## adds to the kill list of SALOME one more process ##########
29 Detect current SALOME session's port number.
32 from salome_utilities import getPortNumber
33 port = getPortNumber()
34 if verbose(): print "myport = ", port
37 def addToKillList(command_pid, command, port=None):
39 Add the process to the SALOME processes dictionary file.
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
46 # retrieve current processes dictionary
47 from killSalomeWithPort import getPiDict
48 if port is None: port=findFileDict()
49 filedict=getPiDict(port)
51 fpid=open(filedict, 'r')
52 process_ids=pickle.load(fpid)
57 # check if PID is already in dictionary
59 for process_id in process_ids:
60 for pid, cmd in process_id.items():
61 if int(pid) == int(command_pid):
67 # add process to the dictionary
70 if type(command) == types.ListType: command=" ".join([str(c) for c in command])
71 command=command.split()[0]
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)
81 if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
86 def killList(port=None):
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
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)
100 fpid=open(filedict, 'r')
101 process_ids=pickle.load(fpid)
107 for process_id in process_ids:
109 for pid, cmd in process_id.items():
110 print "stop process %s : %s"% (pid, cmd[0])
112 os.kill(int(pid),signal.SIGKILL)
114 print " ------------------ process %s : %s inexistant"% (pid, cmd[0])
118 # remove processes dictionary file
122 if __name__ == "__main__":
123 if verbose(): print sys.argv
124 addToKillList(sys.argv[1], sys.argv[2])