From 653dd4d5b659c4818538ba4ff8a976290a72302e Mon Sep 17 00:00:00 2001 From: JoYo Date: Fri, 15 Jul 2016 00:16:25 -0400 Subject: [PATCH] python run script for parent segfaults --- run.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 run.py diff --git a/run.py b/run.py new file mode 100644 index 0000000..0ec460c --- /dev/null +++ b/run.py @@ -0,0 +1,87 @@ +#! /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)