Salome HOME
add a runSalome.bat for windows
[modules/kernel.git] / bin / launchSalome.py
1 #!/usr/bin/env python
2 #  Copyright (C) 2007-2008  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 import socket
24 import os
25 import sys
26 import shutil
27
28 #####################################################################
29 def findFreePort(startPort, step) :
30
31     currentPort = startPort;
32     if step < 1:
33         step = 1;
34
35     serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
36
37     portFound = 0;
38     while (portFound != 1 and currentPort < 65536):
39         try :
40             serversocket.bind((socket.gethostname(), currentPort));
41             portFound = 1;
42             print str(currentPort) + ": " + "OK";
43 #            serversocket.shutdown(0);
44 #            serversocket.close();
45         except Exception, inst:
46             print str(currentPort) + ": " + str(inst.args);
47             currentPort = currentPort+step;
48     
49     
50     
51     
52     if (portFound != 1) :
53         currentPort = -1;
54     return currentPort;
55 #####################################################################
56
57
58
59 ## 1. Generate config file for omniORB:
60 # content:
61 # InitRef = NameService=corbaname::[HOST_NAME]:[PORT_NUMBER]
62
63 freePort = findFreePort(2810, 1);
64 hostName = socket.gethostname();
65
66 if (len(sys.argv) > 1) and sys.argv[1] == "-nothing" :
67     print "port:" + str(freePort);
68     sys.exit(0)
69     
70
71         
72
73 if freePort < 0 :
74     print
75     print "Can't find a free port to launch omniNames"
76     print "Try to kill the running servers and then launch SALOME again."
77     print
78     sys.exit(0)
79     
80 else :
81     omniCfgFileContent = "InitRef = NameService=corbaname::" + str(hostName) + ":" + str(freePort);
82     omniCfgFilePath = os.environ.get('HOME') + "/" + ".omniORB_" + str(hostName) + "_" + str(freePort) + ".cfg";
83
84     omni_file = file(omniCfgFilePath,'w+');
85     omni_file.write(omniCfgFileContent);
86     omni_file.write("\n");
87     omni_file.close();
88
89     if (len(sys.argv) > 1) and sys.argv[1] == "--save-config" :
90         omniCfgCurrent = os.environ.get('HOME') + "/.omniORB_current.cfg";
91         if os.name == "posix" :
92             #make a symbolic link
93             symlink(omniCfgFilePath, omniCfgCurrent);
94         else :
95             #copy the file
96             shutil.copy(omniCfgFilePath, omniCfgCurrent);
97             
98         omniCfgFilePath = omniCfgCurrent;
99             
100     
101     
102
103     ## 2. Set the OMNIORB_CONFIG environment variable
104     os.environ['OMNIORB_CONFIG']=omniCfgFilePath;
105
106
107     ## 3. execute the file runSalome.py
108     runSalomePyFilePath = os.environ.get('KERNEL_ROOT_DIR') + "/bin/salome/runSalome.py";
109
110     if os.path.isfile(runSalomePyFilePath) :
111         execfile(runSalomePyFilePath);
112