OpenStructure
settings.py
Go to the documentation of this file.
1 import os, platform
2 import __main__
3 
4 def GetPlatform():
5  """
6  Returns platform.system(). If the system call is interrupted, it is repeated.
7  This function is a workaround for the buggy system call handling in Python.
8  """
9  system=None
10  while not system:
11  try:
12  system=platform.system()
13  except IOError:
14  pass
15  return system
16 
17 def GetValue(val_key,val_default=None,prefix='OST'):
18  """
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:
21 
22  * environment variable called $prefix_$val_key
23  * variable called val_key in .ostrc file
24  """
25  if prefix:
26  env_var_name='%s_%s' % (prefix, val_key)
27  else:
28  env_var_name=val_key
29  env_value=os.getenv(env_var_name)
30  if env_value:
31  return env_value
32  else:
33  main_attr=getattr(__main__, val_key, None)
34  if main_attr:
35  return main_attr
36  else:
37  return val_default
38 
39 class FileNotFound(RuntimeError):
40  """
41  Raised when :func:`Locate` is unable to locate a file. The exception contains
42  detailed information on what was tried to locate the file, i.e. search paths,
43  environment variables and also provides useful hints on how to let Locate know
44  where to find the file.
45  """
46  def __init__(self, name, reason):
47  self.namename=name
48  self.reasonreason=reason
49  def __str__(self):
50  return 'Could not find "%s": %s' % (self.namename, self.reasonreason)
51 
52 def Locate(file_name, explicit_file_name=None, search_paths=[],
53  env_name=None, search_system_paths=True):
54  """
55  Helper function to locate files. To get the full name of an executable, let's
56  say qmake, use
57 
58  .. code-block:: python
59 
60  abs_qmake_path=Locate('qmake', env_name='QMAKE_EXECUTABLE')
61 
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
66  paths are searched.
67 
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
71  the user.
72  """
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]
77  else:
78  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
85  else:
86  raise FileNotFound(file_name, epxl_inexistent % explicit_file_name)
87  if env_name:
88  file_env_name=os.getenv(env_name, None)
89  if file_env_name:
90  if _is_executable(file_env_name):
91  return file_env_name
92  else:
93  raise FileNotFound(file_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
101 
102  if search_system_paths:
103  paths=os.getenv('PATH')
104  if GetPlatform() == "Windows":
105  searched+=paths.split(';')
106  else:
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
113  msg=''
114  if len(searched)>0:
115  msg='searched in \n%s\n' % ( '\n'.join([' - %s' % s for s in searched]))
116  if env_name:
117  msg+=set_env_var % (env_name, ', ' % file_names)
118  msg+='put %s into one of the search paths' % ', '.join(file_names)
119  raise FileNotFound(file_name, msg)
def __init__(self, name, reason)
Definition: settings.py:46
def Locate(file_name, explicit_file_name=None, search_paths=[], env_name=None, search_system_paths=True)
Definition: settings.py:53
def GetPlatform()
Definition: settings.py:4
def GetValue(val_key, val_default=None, prefix='OST')
Definition: settings.py:17