From: Martin Mares Date: Sun, 9 Aug 2009 21:39:09 +0000 (+0200) Subject: Locating batch tasks X-Git-Tag: python-dummy-working~86 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=9587bd376c403e16a0f79c9046eecc9b8f543cb8;p=eval.git Locating batch tasks --- diff --git a/t/config b/t/config index 910dfcb..c75c2d5 100644 --- a/t/config +++ b/t/config @@ -1,8 +1,21 @@ # HOME set automatically # CONTESTANT set automatically # TASK set automatically +## FIXME: Rename? TASK_DIR="${HOME}/problems/${TASK}" SOL_DIR="${HOME}/solutions/${CONTESTANT}/${TASK}" TEST_DIR="${HOME}/testing/${CONTESTANT}/${TASK}" TASK_TYPE=batch + +### Programming language settings + +# Known source file extensions +EXTENSIONS="c cc C cpp p pas" + +# Some of the extensions can be aliases for other extensions +ALIAS_EXT_cc=cpp +ALIAS_EXT_C=cpp +ALIAS_EXT_p=pas + +# SRC is auto diff --git a/t/moe/batch.py b/t/moe/batch.py index 7fcfd7a..fb891da 100644 --- a/t/moe/batch.py +++ b/t/moe/batch.py @@ -1,10 +1,70 @@ #!/usr/bin/env python +import os.path +import moe.log +import moe.eval +import moe.util + +def normalize_ext(e, ext): + alias = e.cfgs["ALIAS_EXT_" + ext] + return alias if alias != "" else ext + +def try_ext(e, ext): + for e in e.cfgs["EXTENSIONS"].split(): + if e == ext: + return + raise moe.eval.MoeEvalErr, "Unknown extension: " + ext + +def locate(e, filename=None): + task = e.cfgs["TASK"] + if filename is None: + dir = "" + file = task + else: + dir, file = os.path.split(filename) + if dir == "": + dir = e.cfgs["SOL_DIR"] + + base, ext = os.path.splitext(file) + if ext != "": + if not os.path.exists(os.path.join(dir, file)): + raise moe.eval.MoeEvalErr, "No solution of %s called %s found" % (task,file) + ext = ext[1:] + try_ext(e, ext) + else: + found = [] + for ext in e.cfgs["EXTENSIONS"].split(): + if os.path.exists(os.path.join(dir, base + "." + ext)): + found.append(ext) + if len(found) == 0: + raise moe.eval.MoeEvalErr, "No solution of %s found" % task + if len(found) > 1: + raise moe.eval.MoeEvalErr, "Multiple solutions of %s found" % task + ext = found[0] + file = base + "." + ext + + orig_path = os.path.join(dir, file) + norm_ext = normalize_ext(e, ext) + moe.log.verbose("Found solution %s\n" % orig_path) + + copy = e.cfgs["TASK"] + "." + norm_ext + copy_path = os.path.join(e.cfgs["TEST_DIR"], copy) + if file != copy: + moe.log.verbose("Renaming to %s\n" % copy) + moe.util.link_or_copy(orig_path, copy_path) + + e.builtins.set("SRC", copy) + e.builtins.set("EXT", norm_ext) + e.cfgs.apply_overrides("EXT_" + norm_ext) + + e.meta["source"] = copy + def compile(e): pass def tests(e): pass -def locate(e, file=None): - pass +def prepare_pipe(e): + e.main_pipe.insert(100, "compile", compile) + e.main_pipe.insert(200, "batch-tests", tests) diff --git a/t/moe/config.py b/t/moe/config.py index 4baee06..99d36f3 100644 --- a/t/moe/config.py +++ b/t/moe/config.py @@ -171,7 +171,7 @@ class MoeConfigStack: return seen.keys() def dump(self, file=sys.stdout): - for k in self.keys(): + for k in sorted(self.keys()): v = self[k] file.write("%s=%s\n" % (k,v)) diff --git a/t/moe/eval.py b/t/moe/eval.py index 9dd4c82..42f5bda 100644 --- a/t/moe/eval.py +++ b/t/moe/eval.py @@ -49,11 +49,11 @@ class Eval: raise MoeEvalErr, "No such task %s" % task task_cfg = moe.config.MoeConfig(name = os.path.join(task_dir, "config")) self.cfgs.push(task_cfg) + self.meta["task"] = task type = self.cfgs['TASK_TYPE'] if type == "batch" or type == "interactive": - self.main_pipe.insert(100, "compile", moe.batch.compile) - self.main_pipe.insert(200, "batch-tests", moe.batch.tests) + moe.batch.prepare_pipe(self) elif type == "opendata": raise MoeEvalErr, "Opendata tasks not implemented yet" else: diff --git a/t/moe/util.py b/t/moe/util.py index 801d2c7..69007a1 100644 --- a/t/moe/util.py +++ b/t/moe/util.py @@ -2,6 +2,7 @@ import os import os.path +import shutil def mkdir_tree(name): try: @@ -15,3 +16,9 @@ def mkdir_tree(name): pass else: raise e + +def link_or_copy(src, dest): + try: + os.link(src, dest) + except OSError: + shutil.copyfile(src, dest)