X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=bin%2FPortManager.py;h=a07bd40d49f1b72be4eb6b6a911875d47ab4828b;hb=a4874256219c549a9b1ff740d549391c4bf2f25f;hp=345af4e41912189ae3191c174941016d60b21118;hpb=79a0be32272f466da346a243f622fd55ed9f134e;p=modules%2Fkernel.git diff --git a/bin/PortManager.py b/bin/PortManager.py index 345af4e41..a07bd40d4 100644 --- a/bin/PortManager.py +++ b/bin/PortManager.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2017 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2007-2021 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 @@ -23,11 +23,11 @@ # import os import sys +import psutil -try: - import cPickle as pickle #@UnusedImport -except: - import pickle #@Reimport +from socket import AF_INET, SOCK_STREAM + +import pickle __PORT_MIN_NUMBER = 2810 __PORT_MAX_NUMBER = 2910 @@ -35,8 +35,8 @@ __PORT_MAX_NUMBER = 2910 import logging def createLogger(): logger = logging.getLogger(__name__) - logger.setLevel(logging.DEBUG) - #logger.setLevel(logging.INFO) + #logger.setLevel(logging.DEBUG) + logger.setLevel(logging.INFO) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s:%(threadName)s:%(pathname)s[%(lineno)s]%(message)s") @@ -82,7 +82,14 @@ def _getConfigurationFilename(): hidden=True) import tempfile temp = tempfile.NamedTemporaryFile() - lock_file = os.path.join(os.path.dirname(temp.name), ".salome_PortManager.lock") + lock_file = os.path.join(os.path.dirname(temp.name), ".salome", ".PortManager.lock") + try: + oldmask = os.umask(0) + os.makedirs(os.path.dirname(lock_file)) + except IOError: + pass + finally: + os.umask(oldmask) temp.close() return (portmanager_config, lock_file) @@ -96,43 +103,12 @@ def __isPortUsed(port, config): # def __isNetworkConnectionActiveOnPort(port): - # :NOTE: Under windows: - # netstat options -l and -t are unavailable - # grep command is unavailable - if sys.platform == "win32": - cmd = ['netstat','-a','-n','-p tcp'] - else: - cmd = ['netstat','-ant'] - pass - - err = None - try: - from subprocess import Popen, PIPE, STDOUT - 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) - import traceback - traceback.print_exc() - return False - - from io import StringIO - buf = StringIO(out.decode()) - ports = buf.readlines() - # search for TCP - LISTEN connections - import re - regObj = re.compile( ".*tcp.*:([0-9]+).*:.*listen", re.IGNORECASE ); - for item in ports: - try: - p = int(regObj.match(item).group(1)) - if p == port: return True - except: - pass - return False -# + # psutil realization + return port in [c.laddr.port for c in psutil.net_connections(kind='inet') if \ + (c.family, c.type, c.status) == (AF_INET, SOCK_STREAM, "LISTEN")] + # -def getPort(preferedPort=None): +def getPort(preferredPort=None): logger.debug("GET PORT") config_file, lock_file = _getConfigurationFilename() @@ -147,8 +123,8 @@ def getPort(preferedPort=None): try: with open(config_file, 'rb') as f: config = pickle.load(f) - except: - logger.info("Problem loading PortManager file: %s"%config_file) + except Exception: + logger.debug("Problem loading PortManager file: %s"%config_file) # In this case config dictionary is reset logger.debug("load config: %s"%str(config)) @@ -159,7 +135,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):