Salome HOME
Windows fix
[modules/kernel.git] / bin / salomeLauncherUtils.py
index 5a59bb1786fec27df3e592e0a0d0936b56f377ef..33d867d7fbfc8ef12f63ef1f8b4c28a4d6f4fe47 100644 (file)
@@ -4,6 +4,7 @@ import os
 import sys
 import glob
 import subprocess
+import re
 
 """
 Define a specific exception class to manage exceptions related to SalomeRunner
@@ -32,7 +33,7 @@ def __getConfigFileNamesDefault():
   return __listDirectory(envdDir)
 #
 
-def getConfigFileNames(args):
+def getConfigFileNames(args, checkExistence=False):
   # special case: configuration files are provided by user
   # Search for command-line argument(s) --config=file1,file2,..., filen
   # Search for command-line argument(s) --config=dir1,dir2,..., dirn
@@ -40,12 +41,13 @@ def getConfigFileNames(args):
   configArgs = [ str(x) for x in args if str(x).startswith(configOptionPrefix) ]
 
   if len(configArgs) == 0:
-    return __getConfigFileNamesDefault(), args
+    return __getConfigFileNamesDefault(), args, []
 
   args = [ x for x in args if not x.startswith(configOptionPrefix) ]
   allLists = [ x.replace(configOptionPrefix, '') for x in configArgs ]
 
   configFileNames = []
+  unexisting = []
   for currentList in allLists:
     elements = currentList.split(',')
     for elt in elements:
@@ -53,9 +55,12 @@ def getConfigFileNames(args):
       if os.path.isdir(elt):
         configFileNames += __listDirectory(elt)
       else:
-        configFileNames += [elt]
+        if checkExistence and not os.path.isfile(elt):
+          unexisting += [elt]
+        else:
+          configFileNames += [elt]
 
-  return configFileNames, args
+  return configFileNames, args, unexisting
 #
 
 # Return an array of dictionaries {script_name: [list_of_its_args]}
@@ -65,6 +70,7 @@ def getScriptsAndArgs(args=[]):
   currentKey = None
   argsPrefix = "args:"
   callPython = False
+  currentScript = None
 
   for i in range(len(args)):
     elt = args[i]
@@ -79,12 +85,14 @@ def getScriptsAndArgs(args=[]):
     elif elt.startswith("python"):
       callPython = True
     elif os.path.isfile(elt) or os.path.isfile(elt+".py"):
-      if elt[-3:] == ".py":
-        currentScript = os.path.abspath(elt)
-      else:
-        currentScript = None
-        if elt[-4:] != ".hdf":
+      if elt[-4:] != ".hdf":
+        if elt[-3:] == ".py":
+          currentScript = os.path.abspath(elt)
+        elif os.path.isfile(elt+".py"):
           currentScript = os.path.abspath(elt+".py")
+        else:
+          currentScript = os.path.abspath(elt) # python script not necessary has .py extension
+        pass
       if currentScript and callPython:
         currentKey = "python "+currentScript
         scriptArgs.append({currentKey:[]})
@@ -94,7 +102,23 @@ def getScriptsAndArgs(args=[]):
           currentKey = "python "+currentScript
           scriptArgs.append({currentKey:[]})
         else:
-          currentKey = currentScript
+          ispython = False
+          try:
+            fn = open(currentScript)
+            for i in xrange(10): # read only 10 first lines 
+              ln = fn.readline()
+              if re.search("#!.*python"): 
+                ispython = True
+                break
+              pass
+            fn.close()
+          except:
+            pass
+          if not ispython and currentScript[-3:] == ".py":
+            currentKey = "python "+currentScript
+          else:
+            currentKey = currentScript
+            pass
           scriptArgs.append({currentKey:[]})
   # end for loop
   return scriptArgs
@@ -114,3 +138,29 @@ def formatScriptsAndArgs(scriptArgs=[]):
     command = "; ".join(["%s"%x for x in commands])
     return command
 #
+
+# Ensure OMNIORB_USER_PATH is defined. This variable refers to a the folder in which
+# SALOME will write omniOrb configuration files.
+# If OMNIORB_USER_PATH is already set, only checks write access to associated directory ;
+# an exception is raised if check fails. It allows users for choosing a specific folder.
+# Else the function sets OMNIORB_USER_PATH this way:
+# - If APPLI environment variable is set, OMNIORB_USER_PATH is set to ${APPLI}/USERS.
+#   The function does not check USERS folder existence or wrute access. This folder
+#   must exist ; this is the case if SALOME virtual application has been create using
+#   appli_gen.py script.
+# - Else OMNIORB_USER_PATH is set to user home directory.
+def setOmniOrbUserPath():
+  omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
+  if omniorbUserPath:
+    if not os.access(omniorbUserPath, os.W_OK):
+      raise Exception("Unable to get write access to directory: %s"%omniorbUserPath)
+    pass
+  else:
+    homePath = os.path.realpath(os.path.expanduser('~'))
+    #defaultOmniorbUserPath = os.path.join(homePath, ".salomeConfig/USERS")
+    defaultOmniorbUserPath = homePath
+    if os.getenv("APPLI"):
+      defaultOmniorbUserPath = os.path.join(homePath, os.getenv("APPLI"), "USERS")
+      pass
+    os.environ["OMNIORB_USER_PATH"] = defaultOmniorbUserPath
+#