]> mj.ucw.cz Git - moe.git/blob - t/moe/eval.py
Merge branch 'python-newpipe' into python
[moe.git] / t / moe / eval.py
1 #!/usr/bin/env python
2
3 import os.path
4 import shutil
5
6 import moe
7 import moe.config
8 import moe.box
9 import moe.status
10 import moe.pipeline
11 import moe.util
12 import moe.logs
13 from moe.logs import *
14
15
16 class Eval:
17     """
18     """
19
20     def __init__(self):
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()
25
26     def __getitem__(self, key):
27         return self.config[key]
28
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 ...")
32         
33         # set basic builtins
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)
40         
41         # apply overrides
42         for ov in overrides:
43             self.config.parse(ov, source="<overrides>", level=100)
44         
45         # load config file
46         self.config.fix('CONFIG')
47         self.config.parse_file(self['CONFIG'], level=30)
48         # fix variables
49         self.config.fix(['LOG', 'USER_LOG', 'VERBOSE', 'HOME', 'DEBUG_LEVEL', 'TDIR'])
50         # start logging
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()
54
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")
60
61         # ininialize extensions (let them insert hooks) 
62         self.config.fix('EXTENSIONS')
63         exts = self['EXTENSIONS'].split()
64         for ex in exts:
65             if not ex:
66                 raise MoeError, "Invalid extension name: %r" % ex
67             log.debug("Loading extension %s", ex)
68             try:
69                 mod = moe.util.load_module('moe.exts.' + ex)
70             except ImportError:
71                 log.exception("Error importing exception %r", ex)
72                 raise MoeError('Unknown extension: %r', ex)
73             mod.init(self)
74         
75     def run(self):
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)
80
81     def debug_dump_config(self):
82         "Dumps config at level DDEBUG (only compiles the dump if main level is low enough)."
83         if log.level <= 5:
84             log.debug(' ****** Config dump: ******')
85             log.debug('\n'.join(self.config.dump(' * ')))
86             log.debug(' **************************')
87
88     def debug_dump_pipe(self, pipe):
89         "Dumps pipeline `pipe` at level DDEBUG (only compiles the dump if main level low enough)."
90         if log.level <= 5:
91             log.debug(' ****** Pipeline %r dump: ******'%pipe.name)
92             log.debug('\n'.join(pipe.dump(prefix=' * ')))
93             log.debug(' **************************')
94     
95     def debug_dump_status(self):
96         "Dumps status metadata at level DDEBUG (only compiles the dump if main level low enough)."
97         if log.level <= 5:
98             log.ddebug(' ****** Status dump: ******')
99             log.ddebug('\n'.join(self.status.dump(prefix=' * ')).rstrip())
100             log.ddebug(' **************************')
101
102 def hook_init_dirs(e):
103     """(mainline at time 5) Create and check directories, fix directory variables.
104     .. note:: Currently only TDIR."""
105     e.config.fix('TDIR')
106     tdir = e['TDIR']
107     if os.path.isdir(tdir):
108         shutil.rmtree(tdir)
109     moe.util.mkdir_tree(tdir)
110
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()
119
120     e.status["task"] = e['TASK']  # Metadata
121
122 def hook_init_tasktype(e):
123     """(mainline at time 20) Fix `TASK_TYPE`, initialize task type module."""
124
125     e.config.fix('TASK_TYPE')
126     task_type = e['TASK_TYPE']
127     log.debug('Loading module for TASK_TYPE: %r', task_type)
128     if not task_type:
129         raise MoeError, "Invalid TASK_TYPE: %r" % e
130     try:
131         e.tasktype_module = moe.util.load_module('moe.tasktypes.' + task_type)
132     except ImportError:
133         log.exception()
134         raise MoeError, 'Unknown TASK_TYPE: %r' % task_type
135     e.tasktype_module.init(e)
136
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:
142         e.status.write(f)
143
144
145