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