Salome HOME
CCAR: improve SALOME application generation : links all subdirectories in share/salome
[modules/kernel.git] / bin / addToKillList.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
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.
12 #
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.
17 #
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
21 #
22 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24 import os, sys, pickle, string, signal
25 from launchConfigureParser import verbose
26
27 ########## adds to the kill list of SALOME one more process ##########
28
29 def findFileDict():
30     """
31     Detect current SALOME session's port number.
32     Returns port number.
33     """
34     from salome_utils import getPortNumber
35     port = getPortNumber()
36     if verbose(): print "myport = ", port
37     return port
38     
39 def addToKillList(command_pid, command, port=None):
40     """
41     Add the process to the SALOME processes dictionary file.
42     Parameters:
43     - command_pid : command PID
44     - command     : command (string or list of strings)
45     - [port]      : SALOME port number; if this parameter is None (default),
46     it is detected automatically
47     """
48     # retrieve current processes dictionary
49     from killSalomeWithPort import getPiDict
50     if port is None: port=findFileDict()
51     filedict=getPiDict(port)
52     try:
53         fpid=open(filedict, 'r')
54         process_ids=pickle.load(fpid)
55         fpid.close()
56     except:
57         process_ids=[]
58         pass
59     # check if PID is already in dictionary
60     already_in=False
61     for process_id in process_ids:
62         for pid, cmd in process_id.items():
63             if int(pid) == int(command_pid):
64                 already_in=True
65                 break
66             pass
67         if already_in: break
68         pass
69     # add process to the dictionary
70     if not already_in:
71         import types
72         if type(command) == types.ListType: command=" ".join([str(c) for c in command])
73         command=command.split()[0]
74         try:
75             if verbose(): print "addToKillList: %s : %s" % ( str(command_pid), command )
76             process_ids.append({int(command_pid): [command]})
77             dir = os.path.dirname(filedict)
78             if not os.path.exists(dir): os.makedirs(dir, 0777)
79             fpid = open(filedict,'w')
80             pickle.dump(process_ids, fpid)
81             fpid.close()
82         except:
83             if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
84             pass
85         pass
86     pass
87
88 def killList(port=None):
89     """
90     Kill all the processes listed in the SALOME processes dictionary file.
91     - [port]      : SALOME port number; if this parameter is None (default),
92     it is detected automatically
93     """
94     # retrieve processes dictionary
95     from killSalomeWithPort import getPiDict
96     if port is None: port=findFileDict()
97     # new-style dot-prefixed pidict file
98     filedict=getPiDict(port, hidden=True)
99     # provide compatibility with old-style pidict file (not dot-prefixed)
100     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
101     try:
102         fpid=open(filedict, 'r')
103         process_ids=pickle.load(fpid)
104         fpid.close()
105     except:
106         process_ids=[]
107         pass
108     # kill processes
109     for process_id in process_ids:
110         #print process_id
111         for pid, cmd in process_id.items():
112             print "stop process %s : %s"% (pid, cmd[0])
113             try:
114                 os.kill(int(pid),signal.SIGKILL)
115             except:
116                 print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
117                 pass
118             pass
119         pass
120     # remove processes dictionary file
121     os.remove(filedict)
122     pass
123
124 if __name__ == "__main__":
125     if verbose(): print sys.argv
126     addToKillList(sys.argv[1], sys.argv[2])
127     pass