From a882cdca9e8b92e89751bfe26a34b5dee59e50c3 Mon Sep 17 00:00:00 2001 From: JoYo Date: Fri, 15 Jul 2016 01:20:57 -0400 Subject: [PATCH] minor tweaking --- run.py | 72 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/run.py b/run.py index 0ec460c..dfc4d57 100644 --- a/run.py +++ b/run.py @@ -4,7 +4,6 @@ import os import logging import subprocess import json -import binascii class Sins(): @@ -18,32 +17,28 @@ class Sins(): 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 + self.seed = os.path.abspath(seed) parent = os.path.abspath(parent) if not os.path.isfile(parent): raise ValueError('Invalid executable image path.') - self.parent = os.path.abspath(parent) + self.parent = 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 + self.execute(self.parent, self.seed, self.run_dir) + paths = sorted(os.listdir(self.run_dir)) + for path in paths: + scrap_path = os.path.join(self.run_dir, path) + if os.path.isfile(scrap_path): + self.execute( + self.parent, + scrap_path, + self.run_dir + ) def scrap_recent(self, run_dir): scraps = sorted(os.listdir(run_dir)) @@ -53,18 +48,32 @@ class Sins(): return None - def execute(self, parent, seed, cwd): - subprocess.run([parent, seed], cwd=cwd) + def execute(self, parent, scrap, cwd): + self.logger.info('execute\n{}'.format( + json.dumps((parent, scrap, cwd)) + )) + subprocess.run([parent, scrap], cwd=cwd) def hex_dumps(scrap_dir): scrap_dir = os.path.abspath(scrap_dir) + dump_dir = os.path.join(scrap_dir, 'hexdumps') + + try: + os.mkdir(dump_dir) + except: + pass + 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: + scrap_path = os.path.join(scrap_dir, scrap) + hex_name = '{}.hex'.format(scrap) + hex_path = os.path.join(dump_dir, hex_name) + + if os.path.isfile(scrap_path): + with open(scrap_path, 'rb') as file_in: + with open(hex_path, 'w') as file_out: binary = bytes(file_in.read()) for each in binary: file_out.writelines('\'\\x{:02X}\',\n'.format(each)) @@ -73,14 +82,29 @@ def hex_dumps(scrap_dir): if __name__ == '__main__': import argparse - logging.basicConfig(filename='sins.log', level=logging.DEBUG) + logger = logging.getLogger() + logger.setLevel(logging.DEBUG) + + file_handler = logging.FileHandler(filename='sins.log') + formatter = logging.Formatter( + '#%(asctime)s %(name)s %(levelname)s\n##%(message)s\n' + ) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + 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') + parser.add_argument('-build', action='store_true') args = parser.parse_args() + if args.build: + waf = ['python3', 'waf.py', 'configure', 'build'] + waf_proc = subprocess.run(waf, stdout=subprocess.PIPE) + logger.info(waf_proc.stdout) + if args.dumps: hex_dumps(args.dir) else: