LSSTApplications  16.0-10-g0ee56ad+4,16.0-11-ga33d1f2+4,16.0-12-g3ef5c14+2,16.0-12-g71e5ef5+17,16.0-12-gbdf3636+2,16.0-13-g118c103+2,16.0-13-g8f68b0a+2,16.0-15-gbf5c1cb+3,16.0-16-gfd17674+2,16.0-17-g7c01f5c+2,16.0-18-g0a50484,16.0-20-ga20f992+7,16.0-21-g0e05fd4+5,16.0-21-g15e2d33+3,16.0-22-g62d8060+3,16.0-22-g847a80f+3,16.0-25-gf00d9b8,16.0-28-g3990c221+3,16.0-3-gf928089+2,16.0-32-g88a4f23+4,16.0-34-gd7987ad+2,16.0-37-gc7333cb+1,16.0-4-g10fc685+1,16.0-4-g18f3627+25,16.0-4-g5f3a788+25,16.0-5-gaf5c3d7+3,16.0-5-gcc1f4bb,16.0-6-g3b92700+3,16.0-6-g4412fcd+2,16.0-6-g7235603+3,16.0-69-g2562ce1b+1,16.0-7-g0913a87,16.0-8-g14ebd58+3,16.0-8-g2df868b,16.0-8-g4cec79c+5,16.0-8-gadf6c7a,16.0-82-g59ec2a54a,16.0-9-g5400cdc+1,16.0-9-ge6233d7+4,master-g2880f2d8cf+2,v17.0.rc1
LSSTDataManagementBasePackage
utils.py
Go to the documentation of this file.
1 
6 
7 import os
8 import sys
9 import warnings
10 import subprocess
11 import platform
12 import SCons.Script
13 
14 
15 
22 class Log:
23 
24  def __init__(self):
25  self.traceback = False
26  self.verbose = True
27 
28  def info(self, message):
29  if self.verbose:
30  print(message)
31 
32  def warn(self, message):
33  if self.traceback:
34  warnings.warn(message, stacklevel=2)
35  else:
36  print(message, file=sys.stderr)
37 
38  def fail(self, message):
39  if self.traceback:
40  raise RuntimeError(message)
41  else:
42  if message:
43  print(message, file=sys.stderr)
44  SCons.Script.Exit(1)
45 
46  def flush(self):
47  sys.stderr.flush()
48 
49 
50 
54 def _has_OSX_SIP():
55  hasSIP = False
56  os_platform = SCons.Platform.platform_default()
57  # SIP is enabled on OS X >=10.11 equivalent to darwin >= 15
58  if os_platform == 'darwin':
59  release_str = platform.release()
60  release_major = int(release_str.split('.')[0])
61  if release_major >= 15:
62  hasSIP = True
63  return hasSIP
64 
65 
66 
71  if _has_OSX_SIP():
72  return "DYLD_LIBRARY_PATH"
73  return None
74 
75 
76 # Cache variable for whichPython() function
77 _pythonPath = None
78 
79 
80 
88  global _pythonPath
89  if _pythonPath is None:
90  _pythonPath = runExternal(["python", "-c", "import sys; print(sys.executable)"],
91  fatal=True, msg="Error getting python path")
92  return _pythonPath
93 
94 
95 
99  return _has_OSX_SIP()
100 
101 
102 
117  libpathstr = ""
118  lib_pass_through_var = libraryPathPassThrough()
119  aux_pass_through_var = "LSST_LIBRARY_PATH"
120  if lib_pass_through_var is not None:
121  for varname in (lib_pass_through_var, aux_pass_through_var):
122  if varname in os.environ:
123  libpathstr += '{}="{}" '.format(varname, os.environ[varname])
124 
125  if aux_pass_through_var in os.environ and \
126  lib_pass_through_var not in os.environ:
127  libpathstr += '{}="{}" '.format(lib_pass_through_var, os.environ[aux_pass_through_var])
128 
129  if lib_pass_through_var in os.environ and \
130  aux_pass_through_var not in os.environ:
131  libpathstr += '{}="{}" '.format(aux_pass_through_var, os.environ[lib_pass_through_var])
132 
133  return libpathstr
134 
135 
136 
144 def runExternal(cmd, fatal=False, msg=None):
145  if msg is None:
146  try:
147  msg = "Error running %s" % cmd.split()[0]
148  except Exception:
149  msg = "Error running external command"
150 
151  # Run with shell unless given a list of options
152  shell = True
153  if isinstance(cmd, (list, tuple)):
154  shell = False
155 
156  try:
157  retval = subprocess.run(cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
158  check=True)
159  except subprocess.CalledProcessError as e:
160  if fatal:
161  raise RuntimeError(f"{msg}: {e.stderr.decode()}") from e
162  else:
163  from . import state # can't import at module scope due to circular dependency
164  state.log.warn(f"{msg}: {e.stderr}")
165  return retval.stdout.decode().strip()
166 
167 
168 
185 def memberOf(cls, name=None):
186  if isinstance(cls, type):
187  classes = (cls,)
188  else:
189  classes = tuple(cls)
190  kw = {"name": name}
191 
192  def nested(member):
193  if kw["name"] is None:
194  kw["name"] = member.__name__
195  for scope in classes:
196  setattr(scope, kw["name"], member)
197  return member
198  return nested
def warn(self, message)
Definition: utils.py:32
def libraryLoaderEnvironment()
Returns library loader path environment string to be prepended to external commands Will be "" if not...
Definition: utils.py:116
def needShebangRewrite()
Returns True if the shebang lines of executables should be rewritten.
Definition: utils.py:98
def libraryPathPassThrough()
Returns name of library path environment variable to be passed through or else returns None if no pas...
Definition: utils.py:70
def whichPython()
Returns the full path to the Python executable as determined from the PATH.
Definition: utils.py:87
A dead-simple logger for all messages.
Definition: utils.py:22
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
def runExternal(cmd, fatal=False, msg=None)
Safe wrapper for running external programs, reading stdout, and sanitizing error messages.
Definition: utils.py:144
def info(self, message)
Definition: utils.py:28
def fail(self, message)
Definition: utils.py:38
def __init__(self)
Definition: utils.py:24
bool strip
Definition: fits.cc:831
table::Key< int > nested
def memberOf(cls, name=None)
A Python decorator that injects functions into a class.
Definition: utils.py:185