5 def GetValue(val_key,val_default=None,prefix='OST'):
7 Returns the value of the variable val_key if defined, otherwise returns the
8 default value provided by the user (if provided). Search order:
10 * environment variable called $prefix_$val_key
11 * variable called val_key in .ostrc file
14 env_var_name=
'%s_%s' % (prefix, val_key)
17 env_value=os.getenv(env_var_name)
21 main_attr=getattr(__main__, val_key,
None)
29 Raised when :func:`Locate` is unable to locate a file. The exception contains
30 detailed information on what was tried to locate the file, i.e. search paths,
31 environment variables and also provides useful hints on how to let Locate know
32 where to find the file.
38 return 'Could not find "%s": %s' % (self.
name, self.
reason)
40 def Locate(file_name, explicit_file_name=None, search_paths=[],
41 env_name=
None, search_system_paths=
True):
43 Helper function to locate files. To get the full name of an executable, let's
46 .. code-block:: python
48 abs_qmake_path=Locate('qmake', env_name='QMAKE_EXECUTABLE')
50 First the function checks if an environment variable with the name
51 QMAKE_EXECUTABLE is set. If so, the value of this variable is returned. Next,
52 each directory listed in search_paths is searched. If the executable could
53 still not be found and search_system_paths is set to True, the binary search
56 If the file could not be located, a :exc:`~ost.settings.FileNotFound`
57 exception will be raised containing a detail description why Locate failed. The
58 error message is formatted in such a way that it can directly be presented to
61 def _is_executable(filename):
62 return os.path.exists(filename)
and os.access(filename, os.X_OK)
63 if type(file_name)
is str:
64 file_names=[file_name]
67 env_var_inexistent=
'env variable %s points to inexistent file %s'
68 epxl_inexistent=
'explicitly set file "%s" does not exist'
69 set_env_var=
'set the environment variable %s to the absolute path to %s or '
70 if explicit_file_name:
71 if _is_executable(explicit_file_name):
72 return explicit_file_name
74 raise FileNotFound(file_name, epxl_inexistent % explicit_file_name)
76 file_env_name=os.getenv(env_name,
None)
78 if _is_executable(file_env_name):
82 env_var_inexistent % (env_name, file_env_name))
83 searched=list(search_paths)
84 for search_path
in search_paths:
85 for file_name
in file_names:
86 full_file_name=os.path.join(search_path, file_name)
87 if _is_executable(full_file_name):
90 if search_system_paths:
91 paths=os.getenv(
'PATH')
92 if platform.system() ==
"Windows":
93 searched+=paths.split(
';')
95 searched+=paths.split(
':')
97 for file_name
in file_names:
98 full_file_name=os.path.join(path, file_name)
99 if _is_executable(full_file_name):
100 return full_file_name
103 msg=
'searched in \n%s\n' % (
'\n'.join([
' - %s' % s
for s
in searched]))
105 msg+=set_env_var % (env_name,
', ' % file_names)
106 msg+=
'put %s into one of the search paths' %
', '.join(file_names)