OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
settings.py
Go to the documentation of this file.
1 import os, platform
2 import __main__
3 
4 
5 def GetValue(val_key,val_default=None,prefix='OST'):
6  """
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:
9 
10  * environment variable called $prefix_$val_key
11  * variable called val_key in .ostrc file
12  """
13  if prefix:
14  env_var_name='%s_%s' % (prefix, val_key)
15  else:
16  env_var_name=val_key
17  env_value=os.getenv(env_var_name)
18  if env_value:
19  return env_value
20  else:
21  main_attr=getattr(__main__, val_key, None)
22  if main_attr:
23  return main_attr
24  else:
25  return val_default
26 
27 class FileNotFound(RuntimeError):
28  """
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.
33  """
34  def __init__(self, name, reason):
35  self.name=name
36  self.reason=reason
37  def __str__(self):
38  return 'Could not find "%s": %s' % (self.name, self.reason)
39 
40 def Locate(file_name, explicit_file_name=None, search_paths=[],
41  env_name=None, search_system_paths=True):
42  """
43  Helper function to locate files. To get the full name of an executable, let's
44  say qmake, use
45 
46  .. code-block:: python
47 
48  abs_qmake_path=Locate('qmake', env_name='QMAKE_EXECUTABLE')
49 
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
54  paths are searched.
55 
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
59  the user.
60  """
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]
65  else:
66  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
73  else:
74  raise FileNotFound(file_name, epxl_inexistent % explicit_file_name)
75  if env_name:
76  file_env_name=os.getenv(env_name, None)
77  if file_env_name:
78  if _is_executable(file_env_name):
79  return file_env_name
80  else:
81  raise FileNotFound(file_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):
88  return full_file_name
89 
90  if search_system_paths:
91  paths=os.getenv('PATH')
92  if platform.system() == "Windows":
93  searched+=paths.split(';')
94  else:
95  searched+=paths.split(':')
96  for path in searched:
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
101  msg=''
102  if len(searched)>0:
103  msg='searched in \n%s\n' % ( '\n'.join([' - %s' % s for s in searched]))
104  if env_name:
105  msg+=set_env_var % (env_name, ', ' % file_names)
106  msg+='put %s into one of the search paths' % ', '.join(file_names)
107  raise FileNotFound(file_name, msg)