The scrap-is-not-scrap (sins) experiment began out of a public school education in biology and dissatisfaction with genetic algorithms with their ability to do work.
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.
 
 
 

119 lines
3.5 KiB

#! /usr/bin/env python3
# encoding: utf-8
import os
import logging
import subprocess
class Sins():
logger = logging.getLogger()
def __init__(self, parent, seed=None, run_dir=None):
self.logger.info('execute\n{}'.format((
(parent, seed, run_dir))
))
self.parent = os.path.abspath(parent)
if not os.path.isfile(parent):
raise ValueError('Invalid executable image path.')
self.seed = os.path.abspath(seed)
if os.path.isdir(run_dir):
self.run_dir = os.path.abspath(run_dir)
shutil.copy2(args.seed, args.dir)
else:
self.run_dir = os.path.dirname(self.seed)
parsed = self.parent, self.seed, self.run_dir
self.logger.info('parsed\n{}'.format((parsed)))
while True:
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))
if scraps:
return os.path.join(run_dir, scraps[-1])
return None
def execute(self, parent, scrap, cwd):
self.logger.info('execute\n{}'.format(((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:
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))
if __name__ == '__main__':
import argparse
import shutil
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# handler = logging.FileHandler(filename='sins.log')
handler = logging.StreamHandler()
formatter = logging.Formatter(
'#%(asctime)s %(name)s %(levelname)s\n##%(message)s\n'
)
handler.setFormatter(formatter)
logger.addHandler(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')
parser.add_argument('-provision', action='store_true')
args = parser.parse_args()
if args.provision:
provision = ['sudo', 'sh', 'provision-ubuntu.sh']
prov_proc = subprocess.run(provision)
elif args.build:
import sys
os.environ['CC'] = 'clang'
waf = [sys.executable, 'waf.py', 'configure', 'build']
waf_proc = subprocess.run(waf)
logger.info(waf_proc.stdout)
elif args.dumps:
hex_dumps(args.dir)
else:
sins = Sins(parent=args.parent, seed=args.seed, run_dir=args.dir)