3Defines the base interface that component classes should extend.
11from ._config
import convert_config_value
12from hpsmc
import global_config
17 Base class for components in a job.
19 Do not perform any logging in the init method of Component subclasses,
20 as this is not configured by the job manager until after the components
23 Optional parameters are: **nevents**, **seed**
25 @param name name of the component
26 @param command command to execute
27 @param nevents number of events to process
28 @param seed random seed
29 @param inputs list of input files
30 @param outputs list of output files
31 @param append_tok token to append to output file names
32 @param output_ext extension to append to output file names; format is .ext
33 @param ignore_job_params list of parameters to ignore when setting parameters
34 @param kwargs additional keyword arguments
65 raise Exception(
"The HPSMC_DIR is not set!")
69 "{}.{}".format(__name__, self.__class__.__name__)
79 def execute(self, log_out=sys.stdout, log_err=sys.stderr):
80 """! Generic component execution method.
82 Individual components may override this if specific behavior is required.
84 @param log_out name of log file for output
85 @param log_err name of log file for error
88 proc = subprocess.Popen(
89 self.
cmd_line_str(), shell=
True, stdout=log_out, stderr=log_err
94 return proc.returncode
97 """! Check if the component's assigned command exists."""
102 stdout=subprocess.PIPE,
103 stderr=subprocess.PIPE,
109 """! Return the command arguments of this component."""
113 """! Return list of arguments, making sure they are all converted to strings."""
114 return [str(c)
for c
in self.
cmd_args()]
117 """! Perform any necessary setup for this component to run such as making symlinks
118 to required directories.
123 """! Perform post-job cleanup such as deleting temporary files."""
128 Configure the logging for a component.
130 @param parser the ConfigParser object passed from the job manager
132 classname = self.__class__.__name__
133 if classname
in parser:
134 if "loglevel" in parser[classname]:
135 loglevel = logging.getLevelName(parser[classname][
"loglevel"])
136 self.logger.setLevel(loglevel)
139 """! Automatic configuration
141 Automatically load attributes from config by reading in values from
142 the section with the same name as the class in the config file and
143 assigning them to class attributes with the same name.
145 @param parser config parser
147 section_name = self.__class__.__name__
148 if parser.has_section(section_name):
149 for name, value
in parser.items(section_name):
150 setattr(self, name, convert_config_value(value))
156 getattr(self, name).__class__.__name__,
162 """! Set class attributes for the component based on JSON parameters.
164 Components should not need to override this method.
166 @param params parameters to setup component
173 "Required parameter '%s' is missing for component '%s'"
178 setattr(self, p, params[p])
179 self.
logger.debug(
"%s:%s=%s [required]" % (self.
name, p, params[p]))
181 self.
logger.debug(
"Ignored job param '%s'" % p)
187 setattr(self, p, params[p])
188 self.
logger.debug(
"%s:%s=%s [optional]" % (self.
name, p, params[p]))
190 self.
logger.debug(
"Ignored job param '%s'" % p)
194 Return a list of required parameters.
196 The job will fail if these are not present in the JSON file.
202 Return a list of optional parameters.
204 Optional parameters are: **nevents**, **seed**
206 return [
"nevents",
"seed"]
210 Return a list of required configuration settings.
212 There are none by default.
217 """! Raise an exception on the first missing config setting for this component."""
219 if not hasattr(self, c):
221 "Missing required config attribute: %s:%s"
222 % (self.__class__.__name__, c)
224 if getattr(self, c)
is None:
226 "Config was not set: %s:%s" % (self.__class__.__name__, c)
230 """! Get a list of input files for this component."""
234 """! Return a list of output files created by this component.
236 By default, a series of transformations will be performed on inputs to
237 transform them into outputs.
245 """! This is the default method for automatically transforming input file names
246 to outputs when output file names are not explicitly provided.
250 f, ext = os.path.splitext(infile)
255 outputs.append(
"%s%s" % (f, ext))
259 """! Configure component from environment variables which are just upper case
260 versions of the required config names set in the shell environment."""
262 self.
logger.debug(
"Setting config '%s' from environ" % c)
263 if c.upper()
in os.environ:
264 setattr(self, c, os.environ[c.upper()])
266 "Set config '%s=%s' from env var '%s'"
267 % (c, getattr(self, c), c.upper())
270 raise Exception(
"Missing config in environ for '%s'" % c)
274 """! A dummy component that just prints some information instead of executing a program."""
277 Component.__init__(self,
"dummy",
"dummy", **kwargs)
279 def execute(self, log_out=sys.stdout, log_err=sys.stderr):
280 self.
logger.debug(
"dummy debug")
281 self.
logger.info(
"dummy info")
282 self.
logger.warning(
"dummy warn")
283 self.
logger.critical(
"dummy critical")
284 self.
logger.error(
"dummy error")
Base class for components in a job.
output_files(self)
Return a list of output files created by this component.
execute(self, log_out=sys.stdout, log_err=sys.stderr)
Generic component execution method.
optional_parameters(self)
Return a list of optional parameters.
cleanup(self)
Perform post-job cleanup such as deleting temporary files.
config_logging(self, parser)
Configure the logging for a component.
config_from_environ(self)
Configure component from environment variables which are just upper case versions of the required con...
cmd_args_str(self)
Return list of arguments, making sure they are all converted to strings.
required_parameters(self)
Return a list of required parameters.
setup(self)
Perform any necessary setup for this component to run such as making symlinks to required directories...
cmd_exists(self)
Check if the component's assigned command exists.
set_parameters(self, params)
Set class attributes for the component based on JSON parameters.
config(self, parser)
Automatic configuration.
required_config(self)
Return a list of required configuration settings.
_inputs_to_outputs(self)
This is the default method for automatically transforming input file names to outputs when output fil...
cmd_args(self)
Return the command arguments of this component.
check_config(self)
Raise an exception on the first missing config setting for this component.
input_files(self)
Get a list of input files for this component.
__init__(self, name, command=None, nevents=None, seed=1, inputs=[], outputs=None, append_tok=None, output_ext=None, ignore_job_params=[], **kwargs)
A dummy component that just prints some information instead of executing a program.
execute(self, log_out=sys.stdout, log_err=sys.stderr)
Generic component execution method.