Skip to content

Commit

Permalink
created many to many model component relation
Browse files Browse the repository at this point in the history
  • Loading branch information
u7384629 committed May 9, 2024
1 parent 90fbd2c commit 0f110c4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
18 changes: 15 additions & 3 deletions tools/release_provenance/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from sqlalchemy import (
DateTime, Text, String, Column, ForeignKey )
DateTime, Text, String, Column, ForeignKey, Table, UniqueConstraint, Integer )
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
Expand Down Expand Up @@ -28,7 +28,8 @@ class ComponentBuild(Base):
install_path = Column(String, nullable=False, unique=True)
created_at = Column(DateTime, nullable=False)
release_url = Column(Text, nullable=False)
model_build = Column(String, ForeignKey("model_build.spack_hash"))
model_build = relationship('ModelBuild', secondary="model_component", back_populates='component_build')




Expand All @@ -38,7 +39,8 @@ class ModelBuild(Base):
spack_hash = Column(String, primary_key=True, index=True)
spec = Column(String, nullable=False)
spack_version = Column(String, ForeignKey("spack_version.commit"))
component_build = relationship('ComponentBuild')
component_build = relationship('ComponentBuild', secondary="model_component", back_populates='model_build')



class SpackVersion(Base):
Expand All @@ -47,3 +49,13 @@ class SpackVersion(Base):
commit = Column(String, primary_key=True, index=True)
version = Column(String, nullable=False)
model_build = relationship('ModelBuild')


model_component_association = Table(
"model_component",
Base.metadata,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("model_build", ForeignKey(ModelBuild.spack_hash)),
Column("component_build", ForeignKey(ComponentBuild.spack_hash)),
UniqueConstraint('model_build', 'component_build', name='uix_1')
)
53 changes: 26 additions & 27 deletions tools/release_provenance/save_release.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
from models import *
import json
import sys
from sqlalchemy.orm.exc import NoResultFound

session = create_session()


def read_release_data(filename):
with open(filename) as release_data:
return json.load(release_data)

def get_component_build(release_data, session):
component_build = ComponentBuild()
component_build.model_build = get_model_build(release_data["model_build"], session)
component_build.created_at = release_data["created_at"]
component_build.install_path = release_data["install_path"]
component_build.spack_hash = release_data["spack_hash"]
component_build.spec = release_data["spec"]
component_build.release_url = release_data["release_url"]
def get_component_build(release_data):
component_build_data = release_data["component_build"]
component_build = session.query(ComponentBuild).get(component_build_data["spack_hash"])

if component_build == None:
component_build = ComponentBuild()
component_build.created_at = component_build_data["created_at"]
component_build.install_path = component_build_data["install_path"]
component_build.spack_hash = component_build_data["spack_hash"]
component_build.spec = component_build_data["spec"]
component_build.release_url = component_build_data["release_url"]

component_build.model_build.append(get_model_build(release_data["model_build"]))
return component_build

def get_model_build(model_build_data, session):
try:
model_build = session.query(ModelBuild).filter(
ModelBuild.spack_hash == model_build_data["spack_hash"]
).one()
except NoResultFound:
def get_model_build(model_build_data):
model_build = session.query(ModelBuild).get(model_build_data["spack_hash"])

if model_build == None:
model_build = ModelBuild()
model_build.spack_version = get_spack_version(model_build_data["spack_version"], session)
model_build.spack_version = get_spack_version(model_build_data["spack_version"])
model_build.spack_hash = model_build_data["spack_hash"]
model_build.spec = model_build_data["spec"]
session.add(model_build)
session.commit()

return model_build.spack_hash
return model_build

def get_spack_version(spack_version_data, session):
try:
spack_version = session.query(SpackVersion).filter(
SpackVersion.commit == spack_version_data["commit"]
).one()
except NoResultFound:
def get_spack_version(spack_version_data):
spack_version = session.query(SpackVersion).get(spack_version_data["commit"])
if spack_version == None:
spack_version = SpackVersion()
spack_version.commit = spack_version_data["commit"]
spack_version.version = spack_version_data["version"]
session.add(spack_version)
session.commit()

return spack_version.commit

def main():
session = create_session()
release_data_filename = sys.argv[1]
release_data = read_release_data(release_data_filename)
component_build = get_component_build(release_data, session)
component_build = get_component_build(release_data)
try:
session.add(component_build)
session.commit()
Expand Down
14 changes: 8 additions & 6 deletions tools/release_provenance/test_release_data.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"spack_hash": "ewcdbrfukblyjxpkhd3mfkj4yxfolal4",
"spec": "[email protected]=2023.11.09",
"component_build": {
"spack_hash": "ewcdbrfukblyjxpkhd3mfkj4yxfolal4",
"spec": "[email protected]=2023.11.09",
"install_path": "/g/data/vk83/apps/spack/0.20/release/linux-rocky8-x86_64/intel-19.0.5.281/",
"created_at": "2024/02/13 11:49:00",
"release_url": "https://github.com/ACCESS-NRI/ACCESS-OM2/releases"
},
"model_build": {
"spack_hash": "f4f2qe5b3c22q4dtzzqs5o2iygy435re",
"spec": "[email protected]=2023.11.23",
"spack_version": {
"commit": "6812713cf470b473a607f0de0e8e1cf53f804fb7",
"version": "0.20.3"
}
},
"install_path": "/g/data/vk83/apps/spack/0.20/release/linux-rocky8-x86_64/intel-19.0.5.281/",
"created_at": "2024/02/13 11:49:00",
"release_url": "https://github.com/ACCESS-NRI/ACCESS-OM2/releases"
}
}

0 comments on commit 0f110c4

Please sign in to comment.