2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 import os, sys, pickle, string, signal
26 from launchConfigureParser import verbose
28 ########## adds to the kill list of SALOME one more process ##########
32 Detect current SALOME session's port number.
35 from salome_utils import getPortNumber
36 port = getPortNumber()
37 if verbose(): print "myport = ", port
40 def addToKillList(command_pid, command, port=None):
42 Add the process to the SALOME processes dictionary file.
44 - command_pid : command PID
45 - command : command (string or list of strings)
46 - [port] : SALOME port number; if this parameter is None (default),
47 it is detected automatically
49 # retrieve current processes dictionary
50 from killSalomeWithPort import getPiDict
51 if port is None: port=findFileDict()
54 filedict = getPiDict(port, hidden=True, with2809pid=True)
56 filedict=getPiDict(port)
59 with open(filedict, 'r') as fpid:
60 process_ids=pickle.load(fpid)
64 # check if PID is already in dictionary
66 for process_id in process_ids:
67 for pid, cmd in process_id.items():
68 if int(pid) == int(command_pid):
74 # add process to the dictionary
77 if type(command) == types.ListType: command=" ".join([str(c) for c in command])
78 command=command.split()[0]
80 if verbose(): print "addToKillList: %s : %s" % ( str(command_pid), command )
81 process_ids.append({int(command_pid): [command]})
82 dir = os.path.dirname(filedict)
83 if not os.path.exists(dir): os.makedirs(dir, 0777)
84 with open(filedict,'w') as fpid:
85 pickle.dump(process_ids, fpid)
87 if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
92 def killList(port=None):
94 Kill all the processes listed in the SALOME processes dictionary file.
95 - [port] : SALOME port number; if this parameter is None (default),
96 it is detected automatically
98 # retrieve processes dictionary
99 from killSalomeWithPort import getPiDict
100 if port is None: port=findFileDict()
101 # new-style dot-prefixed pidict file
102 filedict=getPiDict(port, hidden=True)
103 # provide compatibility with old-style pidict file (not dot-prefixed)
104 if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
106 with open(filedict, 'r') as fpid:
107 process_ids=pickle.load(fpid)
112 for process_id in process_ids:
114 for pid, cmd in process_id.items():
116 os.kill(int(pid),signal.SIGKILL)
118 print " ------------------ process %s : %s inexistant"% (pid, cmd[0])
122 # remove processes dictionary file
126 if __name__ == "__main__":
127 if verbose(): print sys.argv
128 addToKillList(sys.argv[1], sys.argv[2])