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'], self['DEBUG_LEVEL'], redirect_fds = True)
51 self.log.open_user_log(self['USER_LOG'])
52 self.debug_dump_config()
55 self.debug('Cleaning TDIR: %s'%self['TDIR'])
58 # insert hooks into main pipeline
59 self.main_pipe.insert(5, "Eval.hook_init_dirs", self.hook_init_dirs)
60 self.main_pipe.insert(15, "Eval.hook_load_task_config", self.hook_load_task_config)
61 self.main_pipe.insert(20, "Eval.hook_init_tasktype", self.hook_init_tasktype)
62 self.main_pipe.insert(90, "Eval.hook_write_metadata", self.hook_write_metadata)
64 # ininialize extensions (let them insert hooks)
65 self.conf.fix('EXTENSIONS')
66 exts = self['EXTENSIONS'].split()
69 raise MoeError, "Invalid extension name: %r" % e
70 self.log.debug("Loading extension %s", e)
72 mod = util.load_module('moe.exts.' + e)
75 raise MoeError, 'Unknown extension: %r' % e
79 "Run the main pipeline."
80 self.debug_dump_pipe(self.main_pipe)
81 self.debug('Running main pipeline')
82 self.main_pipe.run(self)
84 def debug_dump_config(self):
85 "Dumps config at level DDEBUG (only compiles the dump if main level is low enough)."
86 if self.log.level <= 5:
87 self.log.ddebug('****** Config dump: ******')
88 self.log.ddebug(self.config.dump('**** '))
89 self.log.ddebug('**************************')
91 def debug_dump_pipe(self, pipe):
92 "Dumps pipeline `pipe` at level DDEBUG (only compiles the dump if main level low enough)."
93 if self.log.level <= 5:
94 self.log.ddebug('****** Pipeline %r dump: ******'%pipe,name)
95 self.log.ddebug(pipe.dump(prefix='**** '))
96 self.log.ddebug('**************************')
98 def hook_init_dirs(self):
99 """(mainline at time 5) Create and check directories, fix directory variables.
100 .. note:: Currently only TDIR."""
101 self.config.fix('TDIR')
103 if os.path.isdir(tdir):
105 moe.util.mkdir_tree(tdir)
107 def hook_load_task_config(self):
108 """(mainline at time 15) Load `TASK_CONFIG` and check `PDIR`, fixes `TASK`, `PDIR`, `TASK_CONFIG`."""
109 self.config.fix(['TASK', 'PDIR', 'TASK_CONFIG'])
110 self.log.debug('Loading task config %s', self['TASK_CONFIG'])
111 if not os.path.isdir(self['PDIR']):
112 raise moe.MoeError, "No such task %s in %s" % (self['TASK'], self['PDIR'])
113 self.config.parse_file(self['TASK_CONFIG'], level=50)
114 self.debug_dump_config()
116 self.stat["task"] = task # Metadata
118 def hook_init_tasktype(self):
119 """(mainline at time 20) Fix `TASK_TYPE`, initialize task type module."""
121 self.config.fix('TASK_TYPE')
122 task_type = self['TASK_TYPE']
123 self.log.debug('Loading module for TASK_TYPE: %r', task_type)
125 raise MoeError, "Invalid TASK_TYPE: %r" % e
127 self.tasktype_module = utils.load_module('moe.tasktypes.' + task_type)
130 raise MoeError, 'Unknown TASK_TYPE: %r' % task_type
131 mod.tasktype_module.init(self)
133 def hook_write_metadata(self):
134 """(mainline at time 90) Write status metadata into file `STATUS_FILE`."""
135 self.log.debug('Writing status file %s', self['STATUS_FILE'])
136 self.status.write(self['STATUS_FILE'])
137 # TODO: dump to ddebug