def writebytes(filename, bytesource):
    """
    Convenience for emitting the bytes we generate to a file. Given a
    filename, opens and truncates the file, dumps the bytes
    from bytesource into it, and closes it
    """
    outfile = open(filename, 'wb')
    for bt in bytesource:
        outfile.write(bt)
