import os

datadir = 'LispData'
numbins = str(2)
rootdir = '/home/jwood13/svns/wisp/var/jwood/lisp101/ml/' + datadir
want  = "flawless"
what  = "$NOC"
value = str(0)
walk = os.walk(rootdir)

# Header to flawless
resheader = "Set 1            A     B     C     D     Pd     Pf   Prec    Acc  Bin\n"
try:
    outfile = open("results/ruletest.txt", "a")
except IOError:
    print ("Error: You lack permission to write files here\n")
    exit()
outfile.write(resheader)
outfile.close()

# Walk through main dir
for subdir, dirs, files in walk:
    # Not really needed but sort the dirs
    dirs.sort()
    for adir in dirs:
        # Subdir for files os walk
        subdir = rootdir + "/" + adir      
        # Walk through the subdir
        for subdir, dirs, files in os.walk(subdir):
            # Put the files in order
            files.sort()
            i = 0
            # Go through files 2 at a time and feed them to test2
            # by re-writing main each time
            while (i < len(files)):
                infile1  = datadir + "/" + adir + "/" + files[i]
                #infile2  = datadir + "/" + adir + "/" + files[i+1]
                outline1 = "(load \"boot.lisp\")\n"
                outline2 = "(test3 \"" + infile1 + "\" " + numbins + "  \"" + want + "\" \"" + what + "\" " + value + ")"
                command  = "bash lisp"
                # Open and write main.lisp
                mainloc = "main.lisp"
                try:
                    outfile = open(mainloc, "w")
                except IOError:
                    print ("Error: You lack permission to write files here\n")
                    exit()              
                outfile.write(outline1)
                outfile.write(outline2)
                outfile.close()
                print (outline2)
                i += 2
                # call lisp (writes a row to the results each call) BROKEN!?
                out = os.popen(command)
                out.close()
        print

        




                
            
