1 """! Utilities for writing config files for jobs from environment variables."""
12 def write_config(filename, component_names, include_defaults, fail_on_missing):
13 """! Write a config file from environment variables.
14 @param filename name of config file
15 @param component_names list of components in job
16 @param include_defaults set true if defaults should be included
17 @param fail_on_missing if true: method fails if environment variable is missing
18 if false: throws warning if environment variable is missing"""
22 for k
in sorted(globals().keys()):
24 if isinstance(v, Component.__class__):
25 if v.__name__
not in _ignore:
26 if len(component_names)
and v.__name__
not in component_names:
30 obj = eval(v.__name__)()
31 section =
'[%s]\n' % v.__name__
33 for c
in obj.required_config():
35 if env_var
in os.environ:
36 s =
'%s = %s\n' % (c, os.environ[env_var])
39 raise Exception(
'Missing env var: %s' % env_var)
41 print(
"WARNING: Missing env var: %s" % env_var)
43 with open(filename,
'w')
as f:
45 print(
'Wrote config: %s' % filename)
49 """! Write a config file for a specific job script.
50 @param job_script job script
51 @param filename currently unused
52 @param include_defaults set true if defaults should be included
53 @param fail_on_missing if true: method fails if environment variable is missing
54 if false: throws warning if environment variable is missing
59 component_names = [c.__class__.__name__
for c
in j.components]
60 write_config(
'job.cfg', component_names, include_defaults, fail_on_missing)
64 """! Get default job class settings."""
69 for cj
in j._config_names:
71 lines +=
'%s = %s\n' % (cj, v)
76 if __name__ ==
'__main__':
78 parser = argparse.ArgumentParser(
"config_writer.py")
79 parser.add_argument(
'-I',
'--ignore-missing', action=
'store_true', help=
'Do not crash if env var is missing.')
80 parser.add_argument(
'-J',
'--job-defaults', action=
'store_true', help=
"Add job defaults to the config")
81 parser.add_argument(
'script', nargs=
'?', help=
"Job script")
82 parser.add_argument(
'config', nargs=
'?', help=
"Config file")
84 cl = parser.parse_args()
87 raise Exception(
'Missing required job script argument.')
Primary class to run HPS jobs from a Python script.
def write_config(filename, component_names, include_defaults, fail_on_missing)
Write a config file from environment variables.
def write_config_for_job(job_script, filename, include_defaults, fail_on_missing)
Write a config file for a specific job script.
def _get_job_defaults()
Get default job class settings.
Utility script for printing help about component classes.