19 self.log = moe.log.Loggers()
20 self.config = moe.config.ConfigTree()
21 self.main_pipe = moe.pipeline.MoePipeline("main")
22 self.test_pipe = moe.pipeline.MoePipeline("test")
23 self.status = moe.status.MoeStatus()
25 def __getitem__(self, key):
26 return self.config[key]
28 def init(self, overrides=[]):
29 "Initializes most part of Eval before running the pipeline. See the timeline for details."
30 self.log.info("Initializing ...")
33 self.config.parse('HOME = \'%s\'' % os.getcwd(), source="<builtins>", level=0)
34 self.config.parse('CONFIG = "{HOME}/config"', source="<builtins>", level=0)
35 self.config.parse('LOG = "{HOME}/log"', source="<builtins>", level=0)
36 self.config.parse('DEBUG_LEVEL = "0"', source="<builtins>", level=0)
37 self.config.parse('VERBOSE = ""', source="<builtins>", level=0)
38 self.config.parse('EXTENSIONS = ""', source="<builtins>", level=0)
42 self.config.parse(ov, source="<overrides>", level=100)
45 self.config.fix('CONFIG')
46 self.config.parse_file(self['CONFIG'], level=30)
48 self.config.fix(['LOG', 'USER_LOG', 'VERBOSE', 'HOME', 'DEBUG_LEVEL', 'TDIR'])
50 self.log.open_eval_log(self['LOG'], int(self['DEBUG_LEVEL']), redirect_fds = True)
51 self.log.open_user_log(self['USER_LOG'])
52 self.debug_dump_config()
54 # insert hooks into main pipeline
55 self.main_pipe.insert(5, "Eval.hook_init_dirs", hook_init_dirs)
56 self.main_pipe.insert(15, "Eval.hook_load_task_config", hook_load_task_config)
57 self.main_pipe.insert(20, "Eval.hook_init_tasktype", hook_init_tasktype)
58 self.main_pipe.insert(90, "Eval.hook_write_metadata", hook_write_metadata)
60 # ininialize extensions (let them insert hooks)
61 self.config.fix('EXTENSIONS')
62 exts = self['EXTENSIONS'].split()
65 raise MoeError, "Invalid extension name: %r" % e
66 self.log.debug("Loading extension %s", e)
68 mod = moe.util.load_module('moe.exts.' + e)
71 raise MoeError, 'Unknown extension: %r' % e
75 "Run the main pipeline."
76 self.debug_dump_pipe(self.main_pipe)
77 self.log.debug('Running main pipeline')
78 self.main_pipe.run(e=self)
80 def debug_dump_config(self):
81 "Dumps config at level DDEBUG (only compiles the dump if main level is low enough)."
82 if self.log.level <= 5:
83 self.log.ddebug('****** Config dump: ******')
84 self.log.ddebug('\n'.join(self.config.dump('* ')))
85 self.log.ddebug('**************************')
87 def debug_dump_pipe(self, pipe):
88 "Dumps pipeline `pipe` at level DDEBUG (only compiles the dump if main level low enough)."
89 if self.log.level <= 5:
90 self.log.ddebug('****** Pipeline %r dump: ******'%pipe.name)
91 self.log.ddebug(pipe.pipe)
92 self.log.ddebug(pipe.dump(prefix='* '))
93 self.log.ddebug('**************************')
95 def hook_init_dirs(e):
96 """(mainline at time 5) Create and check directories, fix directory variables.
97 .. note:: Currently only TDIR."""
100 if os.path.isdir(tdir):
102 moe.util.mkdir_tree(tdir)
104 def hook_load_task_config(e):
105 """(mainline at time 15) Load `TASK_CONFIG` and check `PDIR`, fixes `TASK`, `PDIR`, `TASK_CONFIG`."""
106 e.config.fix(['TASK', 'PDIR', 'TASK_CONFIG'])
107 e.log.debug('Loading task config %s', self['TASK_CONFIG'])
108 if not os.path.isdir(e['PDIR']):
109 raise moe.MoeError, "No such task %s in %s" % (e['TASK'], self['PDIR'])
110 e.config.parse_file(self['TASK_CONFIG'], level=50)
111 e.debug_dump_config()
113 e.status["task"] = task # Metadata
115 def hook_init_tasktype(e):
116 """(mainline at time 20) Fix `TASK_TYPE`, initialize task type module."""
118 e.config.fix('TASK_TYPE')
119 task_type = e['TASK_TYPE']
120 e.log.debug('Loading module for TASK_TYPE: %r', task_type)
122 raise MoeError, "Invalid TASK_TYPE: %r" % e
124 e.tasktype_module = utils.load_module('moe.tasktypes.' + task_type)
127 raise MoeError, 'Unknown TASK_TYPE: %r' % task_type
128 mod.tasktype_module.init(e)
130 def hook_write_metadata(e):
131 """(mainline at time 90) Write status metadata into file `STATUS_FILE`."""
132 e.log.debug('Writing status file %s', self['STATUS_FILE'])
133 e.status.write(self['STATUS_FILE'])
134 # TODO: dump to ddebug