Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cubicweb/owl2yams
1 result
Show changes
Commits on Source (5)
from typing import Iterable
import subprocess
import re
import os
from argparse import ArgumentParser
from rdflib import Graph, RDF, RDFS, OWL, XSD, URIRef # type: ignore
......@@ -120,9 +123,18 @@
return schema
def run_and_print_if_error(command):
res = subprocess.run(command, shell=True, capture_output=True)
if res.returncode != 0:
print(res.stdout.decode())
print(res.stderr.decode())
exit(1)
return res
def main():
parser = ArgumentParser()
parser.add_argument(
"--owl-model",
"-m",
default="owl2yams/model.owl",
......@@ -123,11 +135,12 @@
def main():
parser = ArgumentParser()
parser.add_argument(
"--owl-model",
"-m",
default="owl2yams/model.owl",
help="Specify the OWL file to translate",
)
parser.add_argument(
"--instance-name",
"-n",
default="owl_instance",
......@@ -129,11 +142,12 @@
)
parser.add_argument(
"--instance-name",
"-n",
default="owl_instance",
help="Specify the instance name for the CW instance",
)
parser.add_argument(
"--parse-format",
"-f",
choices=["turtle", "xml", "n3", "nquads", "nt", "trix"],
default="turtle",
......@@ -134,9 +148,16 @@
)
parser.add_argument(
"--parse-format",
"-f",
choices=["turtle", "xml", "n3", "nquads", "nt", "trix"],
default="turtle",
help="Specify the OWL file serialization",
)
parser.add_argument(
"--dry-run",
"-d",
action="store_true",
help="Print the YAMS schema only",
)
args = parser.parse_args()
......@@ -146,4 +167,61 @@
schema = owl_model_to_yams(owl_model, args.instance_name)
print(serialize_to_python(schema))
if args.dry_run:
print(serialize_to_python(schema))
exit(0)
cube_name = args.instance_name.replace("_", "-")
cube_master_folder = f"cubicweb-{cube_name}"
print(
f"creating the cube inside the folder {cube_master_folder}/ "
"(the schema is there)"
)
create_cube = (
f"cubicweb-ctl newcube {args.instance_name}"
' -s "cube representating {args.owl_model}"'
)
run_and_print_if_error(create_cube)
cube_subfolder = cube_master_folder.replace("-", "_")
with open(f"{cube_master_folder}/{cube_subfolder}/schema.py", "a") as f:
f.write(serialize_to_python(schema))
# should pip install the cube
pip_install_cube = f"pip install -e cubicweb-{cube_name}"
run_and_print_if_error(pip_install_cube)
print(
f"creating the instance {args.instance_name}. "
f"The parameters are in ~/etc/cubicweb.d/{args.instance_name}/"
)
create_instance = (
"CW_DB_DRIVER=sqlite cubicweb-ctl create "
f"{args.instance_name} {args.instance_name} -a --no-db-create"
)
run_and_print_if_error(create_instance)
db_init = (
"CW_DB_DRIVER=sqlite cubicweb-ctl db-create "
f"{args.instance_name} -a --drop=y"
)
run_and_print_if_error(db_init)
source_path = os.path.expanduser(f"~/etc/cubicweb.d/{args.instance_name}/sources")
# The source file has to be modified to include sqlite
driver_regexp = re.compile("db-driver.*")
admin_pass = re.compile("password=(.*)")
with open(source_path, "r") as f:
content = f.read()
replaced_content = driver_regexp.sub("db-driver=sqlite", content)
admin_pwd = next(admin_pass.finditer(content)).group(1)
with open(source_path, "w") as f:
f.write(replaced_content)
print(
"Congratulation ! You can run your instance with : "
f"`cubicweb-ctl pyramid -D -l info {args.instance_name}`"
)
print(f"(admin password: {admin_pwd} (from {source_path})")
......@@ -14,6 +14,7 @@
author = "Fabien Amarger"
author_email = "famarger@logilab.fr"
requires = {
"cubicweb": ">=3.30",
"yams": None,
"rdflib": None,
}
......