HPS-MC
config_writer.py
Go to the documentation of this file.
1 """! Utilities for writing config files for jobs from environment variables."""
2 
3 import argparse
4 
5 from hpsmc.tools import *
6 from hpsmc.generators import *
7 
8 from hpsmc.job import Job
9 from hpsmc.help import _ignore
10 
11 
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"""
19  lines = []
20  if include_defaults:
21  lines.extend(_get_job_defaults())
22  for k in sorted(globals().keys()):
23  v = globals()[k]
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:
27  # print('Skipping ' + v.__name__)
28  continue
29  # print('Config env for ' + v.__name__)
30  obj = eval(v.__name__)()
31  section = '[%s]\n' % v.__name__
32  lines += section
33  for c in obj.required_config():
34  env_var = c.upper()
35  if env_var in os.environ:
36  s = '%s = %s\n' % (c, os.environ[env_var])
37  lines += s
38  elif fail_on_missing:
39  raise Exception('Missing env var: %s' % env_var)
40  else:
41  print("WARNING: Missing env var: %s" % env_var)
42  lines += '\n'
43  with open(filename, 'w') as f:
44  f.writelines(lines)
45  print('Wrote config: %s' % filename)
46 
47 
48 def write_config_for_job(job_script, filename, include_defaults, fail_on_missing):
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
55  """
56  j = Job()
57  j.script = job_script
58  j._load_script()
59  component_names = [c.__class__.__name__ for c in j.components]
60  write_config('job.cfg', component_names, include_defaults, fail_on_missing)
61 
62 
64  """! Get default job class settings."""
65 
66  lines = []
67  lines += '[Job]\n'
68  j = Job()
69  for cj in j._config_names:
70  v = getattr(j, cj)
71  lines += '%s = %s\n' % (cj, v)
72  lines += '\n'
73  return lines
74 
75 
76 if __name__ == '__main__':
77 
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")
83 
84  cl = parser.parse_args()
85 
86  if not cl.script:
87  raise Exception('Missing required job script argument.')
88 
89  if cl.config:
90  config = cl.config
91  else:
92  config = 'job.cfg'
93 
94  write_config_for_job(cl.script, config, cl.job_defaults, (not cl.ignore_missing))
Primary class to run HPS jobs from a Python script.
Definition: job.py:160
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.
Definition: help.py:1
Definition: job.py:1
Tools that can be used in HPSMC jobs.
Definition: tools.py:1