Salome HOME
ae57dae12f78c471fa4f96f7d4eaad80e01822ac
[modules/kernel.git] / bin / appliskel / salome
1 #! /usr/bin/env python
2
3 # Copyright (C) 2013-2014  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 import os
23 import sys
24
25 def __detectAppliPath(fromPath):
26   detection_criterion = "USERS" # the application folder is found if it contains a USERS subfolder
27
28   users_folder = os.path.join(fromPath, detection_criterion)
29   if os.path.isdir(users_folder):
30     return fromPath
31
32   pattern = "/bin/salome/appliskel"
33   if fromPath.endswith(pattern):
34     currentPath = __detectAppliPath(fromPath[:-len(pattern)])
35     if not currentPath is None:
36       return currentPath
37
38   if sys.platform.startswith("linux"):
39     filename = os.path.basename(__file__)
40     link_target = os.readlink(os.path.join(fromPath,filename)) # LINUX ONLY
41     currentPath = os.path.dirname(os.path.abspath(link_target))
42     return __detectAppliPath(currentPath)
43
44   return None
45 #
46
47 # Preliminary work to initialize path to SALOME Python modules
48 def __initialize():
49   currentPath = os.path.dirname( os.path.abspath( __file__ ) )
50   homePath = os.path.realpath(os.path.expanduser('~'))
51
52   appliPath = __detectAppliPath(currentPath)
53
54   if appliPath is None:
55     print "ERROR: Unable to find application folder"
56     sys.exit(0)
57
58   appliPath = os.path.relpath(appliPath, homePath)
59   absoluteAppliPath = os.path.join(homePath, appliPath)
60   os.environ['APPLI'] = appliPath # needed to convert .sh environment files
61   os.environ['ABSOLUTE_APPLI_PATH'] = absoluteAppliPath
62
63   sys.path[:0] = [absoluteAppliPath+'/bin/salome']
64
65   # define folder to store omniorb config (initially in virtual application folder)
66   try:
67     from salomeContextUtils import setOmniOrbUserPath
68     setOmniOrbUserPath()
69   except Exception, e:
70     print e
71     sys.exit(1)
72 # End of preliminary work
73
74 def main(args):
75   # Identify application path then locate configuration files
76   __initialize()
77
78   if args == ['--help']:
79     from salomeContext import usage
80     usage()
81     sys.exit(0)
82
83   from salomeContextUtils import getConfigFileNames
84   configFileNames, args, unexisting = getConfigFileNames(args, checkExistence=True)
85   if len(unexisting) > 0:
86     print "ERROR: unexisting configuration file(s): " + ', '.join(unexisting)
87     sys.exit(1)
88
89   # Create a SalomeContext which parses configFileNames to initialize environment
90   try:
91     from salomeContext import SalomeContext, SalomeContextException
92     context = SalomeContext(configFileNames)
93
94     # Here set specific variables, if needed
95     # context.addToPath('mypath')
96     # context.addToLdLibraryPath('myldlibrarypath')
97     # context.addToPythonPath('mypythonpath')
98     # context.setVariable('myvarname', 'value')
99
100     # Start SALOME, parsing command line arguments
101     context.runSalome(args)
102     #print 'Thank you for using SALOME!'
103
104   except SalomeContextException, e:
105     import logging
106     logging.getLogger("salome").error(e)
107     sys.exit(1)
108 #
109
110 if __name__ == "__main__":
111   args = sys.argv[1:]
112   main(args)
113 #