Salome HOME
#20171 [CEA 20071] Handle missing netstat
[modules/kernel.git] / bin / addToKillList.py
index de99c74775a3df05db0876395af34c0801daaa29..762839ad99cab8af88142c1ea106a1234525db88 100755 (executable)
@@ -1,6 +1,6 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -22,7 +22,7 @@
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
 
-import os, sys, pickle, string, signal
+import os, sys, pickle, signal
 from launchConfigureParser import verbose
 
 ########## adds to the kill list of SALOME one more process ##########
@@ -34,7 +34,7 @@ def findFileDict():
     """
     from salome_utils import getPortNumber
     port = getPortNumber()
-    if verbose(): print "myport = ", port
+    if verbose(): print("myport = ", port)
     return port
 
 def addToKillList(command_pid, command, port=None):
@@ -49,14 +49,11 @@ def addToKillList(command_pid, command, port=None):
     # retrieve current processes dictionary
     from killSalomeWithPort import getPiDict
     if port is None: port=findFileDict()
-    try:
-        import PortManager
-        filedict = getPiDict(port, hidden=True, with2809pid=True)
-    except:
-        filedict=getPiDict(port)
+    filedict = getPiDict(port)
+    #filedict = getPiDict(port).encode()
 
     try:
-        with open(filedict, 'r') as fpid:
+        with open(filedict, 'rb') as fpid:
             process_ids=pickle.load(fpid)
     except:
         process_ids=[]
@@ -64,27 +61,28 @@ def addToKillList(command_pid, command, port=None):
     # check if PID is already in dictionary
     already_in=False
     for process_id in process_ids:
-        for pid, cmd in process_id.items():
+        for pid in process_id:
             if int(pid) == int(command_pid):
                 already_in=True
                 break
             pass
         if already_in: break
         pass
+
     # add process to the dictionary
     if not already_in:
         import types
-        if type(command) == types.ListType: command=" ".join([str(c) for c in command])
+        if isinstance(command, list): command=" ".join([str(c) for c in command])
         command=command.split()[0]
         try:
-            if verbose(): print "addToKillList: %s : %s" % ( str(command_pid), command )
+            if verbose(): print("addToKillList: %s : %s" % ( str(command_pid), command ))
             process_ids.append({int(command_pid): [command]})
             dir = os.path.dirname(filedict)
-            if not os.path.exists(dir): os.makedirs(dir, 0777)
-            with open(filedict,'w') as fpid:
+            if not os.path.exists(dir): os.makedirs(dir, 0o777)
+            with open(filedict,'wb') as fpid:
                 pickle.dump(process_ids, fpid)
         except:
-            if verbose(): print "addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command )
+            if verbose(): print("addToKillList: can not add command %s : %s to the kill list" % ( str(command_pid), command ))
             pass
         pass
     pass
@@ -98,24 +96,26 @@ def killList(port=None):
     # retrieve processes dictionary
     from killSalomeWithPort import getPiDict
     if port is None: port=findFileDict()
+
     # new-style dot-prefixed pidict file
-    filedict=getPiDict(port, hidden=True)
+    filedict=getPiDict(port)
     # provide compatibility with old-style pidict file (not dot-prefixed)
     if not os.path.exists(filedict): filedict = getPiDict(port, hidden=False)
+
     try:
-        with open(filedict, 'r') as fpid:
+        with open(filedict, 'rb') as fpid:
             process_ids=pickle.load(fpid)
     except:
         process_ids=[]
         pass
     # kill processes
     for process_id in process_ids:
-        #print process_id
-        for pid, cmd in process_id.items():
+        # print(process_id)
+        for pid, cmd in list(process_id.items()):
             try:
                 os.kill(int(pid),signal.SIGKILL)
             except:
-                print "  ------------------ process %s : %s inexistant"% (pid, cmd[0])
+                print("  ------------------ process %s : %s inexistant"% (pid, cmd[0]))
                 pass
             pass
         pass
@@ -124,6 +124,8 @@ def killList(port=None):
     pass
 
 if __name__ == "__main__":
-    if verbose(): print sys.argv
+    if verbose(): print(sys.argv[1:])
+    if len(sys.argv) < 3:
+        sys.exit(-1)
     addToKillList(sys.argv[1], sys.argv[2])
     pass