Salome HOME
#17872 [CEA] /tmp/.salome_PortManager.lock permission denied
[modules/kernel.git] / bin / PortManager.py
index ced2ad296a56f28f7c2f388c0e63bb196362eaea..e0584ac81529000f029ad5267f0b1b6a66dbd8b9 100644 (file)
@@ -1,6 +1,6 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2017  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2019  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
@@ -35,11 +35,11 @@ __PORT_MAX_NUMBER = 2910
 import logging
 def createLogger():
   logger = logging.getLogger(__name__)
-#  logger.setLevel(logging.DEBUG)
+  #logger.setLevel(logging.DEBUG)
   logger.setLevel(logging.INFO)
   ch = logging.StreamHandler()
   ch.setLevel(logging.DEBUG)
-  formatter = logging.Formatter("%(levelname)s:%(threadName)s:%(message)s")
+  formatter = logging.Formatter("%(levelname)s:%(threadName)s:%(pathname)s[%(lineno)s]%(message)s")
   ch.setFormatter(formatter)
   logger.addHandler(ch)
   return logger
@@ -83,6 +83,11 @@ def _getConfigurationFilename():
   import tempfile
   temp = tempfile.NamedTemporaryFile()
   lock_file = os.path.join(os.path.dirname(temp.name), ".salome_PortManager.lock")
+  try:
+    with open(lock_file, 'wb'):
+      pass
+  except IOError:
+    pass
   temp.close()
 
   return (portmanager_config, lock_file)
@@ -100,7 +105,7 @@ def __isNetworkConnectionActiveOnPort(port):
   #        netstat options -l and -t are unavailable
   #        grep command is unavailable
   if sys.platform == "win32":
-    cmd = ['netstat','-a','-n','-p tcp']
+    cmd = ['netstat','-a','-n','-p','tcp']
   else:
     cmd = ['netstat','-ant']
     pass
@@ -111,14 +116,14 @@ def __isNetworkConnectionActiveOnPort(port):
     p = Popen(cmd, stdout=PIPE, stderr=STDOUT)
     out, err = p.communicate()
   except:
-    print "Error when trying to access active network connections."
-    if err: print err
+    print("Error when trying to access active network connections.")
+    if err: print(err)
     import traceback
     traceback.print_exc()
     return False
 
-  import StringIO
-  buf = StringIO.StringIO(out)
+  from io import StringIO
+  buf = StringIO(out.decode('utf-8', 'ignore'))
   ports = buf.readlines()
   # search for TCP - LISTEN connections
   import re
@@ -132,12 +137,12 @@ def __isNetworkConnectionActiveOnPort(port):
   return False
 #
 
-def getPort(preferedPort=None):
+def getPort(preferredPort=None):
   logger.debug("GET PORT")
 
   config_file, lock_file = _getConfigurationFilename()
   oldmask = os.umask(0)
-  with open(lock_file, 'w') as lock:
+  with open(lock_file, 'rb') as lock:
     # acquire lock
     __acquire_lock(lock)
 
@@ -145,7 +150,7 @@ def getPort(preferedPort=None):
     config = {}
     logger.debug("read configuration file")
     try:
-      with open(config_file, 'r') as f:
+      with open(config_file, 'rb') as f:
         config = pickle.load(f)
     except:
       logger.info("Problem loading PortManager file: %s"%config_file)
@@ -159,7 +164,7 @@ def getPort(preferedPort=None):
         config[appli_path] = []
 
     # append port
-    port = preferedPort
+    port = preferredPort
     if not port or __isPortUsed(port, config):
       port = __PORT_MIN_NUMBER
       while __isPortUsed(port, config):
@@ -167,7 +172,7 @@ def getPort(preferedPort=None):
           msg  = "\n"
           msg += "Can't find a free port to launch omniNames\n"
           msg += "Try to kill the running servers and then launch SALOME again.\n"
-          raise RuntimeError, msg
+          raise RuntimeError(msg)
         logger.debug("Port %s seems to be busy"%str(port))
         port = port + 1
     logger.debug("found free port: %s"%str(port))
@@ -176,8 +181,8 @@ def getPort(preferedPort=None):
     # write config
     logger.debug("write config: %s"%str(config))
     try:
-      with open(config_file, 'w') as f:
-        pickle.dump(config, f)
+      with open(config_file, 'wb') as f:
+        pickle.dump(config, f, protocol=0)
     except IOError:
       pass
 
@@ -196,7 +201,7 @@ def releasePort(port):
 
   config_file, lock_file = _getConfigurationFilename()
   oldmask = os.umask(0)
-  with open(lock_file, 'w') as lock:
+  with open(lock_file, 'rb') as lock:
     # acquire lock
     __acquire_lock(lock)
 
@@ -204,7 +209,7 @@ def releasePort(port):
     config = {}
     logger.debug("read configuration file")
     try:
-      with open(config_file, 'r') as f:
+      with open(config_file, 'rb') as f:
         config = pickle.load(f)
     except IOError: # empty file
       pass
@@ -223,8 +228,8 @@ def releasePort(port):
     # write config
     logger.debug("write config: %s"%str(config))
     try:
-      with open(config_file, 'w') as f:
-        pickle.dump(config, f)
+      with open(config_file, 'wb') as f:
+        pickle.dump(config, f, protocol=0)
     except IOError:
       pass
 
@@ -239,7 +244,7 @@ def releasePort(port):
 def getBusyPorts():
   config_file, lock_file = _getConfigurationFilename()
   oldmask = os.umask(0)
-  with open(lock_file, 'w') as lock:
+  with open(lock_file, 'wb') as lock:
     # acquire lock
     __acquire_lock(lock)
 
@@ -247,7 +252,7 @@ def getBusyPorts():
     config = {}
     logger.debug("read configuration file")
     try:
-      with open(config_file, 'r') as f:
+      with open(config_file, 'rb') as f:
         config = pickle.load(f)
     except IOError: # empty file
       pass