Skip to content

Convert LinkML to ttl during poetry install and poetry run pytest #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: feat/linkml
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3

import os
import subprocess
import sys
from linkml.generators.shaclgen import ShaclGenerator
import linkml._version as linkml_version


def convert_to_shacl(input_file: str, output_file: str):
# Generate shacl
shacl_shapes = ShaclGenerator(input_file).serialize()

comment = (
f"# This SHACL file was auto-generated with LinkML {linkml_version.__version__}.\n"
f"#Changes will be overwritten on install.\n"
f"# Source file: {input_file}\n"
"\n"
)

# Run the command and redirect output to the output file
with open(output_file, "w") as outfile:
outfile.write(comment)
outfile.write(shacl_shapes)


def convert_folder_recursively(path: str, force: bool = False):
# recursively iterate through subfolders searching for yaml files
for root, _, files, in os.walk(path):
for file in files:

# skip non-yaml files
if not file.endswith(".yaml"):
continue

input_file = os.path.join(root, file)
output_file = input_file.replace(".yaml", ".ttl")
if force or not os.path.exists(output_file) or os.path.getmtime(input_file) > os.path.getmtime(output_file):
print("Converting", input_file)
convert_to_shacl(input_file, output_file)


if __name__ == "__main__":
convert_folder_recursively("rocrate_validator/profiles", force="--force" in sys.argv)
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ pytest-xdist = "^3.6.1"
max-line-length = 120

[build-system]
requires = ["poetry-core"]
requires = ["poetry-core", "linkml"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.build]
generate-setup-file = false
script = "build.py"

[tool.poetry.scripts]
rocrate-validator = "rocrate_validator.cli:cli"

Expand Down

This file was deleted.

9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
# calculate the absolute path of the rocrate-validator package
# and add it to the system path
import os
import subprocess

from pytest import fixture
from pytest import fixture, hookimpl

import rocrate_validator.log as logging

Expand All @@ -37,6 +38,12 @@
PROFILES_PATH = os.path.abspath(f"{CURRENT_PATH}/../rocrate_validator/profiles")


@hookimpl(tryfirst=True)
def pytest_configure():
# make sure all linkml files are converted to shacl
subprocess.run(["python", "build.py"], check=True)


@fixture
def random_path():
return "/tmp/random_path"
Expand Down