def generateSteeringFile(run, alignruns, fun, siteConfig, runConfig, jobs):
    params = {}
    params["RunNumber"] = run
    params["AlignInputFiles"] = alignruns
    siteConfig(params)
    runConfig(params)
    fun(params)
    #Slurp template file 
    steeringString = open(params["TemplatePath"] + "/" + params["TemplateFile"], "r").read()
    #Query replace
    for key in params.keys():
        steeringString = steeringString.replace("@" + key + "@", params[key])
    checkSteer(steeringString)
    jobs.append( [run, steeringString] )
def jobMaker( functions, runConfig):
    import sys, time, os
    from siteConfig import siteConfig 
    modes, runs, dryRun = parseArgs(sys.argv)
    globParams = {}
    siteConfig(globParams)
    runConfig(globParams)
    if( len(runs) == 0):
        runs = eval(globParams["DataSet"])
    for mode in modes:
        jobs = []
        func = None
        try:
            func = functions[mode]
        except:
            print( mode , " does not appear to be a valid mode. Should be one of ", functions.keys())
            sys.exit()
        if (mode == "aligndut") or (mode == "aligndut2") or (mode == "aligntel") or (mode == "daffittersingle") or (mode == "matest"):
            runstring = ""
            for run in runs:
                runstring = runstring + globParams["ResultPath"] + "/run" + str(run).zfill(6) + "-hit-p.slcio\n"
            generateSteeringFile(str(runs[0]).zfill(6), runstring, func, siteConfig, runConfig, jobs)
        else:
            for run in runs:
                generateSteeringFile(str(run).zfill(6), "", functions[mode], siteConfig, runConfig, jobs )
        procs = {}
        if dryRun:
            for steer in jobs:
                print( steer[1])
        else:
            while len(procs) or len(jobs):
                if (len(procs) >= int(globParams["NParallel"])) or len(jobs) == 0:
                    print( len(procs), "job(s) running, waiting for one to finish...")
                    (pid,status) = os.wait()
                    cleanUpJob(procs[pid])
                    del procs[pid]
                if len(jobs):
                    startJob(mode, jobs[0], procs)
                    del jobs[0]
        print( "Done")