13 from moe.logs import *
21 self.config = moe.config.ConfigTree()
22 self.main_pipe = moe.pipeline.Pipeline("main")
23 self.test_pipe = moe.pipeline.Pipeline("test")
24 self.status = moe.status.Status()
26 def __getitem__(self, key):
27 return self.config[key]
29 def init(self, overrides=[]):
30 "Initializes most part of Eval before running the pipeline. See the timeline for details."
31 log.info("Initializing Eval ...")
34 self.config.parse('HOME = \'%s\'' % os.getcwd(), source="<builtins>", level=0)
35 self.config.parse('CONFIG = "{HOME}/config"', source="<builtins>", level=0)
36 self.config.parse('LOG = "{HOME}/log"', source="<builtins>", level=0)
37 self.config.parse('DEBUG_LEVEL = "0"', source="<builtins>", level=0)
38 self.config.parse('VERBOSE = ""', source="<builtins>", level=0)
39 self.config.parse('EXTENSIONS = ""', source="<builtins>", level=0)
43 self.config.parse(ov, source="<overrides>", level=100)
46 self.config.fix('CONFIG')
47 self.config.parse_file(self['CONFIG'], level=30)
49 self.config.fix(['LOG', 'USER_LOG', 'VERBOSE', 'HOME', 'DEBUG_LEVEL', 'TDIR'])
51 moe.logs.open_eval_log(self['LOG'], level=int(self['DEBUG_LEVEL']), redirect_fds = True)
52 moe.logs.open_user_log(self['USER_LOG'])
53 self.debug_dump_config()
55 # insert hooks into main pipeline
56 self.main_pipe.insert(50, hook_init_dirs, "Initialize working directories")
57 self.main_pipe.insert(100, hook_load_task_config, "Load task config")
58 self.main_pipe.insert(200, hook_init_tasktype, "Load tasktype module")
59 self.main_pipe.insert(900, hook_write_metadata, "Write final metadata file")
61 # ininialize extensions (let them insert hooks)
62 self.config.fix('EXTENSIONS')
63 exts = self['EXTENSIONS'].split()
66 raise MoeError, "Invalid extension name: %r" % ex
67 log.debug("Loading extension %s", ex)
69 mod = moe.util.load_module('moe.exts.' + ex)
71 log.exception("Error importing exception %r", ex)
72 raise MoeError('Unknown extension: %r', ex)
76 "Run the main pipeline."
77 self.debug_dump_pipe(self.main_pipe)
78 log.debug('Running main pipeline')
79 self.main_pipe.run(e=self)
81 def debug_dump_config(self):
82 "Dumps config at level DDEBUG (only compiles the dump if main level is low enough)."
84 log.debug(' ****** Config dump: ******')
85 log.debug('\n'.join(self.config.dump(' * ')))
86 log.debug(' **************************')
88 def debug_dump_pipe(self, pipe):
89 "Dumps pipeline `pipe` at level DDEBUG (only compiles the dump if main level low enough)."
91 log.debug(' ****** Pipeline %r dump: ******'%pipe.name)
92 log.debug('\n'.join(pipe.dump(prefix=' * ')))
93 log.debug(' **************************')
95 def debug_dump_status(self):
96 "Dumps status metadata at level DDEBUG (only compiles the dump if main level low enough)."
98 log.ddebug(' ****** Status dump: ******')
99 log.ddebug('\n'.join(self.status.dump(prefix=' * ')).rstrip())
100 log.ddebug(' **************************')
102 def hook_init_dirs(e):
103 """(mainline at time 5) Create and check directories, fix directory variables.
104 .. note:: Currently only TDIR."""
107 if os.path.isdir(tdir):
109 moe.util.mkdir_tree(tdir)
111 def hook_load_task_config(e):
112 """(mainline at time 15) Load `TASK_CONFIG` and check `PDIR`, fixes `TASK`, `PDIR`, `TASK_CONFIG`."""
113 e.config.fix(['TASK', 'PDIR', 'TASK_CONFIG'])
114 log.debug('Loading task config %s', e['TASK_CONFIG'])
115 if not os.path.isdir(e['PDIR']):
116 raise moe.MoeError, "No such task %s in %s" % (e['TASK'], e['PDIR'])
117 e.config.parse_file(e['TASK_CONFIG'], level=50)
118 e.debug_dump_config()
120 e.status["task"] = e['TASK'] # Metadata
122 def hook_init_tasktype(e):
123 """(mainline at time 20) Fix `TASK_TYPE`, initialize task type module."""
125 e.config.fix('TASK_TYPE')
126 task_type = e['TASK_TYPE']
127 log.debug('Loading module for TASK_TYPE: %r', task_type)
129 raise MoeError, "Invalid TASK_TYPE: %r" % e
131 e.tasktype_module = moe.util.load_module('moe.tasktypes.' + task_type)
134 raise MoeError, 'Unknown TASK_TYPE: %r' % task_type
135 e.tasktype_module.init(e)
137 def hook_write_metadata(e):
138 """(mainline at time 90) Write status metadata into file `STATUS_FILE`."""
139 e.debug_dump_status()
140 log.debug('Writing status file %s', e['STATUS_FILE'])
141 with open(e['STATUS_FILE'], 'w') as f: