49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
from datetime import datetime
|
|
from sqlalchemy import Blob, Column, ForeignKey, Integer, String, DateTime, create_engine, exists, desc
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import Session, relationship, backref
|
|
from sqlalchemy.orm.collections import attribute_mapped_collection
|
|
import logging
|
|
from hashlib import sha1
|
|
|
|
logger = logging.getLogger('sins')
|
|
now = '{0:%Y%m%dT%H%M%S}'.format(datetime.utcnow())
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
class SeedNode(Base):
|
|
ctime = Column(DateTime, default=datetime.utcnow)
|
|
id = Column(Integer, primary_key=True)
|
|
length = Column(Integer, default=0)
|
|
mtime = Column(DateTime, onupdate=datetime.utcnow)
|
|
parent_id = Column(Integer, ForeignKey(id))
|
|
checksum = Column(String)
|
|
stdout = Column(String)
|
|
image = Column(Blob)
|
|
|
|
children = relationship(
|
|
"SeedNode",
|
|
cascade="all, delete-orphan",
|
|
backref=backref("parent", remote_side=id),
|
|
collection_class=attribute_mapped_collection('name'))
|
|
|
|
def __init__(self, *, child: bytes, parent: SeedNode = None):
|
|
if parent:
|
|
self.parent_id = parent.id
|
|
|
|
self.image = child
|
|
self.length = len(child)
|
|
|
|
@property
|
|
def sha1sum(self):
|
|
if self.checksum:
|
|
return self.checksum
|
|
|
|
checksum = sha1()
|
|
checksum.update(self.image)
|
|
self.checksum = checksum.hexdigest()
|
|
|
|
return self.checksum
|