]> mj.ucw.cz Git - moe.git/blob - t/moe/eval.py
Fix in status parsing, add test, add update test
[moe.git] / t / moe / eval.py
1 #!/usr/bin/env python
2
3 import moe
4 import moe.config
5 import moe.box
6 import moe.log
7 import moe.status
8 import moe.pipeline
9 import moe.util
10 import os.path
11 import shutil
12
13 class Eval:
14     """
15     """
16
17     def __init__(self):
18         self.log = moe.log.Loggers()
19         self.config = moe.config.ConfigTree()
20         self.main_pipe = moe.pipeline.Pipeline(self, "main")
21         self.test_pipe = moe.pipeline.Pipeline(self, "test")
22         self.status = moe.status.Status()
23
24     def __getitem__(self, key):
25         return self.config[key]
26
27     def init(self, overrides=[]):
28         "Initializes most part of Eval before running the pipeline. See the timeline for details."
29         self.log.info("Initializing ...")
30         
31         # set basic builtins
32         self.config.parse('HOME = \'%s\'' % os.getcwd(), source="<builtins>", level=0)
33         self.config.parse('CONFIG = "{HOME}/config"', source="<builtins>", level=0)
34         self.config.parse('LOG = "{HOME}/log"', source="<builtins>", level=0)
35         self.config.parse('DEBUG_LEVEL = "0"', source="<builtins>", level=0)
36         self.config.parse('VERBOSE = ""', source="<builtins>", level=0)
37         self.config.parse('EXTENSIONS = ""', source="<builtins>", level=0)
38         
39         # apply overrides
40         for ov in overrides:
41             self.config.parse(ov, source="<overrides>", level=100)
42         
43         # load config file
44         self.config.fix('CONFIG')
45         self.config.parse_file(self['CONFIG'], level=30)
46         # fix variables
47         self.config.fix(['LOG', 'USER_LOG', 'VERBOSE', 'HOME', 'DEBUG_LEVEL', 'TDIR'])
48         # start logging
49         self.log.open_eval_log(self['LOG'], int(self['DEBUG_LEVEL']), redirect_fds = True)
50         self.log.open_user_log(self['USER_LOG'])
51         self.debug_dump_config()
52
53         # insert hooks into main pipeline
54         self.main_pipe.insert(5, hook_init_dirs, "Initialize working directories")
55         self.main_pipe.insert(15, hook_load_task_config, "Load task config")
56         self.main_pipe.insert(20, hook_init_tasktype, "Load tasktype module")
57         self.main_pipe.insert(90, hook_write_metadata, "Write final metadata file")
58
59         # ininialize extensions (let them insert hooks) 
60         self.config.fix('EXTENSIONS')
61         exts = self['EXTENSIONS'].split()
62         for e in exts:
63             if not e:
64                 raise MoeError, "Invalid extension name: %r" % e
65             self.log.debug("Loading extension %s", e)
66             try:
67                 mod = moe.util.load_module('moe.exts.' + e)
68             except ImportError:
69                 self.log.exception()
70                 raise MoeError, 'Unknown extension: %r' % e
71             mod.init(self)
72         
73     def run(self):
74         "Run the main pipeline."
75         self.debug_dump_pipe(self.main_pipe)
76         self.log.debug('Running main pipeline')
77         self.main_pipe.run(e=self)
78
79     def debug_dump_config(self):
80         "Dumps config at level DDEBUG (only compiles the dump if main level is low enough)."
81         if self.log.level <= 5:
82             self.log.ddebug(' ****** Config dump: ******')
83             self.log.ddebug('\n'.join(self.config.dump(' * ')))
84             self.log.ddebug(' **************************')
85
86     def debug_dump_pipe(self, pipe):
87         "Dumps pipeline `pipe` at level DDEBUG (only compiles the dump if main level low enough)."
88         if self.log.level <= 5:
89             self.log.ddebug(' ****** Pipeline %r dump: ******'%pipe.name)
90             self.log.ddebug('\n'.join(pipe.dump(prefix=' * ')))
91             self.log.ddebug(' **************************')
92     
93     def debug_dump_status(self):
94         "Dumps status metadata at level DDEBUG (only compiles the dump if main level low enough)."
95         if self.log.level <= 5:
96             self.log.ddebug(' ****** Status dump: ******')
97             self.log.ddebug('\n'.join(self.status.dump(prefix=' * ')).rstrip())
98             self.log.ddebug(' **************************')
99
100 def hook_init_dirs(e):
101     """(mainline at time 5) Create and check directories, fix directory variables.
102     .. note:: Currently only TDIR."""
103     e.config.fix('TDIR')
104     tdir = e['TDIR']
105     if os.path.isdir(tdir):
106         shutil.rmtree(tdir)
107     moe.util.mkdir_tree(tdir)
108
109 def hook_load_task_config(e):
110     """(mainline at time 15) Load `TASK_CONFIG` and check `PDIR`, fixes `TASK`, `PDIR`, `TASK_CONFIG`."""
111     e.config.fix(['TASK', 'PDIR', 'TASK_CONFIG'])
112     e.log.debug('Loading task config %s', e['TASK_CONFIG'])
113     if not os.path.isdir(e['PDIR']):
114         raise moe.MoeError, "No such task %s in %s" % (e['TASK'], e['PDIR'])
115     e.config.parse_file(e['TASK_CONFIG'], level=50)
116     e.debug_dump_config()
117
118     e.status["task"] = e['TASK']  # Metadata
119
120 def hook_init_tasktype(e):
121     """(mainline at time 20) Fix `TASK_TYPE`, initialize task type module."""
122
123     e.config.fix('TASK_TYPE')
124     task_type = e['TASK_TYPE']
125     e.log.debug('Loading module for TASK_TYPE: %r', task_type)
126     if not task_type:
127         raise MoeError, "Invalid TASK_TYPE: %r" % e
128     try:
129         e.tasktype_module = moe.util.load_module('moe.tasktypes.' + task_type)
130     except ImportError:
131         e.log.exception()
132         raise MoeError, 'Unknown TASK_TYPE: %r' % task_type
133     e.tasktype_module.init(e)
134
135 def hook_write_metadata(e):
136     """(mainline at time 90) Write status metadata into file `STATUS_FILE`."""
137     e.debug_dump_status()
138     e.log.debug('Writing status file %s', e['STATUS_FILE'])
139     with open(e['STATUS_FILE'], 'w') as f:
140         e.status.write(f)
141
142
143