Salome HOME
Merge from BR_KERNEL_REFACTORING
[modules/kernel.git] / bin / runSession.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 import os
25 import sys
26 from optparse import OptionParser
27 from NSparam import getNSparams
28
29 # Use to display newlines (\n) in epilog
30 class MyParser(OptionParser):
31     def format_epilog(self, formatter):
32         return self.epilog
33 #
34
35 def configureSession(args=[]):
36   usage = "Usage: %prog [options]"
37   epilog  = """\nIf the command is not given a shell is opened.
38 If PORT and MACHINE are not given, try to connect to the last active session on the local machine.
39 If PORT and MACHINE are given, try to connect to the remote session associated with PORT on MACHINE.
40 If MACHINE is not given, try to connect to the session associated to PORT on the local machine.
41 If PORT is not given, try to connect to the remote session associated to port 2810 on MACHINE.\n\n"""
42   parser = MyParser(usage=usage, epilog=epilog)
43   parser.add_option("-p", "--port", metavar="<port>", default=0,
44                     action="store", type="int", dest="port",
45                     help="The port to connect to."
46                     )
47   parser.add_option("-m", "--machine", metavar="<machine>", default=0,
48                     action="store", type="int", dest="machine",
49                     help="The machine to connect to."
50                     )
51   try:
52     (options, args) = parser.parse_args(args)
53   except Exception, e:
54     print e
55     return
56
57   port = options.port
58   machine = options.machine
59
60   # :GLITCH: this code defines specific environment variables (OMNIORB_CONFIG, NSPORT,
61   # NSHOST) which are later used by other modules. Working, but not really "safe"...
62   if not port:
63     if not machine:
64       # neither MACHINE nor PORT are given
65       # --- set omniORB configuration to current session if any
66       absoluteAppliPath = os.environ['ABSOLUTE_APPLI_PATH']
67       fileOmniConfig = absoluteAppliPath + '/USERS/.omniORB_' + os.environ['USER'] + '_last.cfg'
68       if os.path.isfile(fileOmniConfig):
69         os.environ['OMNIORB_CONFIG'] = fileOmniConfig
70         # --- set environment variables for port and hostname of NamingService
71         host, port = getNSparams()
72         os.environ['NSHOST'] = host
73         os.environ['NSPORT'] = port
74       else:
75         # No running session
76         os.environ['NSHOST'] = "no_host"
77         os.environ['NSPORT'] = "no_port"
78         pass
79     else:
80       # only MACHINE is given
81       _writeConfigFile(2810, os.environ['NSHOST'])
82     #
83   else:
84     if not machine:
85       # only PORT is given
86       os.environ['NSHOST'] = `hostname`
87       pass
88     # both MACHINE and PORT are given
89     _writeConfigFile(os.environ['NSPORT'], os.environ['NSHOST'])
90   #
91 #
92
93 # --- set the OMNIORB_CONFIG file and environment relative to this run of SALOME
94 def _writeConfigFile(port, host):
95   os.environ['NSPORT'] = port
96   os.environ['NSHOST'] = host
97
98   absoluteAppliPath = os.environ['ABSOLUTE_APPLI_PATH']
99   path = absoluteAppliPath + '/USERS'
100   kwargs = {'with_username' : os.environ['USER']}
101
102   from ORBConfigFile import writeORBConfigFile
103   [ filename, msgSize ] = writeORBConfigFile(path, host, port, kwargs)
104
105   os.environ['OMNIORB_CONFIG'] = filename
106 #