initial orm for sqlite state

master
JoYo 2019-02-19 04:40:35 +00:00
parent 4cb44525dc
commit ce4e8b57c5
3 changed files with 68 additions and 17 deletions

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .run import sins, generation from .run import sins
# from .orm import SeedNode from .mutation import generation, flip, seed_shell
from .orm import db_config, ScrapNode

View File

@ -1,19 +1,26 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from datetime import datetime from datetime import datetime
from sqlalchemy import Blob, Column, ForeignKey, Integer, String, DateTime, create_engine, exists, desc from hashlib import sha1
from pathlib import Path
from sqlalchemy import LargeBinary, Column, ForeignKey, Integer, String, DateTime, create_engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship, backref from sqlalchemy.orm import Session, relationship, backref
from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy.orm.collections import attribute_mapped_collection
import logging import logging
from hashlib import sha1
logger = logging.getLogger('sins') logger = logging.getLogger('sins')
now = '{0:%Y%m%dT%H%M%S}'.format(datetime.utcnow()) now = '{0:%Y%m%dT%H%M%S}'.format(datetime.utcnow())
Base = declarative_base() Base = declarative_base()
def db_config(path: Path) -> Session:
engine = create_engine(f'sqlite:///{path.resolve()}', native_datetime=True)
Base.metadata.create_all(engine)
session = Session(engine)
return session
class SeedNode(Base): class ScrapNode(Base):
__tablename__ = 'scrap_node'
ctime = Column(DateTime, default=datetime.utcnow) ctime = Column(DateTime, default=datetime.utcnow)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
length = Column(Integer, default=0) length = Column(Integer, default=0)
@ -21,20 +28,29 @@ class SeedNode(Base):
parent_id = Column(Integer, ForeignKey(id)) parent_id = Column(Integer, ForeignKey(id))
checksum = Column(String) checksum = Column(String)
stdout = Column(String) stdout = Column(String)
image = Column(Blob) image = Column(LargeBinary)
children = relationship( children = relationship(
"SeedNode", "ScrapNode",
cascade="all, delete-orphan", cascade="all, delete-orphan",
backref=backref("parent", remote_side=id), backref=backref("parent", remote_side=id),
collection_class=attribute_mapped_collection('name')) collection_class=attribute_mapped_collection('name'))
def __init__(self, *, child: bytes, parent: SeedNode = None): def __init__(self, *, child: bytes, parent_id: int = None):
if parent: self.parent_id = parent_id
self.parent_id = parent.id
self.image = child self.image = child
self.length = len(child) self.length = len(child)
self.sha1sum
def __repr__(self):
values = {
'id': self.id,
'checksum': self.checksum,
'length': self.length,
'parent_id': self.parent_id,
}
return str(values)
@property @property
def sha1sum(self): def sha1sum(self):

View File

@ -4,9 +4,12 @@ from datetime import datetime
from multiprocessing import Process, Queue from multiprocessing import Process, Queue
from pathlib import Path from pathlib import Path
from queue import Empty from queue import Empty
from sqlalchemy import exists, desc
from tempfile import TemporaryDirectory
import logging import logging
from .mutation import generation, flip, seed_shell from .mutation import generation, flip, seed_shell
from .orm import db_config, ScrapNode
def sins(): def sins():
@ -53,19 +56,47 @@ def sins():
with seed.open('rb') as seed_file: with seed.open('rb') as seed_file:
seed_data = seed_file.read() seed_data = seed_file.read()
logger.info(f'seed:\n{seed_data}') seed = ScrapNode(child=seed_data)
logger.info(f'seed:\n{seed}')
if args.output:
db_path = Path(f'{args.output}/sins.sqlite')
else:
temp_dir = TemporaryDirectory()
db_path = Path(f'{temp_dir.name}/sins.sqlite')
session = db_config(db_path)
logger.info(f'db_path: {db_path}')
if args.seed:
exists = session.query(ScrapNode).filter(ScrapNode.checksum == seed.checksum)
if exists:
seed = exists[0]
else:
session.add(seed)
session.commit()
else:
recent = session.query(ScrapNode).order_by(desc('ctime')).first()
if recent:
seed = recent
parent = seed
queue = Queue() queue = Queue()
while True: while True:
lineage = 0 lineage = 0
seed_flipped = flip(seed_data) scrap = flip(parent.image)
while lineage < args.lineage: while lineage < args.lineage:
logger.info(f'lineage: {lineage}') logger.info(f'lineage: {lineage}')
result = None result = None
proc = Process(target=generation, args=(queue, seed_flipped)) proc = Process(target=generation, args=(queue, scrap))
proc.start() proc.start()
try: try:
result = queue.get(timeout=1) result = queue.get(timeout=1)
@ -77,7 +108,10 @@ def sins():
lineage += 1 lineage += 1
continue continue
logger.info(f'scrap:\n{seed_flipped}') parent = ScrapNode(child=scrap, parent_id=parent.id)
logger.info(f'result: {result}') parent.length = result
session.add(seed)
session.commit()
logger.info(f'scrap:\n{parent}')
lineage = 0 lineage = 0
seed_flipped = flip(seed_flipped) scrap = flip(parent.image)