X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=bin%2FsearchFreePort.py;h=9b14fabdb12a3a28e84a361f99a9f550abe2c6dc;hb=32cb9f7cf3440f3c495de75632b4cefbd23298a2;hp=32d858f316ef623ebb80ada9f02943041505c925;hpb=e5867636265822b5e70ca0406c9321ccff264bb0;p=modules%2Fkernel.git diff --git a/bin/searchFreePort.py b/bin/searchFreePort.py index 32d858f31..9b14fabdb 100644 --- a/bin/searchFreePort.py +++ b/bin/searchFreePort.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2007-2014 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 @@ -8,7 +8,7 @@ # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either -# version 2.1 of the License. +# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -22,18 +22,52 @@ # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -def searchFreePort(args={}, save_config=1): - """ - Search free port for SALOME session. - Returns first found free port number. - """ - import sys, os, re, shutil - print "Searching for a free port for naming service:", +import os +import sys + +def __setup_config(nsport, args, save_config): + # + from salome_utils import generateFileName, getHostName + hostname = getHostName() + # + omniorbUserPath = os.getenv("OMNIORB_USER_PATH") + kwargs={} + if omniorbUserPath is not None: + kwargs["with_username"]=True + # + from ORBConfigFile import writeORBConfigFile + omniorb_config, giopsize = writeORBConfigFile(omniorbUserPath, hostname, nsport, kwargs) + args['port'] = os.environ['NSPORT'] + # + if save_config: + last_running_config = generateFileName(omniorbUserPath, prefix="omniORB", + suffix="last", + extension="cfg", + hidden=True, + **kwargs) + os.environ['LAST_RUNNING_CONFIG'] = last_running_config + try: + if sys.platform == "win32": + import shutil + shutil.copyfile(omniorb_config, last_running_config) + else: + try: + if os.access(last_running_config, os.F_OK): + os.remove(last_running_config) + except OSError: + pass + os.symlink(omniorb_config, last_running_config) + pass + pass + except: + pass + # +# +def searchFreePort_withoutPortManager(args={}, save_config=1, use_port=None): # :NOTE: Under windows: # netstat options -l and -t are unavailable # grep command is unavailable - from subprocess import Popen, PIPE (stdout, stderr) = Popen(['netstat','-an'], stdout=PIPE).communicate() import StringIO @@ -42,6 +76,7 @@ def searchFreePort(args={}, save_config=1): # def portIsUsed(port, data): + import re regObj = re.compile( ".*tcp.*:([0-9]+).*:.*listen", re.IGNORECASE ); for item in data: try: @@ -53,6 +88,21 @@ def searchFreePort(args={}, save_config=1): pass return False # + + if use_port: + print "Check if port can be used: %d" % use_port, + if not portIsUsed(use_port, ports): + print "- OK" + __setup_config(use_port, args, save_config) + return + else: + print "- KO: port is busy" + pass + # + + print "Searching for a free port for naming service:", + # + NSPORT=2810 limit=NSPORT+100 # @@ -60,45 +110,7 @@ def searchFreePort(args={}, save_config=1): while 1: if not portIsUsed(NSPORT, ports): print "%s - OK"%(NSPORT) - # - from salome_utils import generateFileName, getHostName - hostname = getHostName() - # - home = os.getenv("HOME") - appli = os.getenv("APPLI") - kwargs={} - if appli is not None: - home = os.path.join(os.path.realpath(home), appli,"USERS") - kwargs["with_username"]=True - # - from ORBConfigFile import writeORBConfigFile - omniorb_config, giopsize = writeORBConfigFile(home, hostname, NSPORT, kwargs) - - args['port'] = os.environ['NSPORT'] - # - if save_config: - last_running_config = generateFileName(home, prefix="omniORB", - suffix="last", - extension="cfg", - hidden=True, - **kwargs) - os.environ['LAST_RUNNING_CONFIG'] = last_running_config - try: - if sys.platform == "win32": - import shutil - shutil.copyfile(omniorb_config, last_running_config) - else: - try: - if os.access(last_running_config, os.F_OK): - os.remove(last_running_config) - except OSError: - pass - os.symlink(omniorb_config, last_running_config) - pass - pass - except: - pass - # + __setup_config(NSPORT, args, save_config) break print "%s"%(NSPORT), if NSPORT == limit: @@ -108,4 +120,69 @@ def searchFreePort(args={}, save_config=1): raise RuntimeError, msg NSPORT=NSPORT+1 pass - return + # +# + +def searchFreePort_withPortManager(queue, args={}, save_config=1, use_port=None): + from PortManager import getPort + port = getPort(use_port) + + if use_port: + print "Check if port can be used: %d" % use_port, + if port == use_port and port != -1: + print "- OK" + __setup_config(use_port, args, save_config) + return + else: + print "- KO: port is busy" + pass + # + print "Searching for a free port for naming service:", + if port == -1: # try again + port = getPort(use_port) + + if port != -1: + print "%s - OK"%(port) + __setup_config(port, args, save_config) + else: + print "Unable to obtain port" + + queue.put([os.environ['OMNIORB_CONFIG'], + os.environ['NSPORT'], + os.environ['NSHOST']]) +# + +def __savePortToFile(args): + # Save Naming service port name into + # the file args["ns_port_log_file"] + if args.has_key('ns_port_log_file'): + omniorbUserPath = os.getenv("OMNIORB_USER_PATH") + file_name = os.path.join(omniorbUserPath, args["ns_port_log_file"]) + with open(file_name, "w") as f: + f.write(os.environ['NSPORT']) +# + +def searchFreePort(args={}, save_config=1, use_port=None): + """ + Search free port for SALOME session. + Returns first found free port number. + """ + try: + import PortManager # mandatory + from multiprocessing import Process, Queue + queue = Queue() + p = Process(target = searchFreePort_withPortManager, args=(queue, args, save_config, use_port,)) + p.start() + info = queue.get() + + os.environ['OMNIORB_CONFIG'] = info[0] + os.environ['NSPORT'] = info[1] + args['port'] = os.environ['NSPORT'] + os.environ['NSHOST'] = info[2] + __savePortToFile(args) + + p.join() # this blocks until the process terminates + except ImportError: + searchFreePort_withoutPortManager(args, save_config, use_port) + __savePortToFile(args) +#