LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
utils.py
Go to the documentation of this file.
1 ##
2 # @file utils.py
3 #
4 # Internal utilities for sconsUtils.
5 ##
6 
7 import sys
8 import warnings
9 import subprocess
10 import SCons.Script
11 
12 ##
13 # @brief A dead-simple logger for all messages.
14 #
15 # This simply centralizes decisions about whether to throw exceptions or print user-friendly messages
16 # (the traceback variable) and whether to print extra debug info (the verbose variable).
17 # These are set from command-line options in state.py.
18 ##
19 class Log(object):
20 
21  def __init__(self):
22  self.traceback = False
23  self.verbose = True
24 
25  def info(self, message):
26  if self.verbose:
27  print message
28 
29  def warn(self, message):
30  if self.traceback:
31  warnings.warn(message, stacklevel=2)
32  else:
33  sys.stderr.write(message + "\n")
34 
35  def fail(self, message):
36  if self.traceback:
37  raise RuntimeError(message)
38  else:
39  if message:
40  sys.stderr.write(message + "\n")
41  SCons.Script.Exit(1)
42 
43  def flush(self):
44  sys.stderr.flush()
45 
46 ##
47 # @brief Safe wrapper for running external programs, reading stdout, and sanitizing error messages.
48 #
49 # Note that the entire program output is returned, not just a single line.
50 ##
51 def runExternal(cmd, fatal=False, msg=None):
52  if msg is None:
53  try:
54  msg = "Error running %s" % cmd.split()[0]
55  except:
56  msg = "Error running external command"
57  process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
58  stdout, stderr = process.communicate()
59  if process.returncode != 0:
60  if fatal:
61  raise RuntimeError("%s: %s" % (msg, stderr))
62  else:
63  from . import state # can't import at module scope due to circular dependency
64  state.log.warn("%s: %s" % (msg, stderr))
65  return stdout
66 
67 ##
68 # @brief A Python decorator that injects functions into a class.
69 #
70 # For example:
71 # @code
72 # class test_class(object):
73 # pass
74 #
75 # @memberOf(test_class):
76 # def test_method(self):
77 # print "test_method!"
78 # @endcode
79 # ...will cause test_method to appear as as if it were defined within test_class.
80 #
81 # The function or method will still be added to the module scope as well, replacing any
82 # existing module-scope function with that name; this appears to be unavoidable.
83 ##
84 def memberOf(cls, name=None):
85  if isinstance(cls, type):
86  classes = (cls,)
87  else:
88  classes = tuple(cls)
89  kw = {"name": name}
90  def nested(member):
91  if kw["name"] is None: kw["name"] = member.__name__
92  for scope in classes:
93  setattr(scope, kw["name"], member)
94  return member
95  return nested
96 
def memberOf
A Python decorator that injects functions into a class.
Definition: utils.py:84
A dead-simple logger for all messages.
Definition: utils.py:19
def runExternal
Safe wrapper for running external programs, reading stdout, and sanitizing error messages.
Definition: utils.py:51