Back to index

salome-kernel  6.5.0
setenv.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #  -*- coding: iso-8859-1 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 import sys, os, string, glob, time, pickle
00026 import orbmodule
00027 from launchConfigureParser import verbose
00028 
00029 # this file is extraction of set_env from runSalome.py
00030 # for reusage in automated tests
00031 
00032 # salome_subdir variable is used for composing paths like $KERNEL_ROOT_DIR/share/salome/resources, etc.
00033 # before moving to SUIT-based gui, instead of salome_subdir there was args['appname'] used.
00034 # but after - 'appname'  = "SalomeApp", so using it in making the subdirectory is an error.
00035 salome_subdir = "salome"
00036 
00037 
00038 # -----------------------------------------------------------------------------
00039 
00040 def add_path(directory, variable_name):
00041     """Function helper to add environment variables"""
00042     if sys.platform == "win32":
00043       splitsym = ";"
00044     else:
00045       splitsym = ":"
00046     if not os.environ.has_key(variable_name):
00047         os.environ[variable_name] = ""
00048         pass
00049     if os.path.exists(directory):
00050         newpath=[]
00051         for _dir in os.environ[variable_name].split(splitsym):
00052             if os.path.exists(_dir):
00053                 if sys.platform != "win32":
00054                     if not os.path.samefile(_dir, directory):
00055                         newpath.append(_dir)
00056                 else:
00057                     newpath.append(_dir)
00058             else:
00059                 if os.path.abspath(_dir) != os.path.abspath(directory):
00060                   newpath.append(_dir)
00061             pass
00062         import string
00063         newpath[:0] = [ directory ]
00064         newpath = string.join(newpath, splitsym)
00065         os.environ[variable_name] = newpath
00066         if variable_name == "PYTHONPATH":
00067             sys.path[:0] = [directory]
00068 
00069 # -----------------------------------------------------------------------------
00070 
00071 __lib__dir__ = None
00072 def get_lib_dir():
00073     global __lib__dir__
00074     if __lib__dir__: return __lib__dir__
00075     import platform
00076     __lib__dir__ = "lib"
00077     return __lib__dir__
00078 
00079 # -----------------------------------------------------------------------------
00080 
00081 def get_config(silent=False):
00082     """
00083     Get list of modules, paths.
00084     
00085     Read args from launch configure xml file and command line options.
00086     Check variables <module>_ROOT_DIR and set list of used modules.
00087     Return args, modules_list, modules_root_dir    
00088     """
00089     
00090     # read args from launch configure xml file and command line options
00091 
00092     #*** Test additional option
00093     #*** import optparse
00094     #*** help_str = "Test options addition."
00095     #*** o_j = optparse.Option("-j", "--join", action="store_true", dest="join", help=help_str)
00096 
00097     import launchConfigureParser
00098     args = launchConfigureParser.get_env()
00099 
00100     #*** Test additional option
00101     #*** args = launchConfigureParser.get_env([o_j])
00102     #*** if args.has_key("join"): print args["join"]
00103 
00104     # Check variables <module>_ROOT_DIR
00105     # and set list of used modules (without KERNEL)
00106 
00107     modules_list = []
00108     if args.has_key("modules"):
00109         modules_list += args["modules"]
00110     # KERNEL must be last in the list to locate it at the first place in PATH
00111     if args["gui"] :
00112         modules_list[:0] = ["GUI"]
00113     modules_list[:0] = ["KERNEL"]
00114     modules_list.reverse()
00115 
00116     modules_root_dir = {}
00117 
00118     to_remove_list=[]
00119     for module in modules_list :
00120         module_variable=module+"_ROOT_DIR"
00121         if not os.environ.has_key(module_variable):
00122             if not silent:
00123                 print "*******************************************************"
00124                 print "*"
00125                 print "* Environment variable",module_variable,"must be set"
00126                 print "* Module", module, "will be not available"
00127                 print "*"
00128                 print "********************************************************"
00129                 pass
00130             to_remove_list.append(module)
00131             continue
00132             pass
00133         module_root_dir = os.environ[module_variable]
00134         modules_root_dir[module]=module_root_dir
00135 
00136     for to_remove in to_remove_list:
00137         modules_list.remove(to_remove)
00138 
00139     while "KERNEL" in modules_list:
00140         modules_list.remove("KERNEL")
00141         pass
00142 
00143     while "GUI" in modules_list:
00144         modules_list.remove("GUI")
00145         pass
00146 
00147     return args, modules_list, modules_root_dir
00148 
00149 # -----------------------------------------------------------------------------
00150 
00151 def set_env(args, modules_list, modules_root_dir, silent=False):
00152     """Add to the PATH-variables modules specific paths"""
00153     
00154     import os
00155     from salome_utils import getTmpDir, generateFileName, makeTmpDir, getPortNumber
00156 
00157     # create temporary directory for environment files needed by modules from the list
00158     port = getPortNumber(False)
00159     if port:
00160         tmp_dir = getTmpDir()
00161         env_dir = generateFileName(tmp_dir, prefix="env", with_port=True)
00162         makeTmpDir(env_dir)
00163         pass
00164 
00165     python_version="python%d.%d" % sys.version_info[0:2]
00166     modules_root_dir_list = []
00167     if os.getenv('SALOME_BATCH') == None:
00168       os.putenv('SALOME_BATCH','0')
00169     if args["gui"] :
00170         modules_list = modules_list[:] + ["GUI"] 
00171     modules_list = modules_list[:] + ["KERNEL"] 
00172     for module in modules_list :
00173         if modules_root_dir.has_key(module):
00174             module_root_dir = modules_root_dir[module]
00175             modules_root_dir_list[:0] = [module_root_dir]
00176             if sys.platform == "win32":
00177               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
00178                      "PATH")
00179             else:
00180               add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
00181                      "LD_LIBRARY_PATH")
00182             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
00183                      "PATH")
00184             if os.path.exists(module_root_dir + "/examples") :
00185                 add_path(os.path.join(module_root_dir,"examples"),
00186                          "PYTHONPATH")
00187                 pass
00188             add_path(os.path.join(module_root_dir,"bin",salome_subdir),
00189                      "PYTHONPATH")
00190             # add lib before site-packages to load script instead of dll if any (win32 platform)
00191             add_path(os.path.join(module_root_dir,get_lib_dir(),salome_subdir),
00192                      "PYTHONPATH")
00193             add_path(os.path.join(module_root_dir,get_lib_dir(),
00194                                   python_version,"site-packages",
00195                                   salome_subdir),
00196                      "PYTHONPATH")
00197             import platform
00198             if platform.machine() == "x86_64":
00199                 add_path(os.path.join(module_root_dir,"lib64",
00200                                       python_version,"site-packages",
00201                                       salome_subdir),
00202                          "PYTHONPATH")
00203                 pass
00204             add_path(os.path.join(module_root_dir,get_lib_dir(),
00205                                   python_version,"site-packages",
00206                                   salome_subdir,
00207                                   "shared_modules"),
00208                      "PYTHONPATH")
00209 
00210             # set environment by modules from the list
00211             if port:
00212                 try:
00213                     mod=__import__(module.lower()+"_setenv")
00214                     mod.set_env(args)
00215                     pass
00216                 except:
00217                     pass
00218                 pass
00219             pass
00220         pass
00221 
00222     if sys.platform == 'win32':
00223         os.environ["SALOMEPATH"]=";".join(modules_root_dir_list)
00224     else:
00225         os.environ["SALOMEPATH"]=":".join(modules_root_dir_list)
00226     
00227     # set trace environment variable
00228     
00229     if not os.environ.has_key("SALOME_trace"):
00230         os.environ["SALOME_trace"]="local"
00231     if args['file']:
00232         os.environ["SALOME_trace"]="file:"+args['file'][0]
00233     if args['logger']:
00234         os.environ["SALOME_trace"]="with_logger"
00235 
00236     # set resources variables if not yet set
00237 
00238     os.environ["CSF_SALOMEDS_ResourcesDefaults"] \
00239     = os.path.join(modules_root_dir["KERNEL"],"share",
00240                    salome_subdir,"resources","kernel")
00241 
00242 # -----------------------------------------------------------------------------
00243 
00244 def main(silent=False):
00245     args, modules_list, modules_root_dir = get_config(silent=silent)
00246     set_env(args, modules_list, modules_root_dir, silent=silent)
00247     return args
00248 
00249 # -----------------------------------------------------------------------------
00250 
00251 if __name__ == "__main__":
00252    import user
00253    args = main()