17def GetValue(val_key,val_default=None,prefix='OST'):
19 Returns the value of the variable val_key if defined, otherwise returns the
20 default value provided by the user (if provided). Search order:
22 * environment variable called $prefix_$val_key
23 * variable called val_key in .ostrc file
26 env_var_name=
'%s_%s' % (prefix, val_key)
29 env_value=os.getenv(env_var_name)
33 main_attr=getattr(__main__, val_key,
None)
52def Locate(file_name, explicit_file_name=None, search_paths=[],
53 env_name=None, search_system_paths=True):
55 Helper function to locate files. To get the full name of an executable, let's
58 .. code-block:: python
60 abs_qmake_path=Locate('qmake', env_name='QMAKE_EXECUTABLE')
62 First the function checks if an environment variable with the name
63 QMAKE_EXECUTABLE is set. If so, the value of this variable is returned. Next,
64 each directory listed in search_paths is searched. If the executable could
65 still not be found and search_system_paths is set to True, the binary search
68 If the file could not be located, a :exc:`~ost.settings.FileNotFound`
69 exception will be raised containing a detail description why Locate failed. The
70 error message is formatted in such a way that it can directly be presented to
73 def _is_executable(filename):
74 return os.path.exists(filename)
and os.access(filename, os.X_OK)
75 if type(file_name)
is str:
76 file_names=[file_name]
79 env_var_inexistent=
'env variable %s points to inexistent file %s'
80 epxl_inexistent=
'explicitly set file "%s" does not exist'
81 set_env_var=
'set the environment variable %s to the absolute path to %s or '
82 if explicit_file_name:
83 if _is_executable(explicit_file_name):
84 return explicit_file_name
86 raise FileNotFound(file_name, epxl_inexistent % explicit_file_name)
88 file_env_name=os.getenv(env_name,
None)
90 if _is_executable(file_env_name):
94 env_var_inexistent % (env_name, file_env_name))
95 searched=list(search_paths)
96 for search_path
in search_paths:
97 for file_name
in file_names:
98 full_file_name=os.path.join(search_path, file_name)
99 if _is_executable(full_file_name):
100 return full_file_name
102 if search_system_paths:
103 paths=os.getenv(
'PATH')
105 searched+=paths.split(
';')
107 searched+=paths.split(
':')
108 for path
in searched:
109 for file_name
in file_names:
110 full_file_name=os.path.join(path, file_name)
111 if _is_executable(full_file_name):
112 return full_file_name
115 msg=
'searched in \n%s\n' % (
'\n'.join([
' - %s' % s
for s
in searched]))
117 msg+=set_env_var % (env_name,
', ' % file_names)
118 msg+=
'put %s into one of the search paths' %
', '.join(file_names)