Salome HOME
Porting to Python 2.6 - add coding page specification for Python scripts
[modules/kernel.git] / bin / launchSalome.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 #  This library is free software; you can redistribute it and/or
9 #  modify it under the terms of the GNU Lesser General Public
10 #  License as published by the Free Software Foundation; either
11 #  version 2.1 of the License.
12 #
13 #  This library is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 #  Lesser General Public License for more details.
17 #
18 #  You should have received a copy of the GNU Lesser General Public
19 #  License along with this library; if not, write to the Free Software
20 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24 import socket
25 import os
26 import sys
27 import shutil
28
29 #####################################################################
30 def findFreePort(startPort, step) :
31
32     currentPort = startPort;
33     if step < 1:
34         step = 1;
35
36     serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
37
38     portFound = 0;
39     while (portFound != 1 and currentPort < 65536):
40         try :
41             serversocket.bind((socket.gethostname(), currentPort));
42             portFound = 1;
43             print str(currentPort) + ": " + "OK";
44 #            serversocket.shutdown(0);
45 #            serversocket.close();
46         except Exception, inst:
47             print str(currentPort) + ": " + str(inst.args);
48             currentPort = currentPort+step;
49     
50     
51     
52     
53     if (portFound != 1) :
54         currentPort = -1;
55     return currentPort;
56 #####################################################################
57
58
59
60 ## 1. Generate config file for omniORB:
61 # content:
62 # InitRef = NameService=corbaname::[HOST_NAME]:[PORT_NUMBER]
63
64 freePort = findFreePort(2810, 1);
65 hostName = socket.gethostname();
66
67 if (len(sys.argv) > 1) and sys.argv[1] == "-nothing" :
68     print "port:" + str(freePort);
69     sys.exit(0)
70     
71
72         
73
74 if freePort < 0 :
75     print
76     print "Can't find a free port to launch omniNames"
77     print "Try to kill the running servers and then launch SALOME again."
78     print
79     sys.exit(0)
80     
81 else :
82     omniCfgFileContent = "InitRef = NameService=corbaname::" + str(hostName) + ":" + str(freePort);
83     omniCfgFilePath = os.environ.get('HOME') + "/" + ".omniORB_" + str(hostName) + "_" + str(freePort) + ".cfg";
84
85     omni_file = file(omniCfgFilePath,'w+');
86     omni_file.write(omniCfgFileContent);
87     omni_file.write("\n");
88     omni_file.close();
89
90     if (len(sys.argv) > 1) and sys.argv[1] == "--save-config" :
91         omniCfgCurrent = os.environ.get('HOME') + "/.omniORB_current.cfg";
92         if os.name == "posix" :
93             #make a symbolic link
94             symlink(omniCfgFilePath, omniCfgCurrent);
95         else :
96             #copy the file
97             shutil.copy(omniCfgFilePath, omniCfgCurrent);
98             
99         omniCfgFilePath = omniCfgCurrent;
100             
101     
102     
103
104     ## 2. Set the OMNIORB_CONFIG environment variable
105     os.environ['OMNIORB_CONFIG']=omniCfgFilePath;
106
107
108     ## 3. execute the file runSalome.py
109     runSalomePyFilePath = os.environ.get('KERNEL_ROOT_DIR') + "/bin/salome/runSalome.py";
110
111     if os.path.isfile(runSalomePyFilePath) :
112         execfile(runSalomePyFilePath);
113