Salome HOME
Deal with float input in MemRepr
[modules/kernel.git] / bin / addToKillList.py
1 #! /usr/bin/env python3
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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
25 import os, sys, pickle, signal
26 from launchConfigureParser import verbose
27
28 ########## adds to the kill list of SALOME one more process ##########
29
30 def findFileDict():
31     """
32     Detect current SALOME session's port number.
33     Returns port number.
34     """
35     from salome_utils import getPortNumber
36     port = getPortNumber()
37     if verbose(): print("myport = ", port)
38     return port
39
40 def addToKillList(command_pid, command, port=None):
41     """
42     Add the process to the SALOME processes dictionary file.
43     Parameters:
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
48     """
49     # retrieve current processes dictionary
50     from killSalomeWithPort import getPiDict
51     if port is None: port=findFileDict()
52     filedict = getPiDict(port)
53     #filedict = getPiDict(port).encode()
54
55     try:
56         with open(filedict, 'rb') as fpid:
57             process_ids=pickle.load(fpid)
58     except Exception:
59         process_ids=[]
60         pass
61     # check if PID is already in dictionary
62     already_in=False
63     for process_id in process_ids:
64         for pid in process_id:
65             if int(pid) == int(command_pid):
66                 already_in=True
67                 break
68             pass
69         if already_in: break
70         pass
71
72     # add process to the dictionary
73     if not already_in:
74         import types
75         if isinstance(command, list): command=" ".join([str(c) for c in command])
76         command=command.split()[0]
77         try:
78             if verbose(): print("addToKillList: %s : %s" % ( str(command_pid), command ))
79             process_ids.append({int(command_pid): [command]})
80             dir = os.path.dirname(filedict)
81             if not os.path.exists(dir):
82                 from salome_utils import makeDir
83                 makeDir(dir)
84             with open(filedict,'wb') as fpid:
85                 pickle.dump(process_ids, fpid)
86         except Exception:
87             if verbose(): print("addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command ))
88             pass
89         pass
90     pass
91
92 def killList(port=None):
93     """
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
97     """
98     # retrieve processes dictionary
99     from killSalomeWithPort import getPiDict
100     if port is None: port=findFileDict()
101
102     # new-style dot-prefixed pidict file
103     filedict=getPiDict(port)
104     # provide compatibility with old-style pidict file (not dot-prefixed)
105     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
106
107     try:
108         with open(filedict, 'rb') as fpid:
109             process_ids=pickle.load(fpid)
110     except Exception:
111         process_ids=[]
112         pass
113     # kill processes
114     for process_id in process_ids:
115         # print(process_id)
116         for pid, cmd in list(process_id.items()):
117             try:
118                 os.kill(int(pid),signal.SIGKILL)
119             except Exception:
120                 print("  ------------------ process %s : %s inexistant"% (pid, cmd[0]))
121                 pass
122             pass
123         pass
124     # remove processes dictionary file
125     if os.path.exists(filedict):
126         os.remove(filedict)
127     pass
128
129 if __name__ == "__main__":
130     if verbose(): print(sys.argv[1:])
131     if len(sys.argv) < 3:
132         sys.exit(-1)
133     addToKillList(sys.argv[1], sys.argv[2])
134     pass