2 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 import os, sys, pickle, string, signal
24 from launchConfigureParser import verbose
26 ########## adds to the kill list of SALOME one more process ##########
30 Detect current SALOME session's port number.
33 from salome_utils import getPortNumber
34 port = getPortNumber()
35 if verbose(): print "myport = ", port
38 def addToKillList(command_pid, command, port=None):
40 Add the process to the SALOME processes dictionary file.
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
47 # retrieve current processes dictionary
48 from killSalomeWithPort import getPiDict
49 if port is None: port=findFileDict()
50 filedict=getPiDict(port)
52 fpid=open(filedict, 'r')
53 process_ids=pickle.load(fpid)
58 # check if PID is already in dictionary
60 for process_id in process_ids:
61 for pid, cmd in process_id.items():
62 if int(pid) == int(command_pid):
68 # add process to the dictionary
71 if type(command) == types.ListType: command=" ".join([str(c) for c in command])
72 command=command.split()[0]
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)
82 if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
87 def killList(port=None):
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
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)
101 fpid=open(filedict, 'r')
102 process_ids=pickle.load(fpid)
108 for process_id in process_ids:
110 for pid, cmd in process_id.items():
111 print "stop process %s : %s"% (pid, cmd[0])
113 os.kill(int(pid),signal.SIGKILL)
115 print " ------------------ process %s : %s inexistant"% (pid, cmd[0])
119 # remove processes dictionary file
123 if __name__ == "__main__":
124 if verbose(): print sys.argv
125 addToKillList(sys.argv[1], sys.argv[2])