00001 import os, platform
00002 import __main__
00003
00004
00005 def GetValue(val_key,val_default=None,prefix='OST'):
00006 """
00007 Returns the value of the variable val_key if defined, otherwise returns the
00008 default value provided by the user (if provided). Search order:
00009
00010 * environment variable called $prefix_$val_key
00011 * variable called val_key in .ostrc file
00012 """
00013 if prefix:
00014 env_var_name='%s_%s' % (prefix, val_key)
00015 else:
00016 env_var_name=val_key
00017 env_value=os.getenv(env_var_name)
00018 if env_value:
00019 return env_value
00020 else:
00021 main_attr=getattr(__main__, val_key, None)
00022 if main_attr:
00023 return main_attr
00024 else:
00025 return val_default
00026
00027 class FileNotFound(RuntimeError):
00028 """
00029 Raised when :func:`Locate` is unable to locate a file. The exception contains
00030 detailed information on what was tried to locate the file, i.e. search paths,
00031 environment variables and also provides useful hints on how to let Locate know
00032 where to find the file.
00033 """
00034 def __init__(self, name, reason):
00035 self.name=name
00036 self.reason=reason
00037 def __str__(self):
00038 return 'Could not find "%s": %s' % (self.name, self.reason)
00039
00040 def Locate(file_name, explicit_file_name=None, search_paths=[],
00041 env_name=None, search_system_paths=True):
00042 """
00043 Helper function to locate files. To get the full name of an executable, let's
00044 say qmake, use
00045
00046 .. code-block:: python
00047
00048 abs_qmake_path=Locate('qmake', env_name='QMAKE_EXECUTABLE')
00049
00050 First the function checks if an environment variable with the name
00051 QMAKE_EXECUTABLE is set. If so, the value of this variable is returned. Next,
00052 each directory listed in search_paths is searched. If the executable could
00053 still not be found and search_system_paths is set to True, the binary search
00054 paths are searched.
00055
00056 If the file could not be located, a :exc:`~ost.settings.FileNotFound`
00057 exception will be raised containing a detail description why Locate failed. The
00058 error message is formatted in such a way that it can directly be presented to
00059 the user.
00060 """
00061 if type(file_name) is str:
00062 file_names=[file_name]
00063 else:
00064 file_names=file_name
00065 env_var_inexistent='env variable %s points to inexistent file %s'
00066 epxl_inexistent='explicitly set file "%s" does not exist'
00067 set_env_var='set the environment variable %s to the absolute path to %s or '
00068 if explicit_file_name:
00069 if os.path.exists(explicit_file_name):
00070 return explicit_file_name
00071 else:
00072 raise FileNotFound(file_name, epxl_inexistent % explicit_file_name)
00073 if env_name:
00074 file_env_name=os.getenv(env_name, None)
00075 if file_env_name:
00076 if os.path.exists(file_env_name):
00077 return file_env_name
00078 else:
00079 raise FileNotFound(file_name,
00080 env_var_inexistent % (env_name, file_env_name))
00081 searched=list(search_paths)
00082 for search_path in search_paths:
00083 for file_name in file_names:
00084 full_file_name=os.path.join(search_path, file_name)
00085 if os.path.exists(full_file_name):
00086 return full_file_name
00087
00088 if search_system_paths:
00089 paths=os.getenv('PATH')
00090 if platform.system() == "Windows":
00091 searched+=paths.split(';')
00092 else:
00093 searched+=paths.split(':')
00094 for path in searched:
00095 for file_name in file_names:
00096 full_file_name=os.path.join(path, file_name)
00097 if os.path.exists(full_file_name):
00098 return full_file_name
00099 msg=''
00100 if len(searched)>0:
00101 msg='searched in \n%s\n' % ( '\n'.join([' - %s' % s for s in searched]))
00102 if env_name:
00103 msg+=set_env_var % (env_name, ', ' % file_names)
00104 msg+='put %s into one of the search paths' % ', '.join(file_names)
00105 raise FileNotFound(file_name, msg)