You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

#! /usr/bin/env python3
# encoding: utf-8
import os
import logging
import subprocess
import json
import binascii
class Sins():
logger = logging.getLogger()
def __init__(self, parent, seed=None, run_dir=None):
self.logger.info('execute\n{}'.format(json.dumps(
(parent, seed, run_dir))
))
if os.path.isdir(run_dir):
self.run_dir = os.path.abspath(run_dir)
path = self.scrap_recent(self.run_dir)
if not path:
path = os.path.abspath(seed)
self.seed = path
parent = os.path.abspath(parent)
if not os.path.isfile(parent):
raise ValueError('Invalid executable image path.')
self.parent = os.path.abspath(parent)
parsed = self.parent, self.seed, self.run_dir
self.logger.info('parsed\n{}'.format(json.dumps(parsed)))
scrap = self.seed
while True:
self.logger.info('execute\n{}'.format(
json.dumps((self.parent, scrap, self.run_dir))
))
self.execute(self.parent, scrap, self.run_dir)
path = self.scrap_recent(run_dir)
if path:
scrap = path
def scrap_recent(self, run_dir):
scraps = sorted(os.listdir(run_dir))
if scraps:
return os.path.join(run_dir, scraps[-1])
return None
def execute(self, parent, seed, cwd):
subprocess.run([parent, seed], cwd=cwd)
def hex_dumps(scrap_dir):
scrap_dir = os.path.abspath(scrap_dir)
scraps = os.listdir(scrap_dir)
for scrap in scraps:
if '.hex' not in scrap:
scrap = os.path.join(scrap_dir, scrap)
out = os.path.join(scrap_dir, '{}.hex'.format(scrap))
with open(scrap, 'rb') as file_in, open(out, 'w') as file_out:
binary = bytes(file_in.read())
for each in binary:
file_out.writelines('\'\\x{:02X}\',\n'.format(each))
if __name__ == '__main__':
import argparse
logging.basicConfig(filename='sins.log', level=logging.DEBUG)
parser = argparse.ArgumentParser()
parser.add_argument('-seed', default='build/scrap.asm.2.o')
parser.add_argument('-parent', default='build/sins')
parser.add_argument('-dir', default='sandbox')
parser.add_argument('-dumps', action='store_true')
args = parser.parse_args()
if args.dumps:
hex_dumps(args.dir)
else:
sins = Sins(parent=args.parent, seed=args.seed, run_dir=args.dir)