Skip to content

Commit

Permalink
ADD: schema deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudocubic committed Nov 7, 2024
1 parent 116d682 commit 04b9d9e
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 14 deletions.
85 changes: 84 additions & 1 deletion .github/workflows/deploy-docs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build Docs and Deploy
name: Build and Deploy

on:
push:
Expand Down Expand Up @@ -78,3 +78,86 @@ jobs:
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs/build/html

build-schema:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
lfs: true
- name: Checkout LFS objects
run: git lfs checkout
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install mdbtools
run: |
sudo apt-get update
sudo apt-get install -y mdbtools
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
echo "export PATH=$HOME/.local/bin:\$PATH" >> $GITHUB_ENV
- name: Cache Poetry dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pypoetry
key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
${{ runner.os }}-poetry-
- name: Install dependencies
run: |
poetry install
- name: Build Schema
run: |
poetry run python docs/source/scripts/schema_deploy.py # Update to the correct command
- name: Upload Schema as artifact
uses: actions/upload-artifact@v4
with:
name: schema
path: schema

deploy-schema:
needs: build-schema
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
lfs: true
- name: Download Schema artifact
uses: actions/download-artifact@v4
with:
name: schema
path: schema
- name: Set up Git user
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Create schema branch if it doesn't exist
run: |
git fetch origin
if ! git rev-parse --verify origin/schema; then
git checkout --orphan schema
git rm -rf .
mkdir schema
touch schema/.gitkeep
git add schema/.gitkeep
git commit -m "Initialize schema branch with schema folder"
git push -u origin schema
fi
- name: Switch to schema branch
run: |
git fetch origin schema
git checkout schema
- name: Copy schema artifact to schema folder
run: |
cp -R schema/* .
- name: Commit and push schema updates
run: |
git add schema
git commit -m "Update schema files"
git push origin schema
52 changes: 52 additions & 0 deletions docs/source/scripts/schema_deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import glob
import json
import os

import json_schema_for_humans.generate as Gen

from ravens.io import parse_uml_data
from ravens.cim_tools.common import build_package_exclusions, build_object_exclusions
from ravens.cim_tools.graph import build_generalization_graph, build_attribute_graph
from ravens.cim_tools.template import CIMTemplate
from ravens.schema.build_definitions import build_definitions
from ravens.schema.build_map import add_attributes_to_template
from ravens.schema.build_schema import build_schema_from_map
from ravens.schema.add_copyright_notice import add_cim_copyright_notice_to_decomposed_schemas
from ravens.schema.decompose_schema import Schemas


def build_schema_docs():
current_dir = os.path.dirname(os.path.abspath(__file__))
template_path = os.path.join(current_dir, "../../../ravens/cim_tools/cim_conversion_template.json")
xmi_path = os.path.join(current_dir, "../../../cim/iec61970cim17v40_iec61968cim13v13b_iec62325cim03v17b_CIM100.1.1.1_mgravens24v1.xmi")
schema_dir = os.path.join(current_dir, "../../../schema")

uml_data = parse_uml_data(xmi_path)

exclude_packages = build_package_exclusions(uml_data.packages, lambda x: any(str(x.Name).startswith(k) for k in ["Inf", "Mkt"]))
exclude_objects = build_object_exclusions(
uml_data.objects,
lambda x: any(str(x.Name).startswith(k) for k in ["Inf", "Mkt"]),
exclude_packages=exclude_packages,
)

schema = build_schema_from_map(
add_attributes_to_template(
CIMTemplate(template_path).template,
CIMTemplate(template_path).template,
uml_data,
build_generalization_graph(uml_data, exclude_packages, exclude_objects),
build_attribute_graph(uml_data, exclude_packages, exclude_objects),
)
)

schema["$defs"] = build_definitions(uml_data)

a = Schemas(schema)

add_cim_copyright_notice_to_decomposed_schemas(a.schemas, uml_data)

for k, v in a.schemas.items():
filename = k.split("/")[-1].replace(".json", "")
with open(os.path.join(schema_dir, f"{filename}.json"), "w") as f:
json.dump(v, f, indent=2)
26 changes: 13 additions & 13 deletions ravens/schema/decompose_schema.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from copy import deepcopy

_default_base_id_url = "https://raw.githubusercontent.com/lanl-ansi/MG-RAVENS/refs/heads/schema"
_default_base_uri = "https://raw.githubusercontent.com/lanl-ansi/MG-RAVENS/refs/heads/schema"
_schema_url = "https://json-schema.org/draft/2020-12/schema"


class Schemas:
def __init__(self, schema, base_id_url=_default_base_id_url):
def __init__(self, schema, base_id_uri=_default_base_uri):
self.schema = deepcopy(schema)
self.schemas = {}
self.base_id_url = base_id_url
self.base_id_uri = base_id_uri

self.decompose_schema(deepcopy(self.schema))
self.decompose_defs(deepcopy(self.schema).get("$defs", {}))
self.schemas[f"{self.base_id_url}/Root.json"].pop("$defs")
self.schemas[f"{self.base_id_uri}/Root.json"].pop("$defs")

self.insert_refs()

Expand All @@ -30,7 +30,7 @@ def decompose_schema(self, schema: dict, debug_key: str = None) -> str:
else:
print(debug_key, _schema.keys())

_schema["$id"] = f"{self.base_id_url}/{title}.json"
_schema["$id"] = f"{self.base_id_uri}/{title}.json"

if _schema.get("type", None) == "object":
for n in ["properties", "patternProperties"]:
Expand Down Expand Up @@ -68,7 +68,7 @@ def decompose_defs(self, defs: dict):
for k, v in defs.items():
_schema = deepcopy(v)
_schema["$schema"] = _schema_url
_schema["$id"] = f"{self.base_id_url}/{_schema["title"]}.json"
_schema["$id"] = f"{self.base_id_uri}/{_schema["title"]}.json"
self.schemas[_schema["$id"]] = _schema

def insert_refs(self):
Expand All @@ -92,14 +92,14 @@ def insert_refs(self):
self.schemas[schema_key]["properties"][k]["items"] = {"$ref": key}
elif v.get("$ref", "").startswith("#/$defs/"):
ref = v["$ref"].split("#/$defs/")[1]
if f"{self.base_id_url}/{ref}.json" in self.schemas:
self.schemas[schema_key]["properties"][k]["$ref"] = f"{self.base_id_url}/{ref}.json"
if f"{self.base_id_uri}/{ref}.json" in self.schemas:
self.schemas[schema_key]["properties"][k]["$ref"] = f"{self.base_id_uri}/{ref}.json"
elif "oneOf" in schema:
for i, item in enumerate(schema["oneOf"]):
if item.get("$ref", "").startswith("#/$defs/"):
ref = v["$ref"].split("#/$defs/")[1]
if f"{self.base_id_url}/{ref}.json" in self.schemas:
self.schemas[schema_key]["oneOf"][i]["$ref"] = f"{self.base_id_url}/{ref}.json"
if f"{self.base_id_uri}/{ref}.json" in self.schemas:
self.schemas[schema_key]["oneOf"][i]["$ref"] = f"{self.base_id_uri}/{ref}.json"
else:
key = item.get("title", "")
if key in self.schemas:
Expand All @@ -109,8 +109,8 @@ def insert_refs(self):
for i, item in enumerate(schema["items"]["oneOf"]):
if item.get("$ref", "").startswith("#/$defs/"):
ref = v["$ref"].split("#/$defs/")[1]
if f"{self.base_id_url}/{ref}.json" in self.schemas:
self.schemas[schema_key]["items"]["oneOf"][i]["$ref"] = f"{self.base_id_url}/{ref}.json"
if f"{self.base_id_uri}/{ref}.json" in self.schemas:
self.schemas[schema_key]["items"]["oneOf"][i]["$ref"] = f"{self.base_id_uri}/{ref}.json"
else:
key = item.get("$id", "")
if key in self.schemas:
Expand Down Expand Up @@ -158,7 +158,7 @@ def insert_refs(self):
with open("out/schema/test_schema.json", "w") as f:
json.dump(schema, f, indent=2)

a = Schemas(schema, base_id_url=f"file://{os.getcwd()}/out/schema/separate")
a = Schemas(schema, base_id_uri=f"file://{os.getcwd()}/out/schema/separate")

add_cim_copyright_notice_to_decomposed_schemas(a.schemas, uml_data)

Expand Down

0 comments on commit 04b9d9e

Please sign in to comment.