Salome HOME
Merge branch 'rbe/evol-job-newparams'
[modules/kernel.git] / bin / searchFreePort.py
index 83e52873453451133abbe2a643dcc7f6344e4af3..9b14fabdb12a3a28e84a361f99a9f550abe2c6dc 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2013  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
@@ -123,7 +123,7 @@ def searchFreePort_withoutPortManager(args={}, save_config=1, use_port=None):
   #
 #
 
-def searchFreePort_withPortManager(args={}, save_config=1, use_port=None):
+def searchFreePort_withPortManager(queue, args={}, save_config=1, use_port=None):
   from PortManager import getPort
   port = getPort(use_port)
 
@@ -146,6 +146,20 @@ def searchFreePort_withPortManager(args={}, save_config=1, use_port=None):
     __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):
@@ -154,8 +168,21 @@ def searchFreePort(args={}, save_config=1, use_port=None):
   Returns first found free port number.
   """
   try:
-    import PortManager
-    searchFreePort_withPortManager(args, save_config, use_port)
+    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)
 #