]> mj.ucw.cz Git - moe.git/blob - t/moe/batch.py
Changes to config operations structure.
[moe.git] / t / moe / batch.py
1 #!/usr/bin/env python
2
3 import os.path
4 import moe
5 import moe.box
6 import moe.eval
7 import moe.util
8 import moe.pipeline
9 import moe.testcase
10 import shutil
11
12 def normalize_ext(e, ext):
13     alias = e["ALIAS_EXT_" + ext]
14     return alias if alias != "" else ext
15
16 def try_ext(e, ext):
17     for e in e["EXTENSIONS"].split():
18         if e == ext:
19             return
20     raise moe.MoeError, "Unknown extension: " + ext
21
22 def locate(e, filename=None):
23     e.log.progress("Locating source... ")
24     task = e["TASK"]
25     if filename is None:
26         dir = ""
27         file = task
28     else:
29         dir, file = os.path.split(filename)
30     if dir == "":
31         dir = e["SDIR"]
32
33     base, ext = os.path.splitext(file)
34     if ext != "":
35         if not os.path.exists(os.path.join(dir, file)):
36             raise moe.SolutionError, "No solution of %s called %s found" % (task,file)
37         ext = ext[1:]
38         try_ext(e, ext)
39     else:
40         found = []
41         for ext in e["EXTENSIONS"].split():
42             if os.path.exists(os.path.join(dir, base + "." + ext)):
43                 found.append(ext)
44         if len(found) == 0:
45             raise moe.SolutionError, "No solution of %s found" % task
46         if len(found) > 1:
47             raise moe.SolutionError, "Multiple solutions of %s found" % task
48         ext = found[0]
49         file = base + "." + ext
50
51     orig_path = os.path.join(dir, file)
52     norm_ext = normalize_ext(e, ext)
53     e.log.verbose("Found solution %s\n" % orig_path)
54
55     copy = e["TASK"] + "." + norm_ext
56     copy_path = os.path.join(e["TDIR"], copy)
57     if file != copy:
58         e.log.verbose("Renaming to %s\n" % copy)
59     moe.util.link_or_copy(orig_path, copy_path)
60
61     e.builtins.set("SRC", copy)
62     e.builtins.set("EXT", norm_ext)
63     e.cfgs.apply_overrides("EXT_" + norm_ext + "_")
64
65     e.stat["source"] = file
66     e.stat["lang"] = ext
67     e.log.progress(file + "\n")
68
69 def compile_init(e):
70     e.log.progress("Compiling... ")
71     boxdir = moe.box.setup(e)
72     pdir = e["PDIR"]
73     tdir = e["TDIR"]
74     shutil.copyfile(os.path.join(tdir, e["SRC"]), os.path.join(boxdir, e["SRC"]))
75     for x in e["EXTRAS"].split() + e["COMP_EXTRAS"].split():
76         xx = os.path.join(tdir, x)
77         if not os.path.isfile(xx):
78             xx = os.path.join(pdir, x)
79         e.log.verbose("Copying extra file %s\n" % xx)
80         shutil.copyfile(xx, os.path.join(boxdir, x))
81
82 def compile_run(e):
83     moe.box.show(e, "compiler input")
84     cc = e["COMP"]
85     e.log.verbose("Compilation command: %s\n" % cc)
86     rc = moe.box.run(e, e["COMP_SANDBOX_OPTS"], cc)
87     if rc > 0:
88         e.log.progress("FAILED\n")
89         ## FIXME: status file ... or generate an exception?
90         raise moe.pipeline.MoeAbortPipeline(200)
91     moe.box.show(e, "compiler output")
92
93 def compile_done(e):
94     try:
95         shutil.copyfile(os.path.join(e["BOXDIR"], e["EXE"]), os.path.join(e["TDIR"], e["EXE"]))
96     except IOError:
97         raise moe.MoeError, "Compiler succeeded, but produced no output"
98     e.log.progress("OK\n")
99
100 def test_in(e):
101     tdir = e["TDIR"]
102     boxdir = moe.box.setup(e)
103     inn = e["TESTCASE_IN"]
104     in_type = e["IN_TYPE"] or e["IO_TYPE"]
105     out_type = e["OUT_TYPE"] or e["IO_TYPE"]
106     is_interactive = e["TASK_TYPE"] == "interactive"
107     sandbox_opts = "-M" + os.path.join(tdir, e["TESTCASE_STATUS"])
108
109     if not os.path.exists(os.path.join(tdir, e["EXE"])):
110         raise TestError("Compilation failed", "CE")
111     shutil.copyfile(os.path.join(tdir, e["EXE"]), os.path.join(boxdir, e["EXE"]))
112     os.chmod(os.path.join(boxdir, e["EXE"]), 0555)
113
114     if in_type == "file":
115         in_name = e["IN_NAME"]
116         e.log.verbose("Input file: %s (copied from %s)\n" % (in_name, os.path.join(e["PDIR"], inn)))
117         try:
118             shutil.copyfile(os.path.join(tdir, inn), os.path.join(boxdir, in_name))
119         except IOError:
120             raise moe.MoeError, "Input file not found"
121         if not is_interactive:
122             sandbox_opts += " -i/dev/null"
123     elif in_type == "stdio":
124         e.log.verbose("Input file: <stdin> (copied from %s)\n" % os.path.join(e["PDIR"], inn))
125         try:
126             shutil.copyfile(os.path.join(tdir, inn), os.path.join(boxdir, ".stdin"))
127         except IOError:
128             raise moe.MoeError, "Input file not found"
129         sandbox_opts += " -i.stdin"
130     elif in_type == "none":
131         e.log.verbose("Input file: <none>\n")
132         if not is_interactive:
133             sandbox_opts += " -i/dev/null"
134     elif in_type == "dir":
135         ## FIXME
136         raise MoeError, "Directory input not yet implemented"
137     else:
138         raise MoeError, "Unknown input type %s" % in_type
139
140     if out_type == "file":
141         out_name = e["OUT_NAME"]
142         e.log.verbose("Output file: %s\n" % out_name)
143         if not is_interactive:
144             sandbox_opts += " -o/dev/null"
145     elif out_type == "stdio":
146         e.log.verbose("Output file: <stdout>\n")
147         sandbox_opts += " -o.stdout"
148     elif out_type == "none":
149         e.log.verbose("Output file: <none>\n")
150         if not is_interactive:
151             sandbox_opts += " -o/dev/null"
152     else:
153         raise MoeError, "Unknown output type %s" % out_type
154
155     e.test_builtins.set("BOX_IO_OPTS", sandbox_opts)
156
157 def test_run(e):
158     e.log.verbose("Time limit: %s s\n" % e["TIME_LIMIT"])
159     e.log.verbose("Memory limit: %s KB\n" % e["MEM_LIMIT"])
160     moe.box.show(e, "test input")
161     e.log.progress("<run> ")
162     rc = moe.box.run(e, e["TEST_SANDBOX_OPTS"], e["TEST_EXEC_CMD"])
163     moe.testcase.collect_status(e)
164     moe.box.show(e, "test output")
165     if rc > 0:
166         raise moe.TestError("Wrong answer", "WA")
167
168 def test_collect(e):
169     tdir = e["TDIR"]
170     boxdir = e["BOXDIR"]
171     out_type = e["OUT_TYPE"] or e["IO_TYPE"]
172     is_interactive = e["TASK_TYPE"] == "interactive"
173
174     if out_type == "file":
175         out_path = e["OUT_NAME"]
176     elif out_type == "stdio":
177         out_path = ".stdout"
178     if not os.path.exists(os.path.join(boxdir, out_path)):
179         raise moe.TestError("No output file", "NO")
180     shutil.copyfile(os.path.join(boxdir, out_path), os.path.join(tdir, e["TESTCASE_OUT"]))
181
182 def tests(e):
183     e.log.progress("\n")
184     e.test_pipe.insert(100, "prepare", test_in)
185     e.test_pipe.insert(200, "run", test_run)
186     e.test_pipe.insert(300, "collect", test_collect)
187     moe.testcase.run_tests(e)
188
189 def prepare_pipe(e):
190     e.main_pipe.insert(100, "compile-init", compile_init)
191     e.main_pipe.insert(150, "compile-run", compile_run)
192     e.main_pipe.insert(190, "compile-done", compile_done)
193     e.main_pipe.insert(200, "batch-tests", tests)