import os
import stat

"""
Auxiliary file to do disassembly + assembly testing of all class files found
"""

def walktree (top = ".", depthfirst = True):
    """
    Get all files in target folder (recursively)    
    """
    
    names = os.listdir(top)
    if not depthfirst:
        yield top, names
    for name in names:
        try:
            st = os.lstat(os.path.join(top, name))
        except os.error:
            continue
        if stat.S_ISDIR(st.st_mode):
            for (newtop, children) in walktree (os.path.join(top, name), depthfirst):
                yield newtop, children
    if depthfirst:
        yield top, names

# Get all files in target folder (recursively)
files = [os.path.join(basepath, child) for (basepath, children) in walktree(".",False) for child in children if child.endswith('.class') and not child.endswith('.bc.class')]

output = open("do-verify.bat", "w")
output.write("@echo off\n")

# Process
for file in files:
    #print file
    # Disassembly file
    #os.popen('python jbytecode.py -d %s' % file)
    output.write('python jbytecode.py -d %s\n' % file)
    # Assembly file
    #os.popen('python jbytecode.py -a %s.bc' % file)
    output.write('python jbytecode.py -a %s.bc\n' % file)
    # Compare (Windows implementation)
    #pipe = os.popen('fc /b %s %s.bc.class' % (file, file))
    output.write('fc /b %s %s.bc.class\n' % (file, file))
    #pipe_lines = pipe.readlines()
    #print pipe_lines[1].rstrip()
    #for pipe_line in pipe.readlines():
    #    print pipe_line.rstrip()

output.close()