1 """!This module runs the NCO dbn_alert program, or logs dbn_alert messages
2 if run with dbn alerts disabled."""
33 """!Locates the dbn_alert executable based on environment
34 variables, and returns it as a produtil.prog.Runner object."""
35 global no_DBNROOT_warn
37 if ENV.get(
'DBNROOT',
'')!=
'':
38 return alias(exe(os.path.join(ENV[
'DBNROOT'],
'bin/dbn_alert')))
40 if not no_DBNROOT_warn:
41 log.warning(
"$DBNROOT is not set. Will search for dbn_alert "
44 return alias(exe(
'dbn_alert'))
48 """!This class represents a call to dbn_alert, as a callable Python
49 object. It allows the instructions on how to make the call to be
50 stored for later use by a produtil.datastore.Product object's
51 add_callback and call_callbacks functions."""
52 def __init__(self,args,loglevel=logging.WARNING,alert_exe=None):
53 """!Create a new DBNAlert object that can be used to send an
55 @param args The arguments to dbn_alert.
56 @param alert_exe The dbn_alert executable name.
57 @param loglevel A Python logging level to log messages before each
59 if not isinstance(args,list)
and not isinstance(args,tuple):
60 raise TypeError(
'In DBNAlert(), the first argument must be a list or tuple of arguments to send to dbn_alert')
61 if alert_exe
is not None and not isinstance(alert_exe,basestring) \
62 and not isinstance(alert_exe,Runner):
63 raise TypeError(
'In DBNAlert(), the alert_exe argument must be a string executable name, or a produtil.prog.Runner object')
66 if isinstance(alert_exe,Runner): alert_exe=alias(alert_exe)
80 """!Expands strings specified in the constructor and calls
81 dbn_alert with the results. If dbn alerts are disabled, then
82 the fact that a dbn alert would be run is logged, but
83 dbn_alert is NOT called.
84 @param kwargs string formatting variables for the dbn alert arguments"""
85 assert(job
is not None)
86 kwargs[
'job']=str(job)
87 alert_args=[ s.format(**kwargs)
for s
in self.
alert_args ]
93 log.log(self.
loglevel,
'DBN Alert: '+repr(cmd))
95 log.log(self.
loglevel,
'Exit status %s from dbn_alert.'%(repr(ret), ))
97 log.log(self.
loglevel,
'dbn_alert is disabled')
98 log.log(self.
loglevel,
'would run: dbn_alert '+(
" ".join(alert_args) ))
102 """!Initializes logging for this module. The argument is either a
103 logging.Logger to log to, or the string name of the logging
107 logger=logging.getLogger(
'dbn_alert')
108 elif isinstance(logger,basestring):
109 logger=logging.getLogger(logger)
114 """!Sets the job string (for dbn_alerts) to the specified value,
115 or if unspecified, tries to guess one from the environment."""
117 if jobname
is not None:
128 (name,iid)=(
None,
None)
131 for namevar
in [
'LSB_JOBNAME',
'PBS_JOBNAME',
'MOAB_JOBNAME']:
132 if namevar
in ENV
and ENV[namevar]!=
'':
133 name=os.path.basename(ENV[namevar])
135 if name
is None: name=
'unknown'
138 for iidvar
in [
'LSB_JOBID',
'PBS_JOBID',
'MOAB_JOBID']:
139 if iidvar
in ENV
and ENV[iidvar]!=
'':
141 if iid
is None: iid=str(os.getpid())
143 job=
'LL%s.%s'%(name,iid)
147 """!DBN alert initialization helper function.
149 This is part of the implementation of init_module: it decides
150 whether to send DBNet alerts, and sets the module-scope
151 send_dbn_alerts variable accordingly. That will then be used by
152 DBNAlert objects to decide whether to actually call the dbn_alert
155 @param send_dbn Do we send dbn alerts?"""
156 logger=logging.getLogger(
'dbn_alert')
157 global send_dbn_alerts
159 send_dbn_alerts=
False
161 if send_dbn
is not None and not send_dbn:
162 send_dbn_alerts=send_dbn
165 if 'PARAFLAG' not in ENV:
166 logger.warning(
'PARAFLAG is unset. Disabling DBN alerts.')
167 elif ENV[
'PARAFLAG'].upper()!=
'NO':
168 logger.warning(
'PARAFLAG=%s is not "NO". Disabling DBN alerts.'
170 elif send_dbn
is None:
171 send_dbn_alerts = (
'YES' == ENV.get(
'SENDDBN',
'NO').upper() )
173 logger.warning(
'Enabling dbn_alerts because SENDDBN=YES')
175 logger.warning(
'Disabling dbn_alerts because SENDDBN is not YES')
177 send_dbn_alerts=send_dbn
179 logger.warning(
'Enabling dbn_alerts because the code manually '
180 'turned them on (send_dbn=True)')
182 logger.warning(
'Disabling dbn_alerts because the code manually '
183 'turned them off (send_dbn=False)')
184 if 'DBNROOT' not in ENV:
185 logger.warning(
'DBNROOT is unset. Disabling DBN alerts.')
186 send_dbn_alerts=
False
187 elif ENV.get(
'DBNROOT') ==
'DBNROOT_OFF':
189 'DBNROOT is set to DBNROOT_OFF. Disabling DBN alerts.')
190 send_dbn_alerts=
False
194 """!Call to initialize this module.
196 Initializes the logging and job string for this module.
197 @param logger Either a logging.Logger object to receive log
198 messages, or the string name of a logger domain.
199 @param jobname The dbn_alert job string for this job.
200 @param send_dbn Optional. If specified, this controls whether
201 dbn_alert is actually run (True) or not (False). If unspecified,
202 then the SENDDBN environment variable is used."""
def find_dbn_alert()
Locates the dbn_alert executable based on environment variables, and returns it as a produtil...
loglevel
Desired logging level.
alert_args
Array of arguments to the alert function.
Implements the produtil.run: provides the object tree for representing shell commands.
A shell-like syntax for running serial, MPI and OpenMP programs.
This class represents a call to dbn_alert, as a callable Python object.
def __init__
Create a new DBNAlert object that can be used to send an alert later on.
def __call__(self, kwargs)
Expands strings specified in the constructor and calls dbn_alert with the results.
def init_logging
Initializes logging for this module.
def init_dbn_alert
DBN alert initialization helper function.
def init_jobstring
Sets the job string (for dbn_alerts) to the specified value, or if unspecified, tries to guess one fr...
alert_exe
Alert executable.
def init_module
Call to initialize this module.