view util/serve_arm_as.py @ 379:78c27013f02e

Removed old recipes
author Windel Bouwman
date Fri, 18 Apr 2014 11:20:11 +0200
parents 3bb7dcfe5529
children
line wrap: on
line source


"""
   Tornado webserver interface to arm-elf-as.
"""

import subprocess
try:
    import tornado.web
    import tornado.ioloop
    from tornado.web import RequestHandler
except Exception as e:
    print(e)
    RequestHandler = object

import io

prefix = 'arm-none-eabi-'
AS = prefix + 'as'
objdump = prefix + 'objdump'

def mangle(inp, outstream):
    print('assembling...', file=outstream)
    p_as = subprocess.Popen([AS], stdin=subprocess.PIPE)
    p_as.communicate(input=inp.encode('ascii'))
    if p_as.returncode != 0:
        raise Exception('{}'.format(p_as.returncode))

    p_objdump = subprocess.Popen([objdump, '-d'], stdout=subprocess.PIPE)
    output = p_objdump.communicate()[0].decode('ascii')
    if p_objdump.returncode != 0:
        raise Exception('{}'.format(p_objdump.returncode))
    print(output, file=outstream)

    p_objdump = subprocess.Popen([objdump, '-s', '-j', '.text'], stdout=subprocess.PIPE)
    output = p_objdump.communicate()[0].decode('ascii')
    if p_objdump.returncode != 0:
        raise Exception('{}'.format(p_objdump.returncode))
    print(output, file=outstream)

page = """
<html>
<head>
</head>
<body>
<h1>

</h1>
<form action="" method="post">
<textarea name="source" rows="25" cols="80">
%src%
</textarea>
<br>
<input type="submit" value="Assemble!">
</form>
<div>
%result%
</div>
</body>
</html>
"""

class MainHandler(RequestHandler):
    def get(self):
        src="sub sp,sp,#8"
        self.write(page.replace('%result%', '').replace('%src%', src))

    def post(self):
        #print(self.request.body)
        src = self.get_argument('source')
        out = io.StringIO()
        mangle(src, out)
        txt = out.getvalue()
        txt = txt.replace('\n', '<br>')
        self.write(page.replace('%result%', txt).replace('%src%', src))


def serve_tornado():
    application = tornado.web.Application([(r"/", MainHandler)])
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

def extract_bytes(txt):
    lines = list(filter(None, (line.strip() for line in txt.split('\n'))))
    idx = lines.index('Contents of section .text:')

    byte_txt = ''
    for line in lines[idx+1:]:
        parts = list(filter(None, line.split(' ')))
        byte_txt += ' '.join(parts[1:-1]) + ' '
    byte_txt = byte_txt.strip()
    return byte_txt

def run_on_file():
    with open('test_patterns.txt', 'r') as f:
        data = f.read()

    outfile = open('tests.pygen', 'w')
    snippets = data.split('===')
    for snippet in snippets:
        make_test(snippet, outfile)
    outfile.close()

def make_test(src, output_file):
    out = io.StringIO()
    try:
        mangle(src, out)
    except Exception as e:
        return
    txt = out.getvalue()
    byte_txt = extract_bytes(txt)
    print("    def testTODO(self):", file=output_file)
    for line in filter(None, src.split('\n')):
        print("        self.feed('{}')".format(line), file=output_file)
    print("        self.check('{}')".format(byte_txt), file=output_file)

if __name__ == '__main__':
    #serve_tornado()
    run_on_file()