Salome HOME
b58a02b32bb6e16cf707706f4888ad9ad2a0054f
[modules/kernel.git] / bin / appliskel / salome_starter.py
1 # Copyright (C) 2014-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 import os
21 import sys
22
23 # This file is imported by launchers to help with application folder detection.
24 # Import is possible because Python automatically add to sys.path the folder of
25 # a running script (here a launcher).
26
27 def __detectAppliPath(fromPath, launcherFile):
28   detection_criterion = "USERS" # the application folder is found if it contains a USERS subfolder
29
30   users_folder = os.path.join(fromPath, detection_criterion)
31   if os.path.isdir(users_folder):
32     return fromPath
33
34   pattern = os.path.sep + os.path.join("bin", "salome", "appliskel")
35   if fromPath.endswith(pattern):
36     currentPath = __detectAppliPath(fromPath[:-len(pattern)], launcherFile)
37     if not currentPath is None:
38       return currentPath
39
40   if sys.platform.startswith("linux"):
41     link_target = os.readlink(os.path.join(fromPath,launcherFile)) # LINUX ONLY
42     currentPath = os.path.dirname(os.path.abspath(link_target))
43     return __detectAppliPath(currentPath, launcherFile)
44
45   return None
46 #
47
48 # Preliminary work to initialize path to SALOME Python modules
49 def initialize(launcherPath, launcherFile):
50   homePath = os.path.realpath(os.path.expanduser('~'))
51   appliPath = __detectAppliPath(launcherPath, launcherFile)
52
53   if appliPath is None:
54     print("ERROR: Unable to find application folder")
55     sys.exit(1)
56
57   appliPath = os.path.relpath(appliPath, homePath)
58   absoluteAppliPath = os.path.join(homePath, appliPath)
59   os.environ['APPLI'] = appliPath # needed to convert .sh environment files
60   os.environ['ABSOLUTE_APPLI_PATH'] = absoluteAppliPath
61
62   sys.path[:0] = [os.path.realpath(os.path.join(absoluteAppliPath, "bin", "salome", "test"))]
63   sys.path[:0] = [os.path.realpath(os.path.join(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 as e:
70     print(e)
71     sys.exit(1)
72 # End of preliminary work