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