def cleanup():
failed = {}<EOL>success = []<EOL>for dbname, db in dbs.items():<EOL><INDENT>try:<EOL><INDENT>db.save(True)<EOL>success.append(dbname)<EOL><DEDENT>except: <EOL><INDENT>import sys, traceback<EOL>xcls, xerr = sys.exc_info()[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>failed[dbname] = traceback.format_tb(sys.exc_info()[<NUM_LIT:2>])<EOL><DEDENT><DEDENT>for sdb in success:<EOL><INDENT>if writeable:<EOL><INDENT>msg.okay("<STR_LIT>".format(*sdb), <NUM_LIT:0>)<EOL><DEDENT><DEDENT>for fdb, tb in failed.items(): <EOL><INDENT>msg.err("<STR_LIT>".format(tb, *fdb),<EOL>prefix=False)<EOL><DEDENT>
Saves all the open databases to JSON so that the kernel can be shut down
    without losing in-memory collections.