]> mj.ucw.cz Git - moe.git/commitdiff
Locating batch tasks
authorMartin Mares <mj@ucw.cz>
Sun, 9 Aug 2009 21:39:09 +0000 (23:39 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 9 Aug 2009 21:39:09 +0000 (23:39 +0200)
t/config
t/moe/batch.py
t/moe/config.py
t/moe/eval.py
t/moe/util.py

index 910dfcb1ec99bc29ca8a7a59ac6347eda485751c..c75c2d53e006deaddbef5037ea82c72b4bd3050a 100644 (file)
--- 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
index 7fcfd7aee6b711cd27d5b7b8163ddc756aad00b1..fb891dabdf7dc4524cc33ad06e87eec577d32788 100644 (file)
@@ -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)
index 4baee06494fb2d675ef3db4be9955701a24b9549..99d36f3cb5fa853ebc79e6e48358cc82f05d9c2a 100644 (file)
@@ -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))
 
index 9dd4c8287d96d700f5572f4d991f1f78519f61d8..42f5bda6dfe3f1e1b98613f2ffc4a5a9936d5167 100644 (file)
@@ -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:
index 801d2c700925cff969b0aba8ae80f84bee6f6d3b..69007a193429f62bf2eadfb68527aa120a06a463 100644 (file)
@@ -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)