#!/usr/bin/env python
import database
import kimobjects
from compute import Computation
import sys
from config import *
from logger import logging
from pipeline import Director
logger = logging.getLogger("pipeline").getChild("development")

if __name__ == "__main__":
    if len(sys.argv) == 2:
        d = Director()
        testname = sys.argv[1]
        name,leader,num,version = database.parse_kim_code(testname)
        if leader == "TE":
            test = kimobjects.Test(testname)
            if len(list(test.models)) > 0:
                for model in test.models:
                    logger.info("Running combination <%r, %r" % (test, model))
                    Computation(test, model, d.get_result_code()).run()
            else:
                logger.info("No matches found for your test")
        if leader == "TD":
            test = kimobjects.TestDriver(testname)
            if len(list(test.tests)) > 0:
                for t in test.tests:
                    for m in t.models:
                        logger.info("Running combination <%r, %r" % (t, m))
                        Computation(t, m, d.get_result_code()).run()
            else:
                logger.info("No matches found for your test driver")
        if leader == "MO":
            model = kimobjects.Model(testname)
            if len(list(model.tests)) > 0:
                for t in model.tests:
                    logger.info("Running combination <%r, %r" % (t, model))
                    Computation(t, model, d.get_result_code()).run()
            else:
                logger.info("No matches found for your test driver")
        if leader == "MD":
            driver = kimobjects.ModelDriver(testname)
            if len(list(driver.models)) > 0:
                for m in driver.models:
                    for t in m.tests:
                        logger.info("Running combination <%r, %r" % (t, m))
                        Computation(t, m, d.get_result_code()).run()
            else:
                logger.info("No matches found for your test driver")
    
    else:
        logger.info("Usage: pipeline_runmatches <testname>")
