diff --git a/CMakeLists.txt b/CMakeLists.txt index c0b152ba7..6d9f9768b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -276,7 +276,7 @@ install( # set(MODEL_SCHEMA_PATH "${CMAKE_BINARY_DIR}/model-schema.yml") set(MODEL_JSONSCHEMA_PATH "${CMAKE_BINARY_DIR}/model-jsonschema.yml") -set(PYTHON_GENERATED_MODEL_PATH revng/model/v1/_generated.py) +set(PYTHON_GENERATED_MODEL_PATH revng/model/_generated.py) # # Enable CTest diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 2e53405ce..860bf8342 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -113,7 +113,7 @@ python_module(TARGET_NAME python-top-internal-module WHEEL revng_internal # Install revng.model (including autogenerated classes) # set(PYTHON_MODEL_FILES revng/model/__init__.py revng/model/metaaddress.py - revng/model/v1/__init__.py revng/model/v1/external.py) + revng/model/__init__.py revng/model/external.py) python_module( TARGET_NAME python-model diff --git a/python/revng/model/README.md b/python/revng/model/README.md index e52da03c8..d4c477194 100644 --- a/python/revng/model/README.md +++ b/python/revng/model/README.md @@ -12,11 +12,4 @@ with open("/path/to/model.yaml") as f: model = yaml.load(f, Loader=m.YamlLoader) ``` -If you need to access a specific version of the model you can import it like so: - -```python -import yaml -from revng.model import v1 - -yaml.load(..., Loader=v1.YamlLoader) -``` +It is not possible to import a model with a schema version other than the latest. diff --git a/python/revng/model/__init__.py b/python/revng/model/__init__.py index 31ca3804b..7cf9c9e4c 100644 --- a/python/revng/model/__init__.py +++ b/python/revng/model/__init__.py @@ -1,7 +1,29 @@ # # This file is distributed under the MIT License. See LICENSE.md for details. # +# flake8: noqa: F405 +# type: ignore -from .v1 import * # noqa: F401,F403 +from revng.tupletree import Reference, enum_value_to_index, init_reference_yaml_classes -_last_version = "v1" +from . import _generated +from ._generated import * +from .metaaddress import * + +init_metaaddress_yaml_classes(YamlLoader, YamlDumper) +init_reference_yaml_classes(YamlLoader, YamlDumper) + + +class Binary(_generated.Binary): + @classmethod + def get_reference_str(cls, t): + if hasattr(t, "Kind"): + typename = t.Kind.value + else: + typename = type(t).__name__ + return f"/TypeDefinitions/{t.ID}-{typename}" + + +# Since we subclassed them we need to re-register their constructors and representers +YamlLoader.add_constructor("!Binary", Binary.yaml_constructor) +YamlDumper.add_representer(Binary, Binary.yaml_representer) diff --git a/tests/tuple-tree-generator/python/v1.yml b/python/revng/model/external.py similarity index 61% rename from tests/tuple-tree-generator/python/v1.yml rename to python/revng/model/external.py index 4c76fc7f9..f2929bf60 100644 --- a/tests/tuple-tree-generator/python/v1.yml +++ b/python/revng/model/external.py @@ -2,4 +2,4 @@ # This file is distributed under the MIT License. See LICENSE.md for details. # -propertyA: somethingsomething +from .metaaddress import MetaAddress # noqa: F401 diff --git a/python/revng/model/v1/__init__.py b/python/revng/model/v1/__init__.py deleted file mode 100644 index 83184eaf5..000000000 --- a/python/revng/model/v1/__init__.py +++ /dev/null @@ -1,29 +0,0 @@ -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# -# flake8: noqa: F405 -# type: ignore - -from revng.tupletree import Reference, enum_value_to_index, init_reference_yaml_classes - -from ..metaaddress import * -from . import _generated -from ._generated import * - -init_metaaddress_yaml_classes(YamlLoader, YamlDumper) -init_reference_yaml_classes(YamlLoader, YamlDumper) - - -class Binary(_generated.Binary): - @classmethod - def get_reference_str(cls, t): - if hasattr(t, "Kind"): - typename = t.Kind.value - else: - typename = type(t).__name__ - return f"/TypeDefinitions/{t.ID}-{typename}" - - -# Since we subclassed them we need to re-register their constructors and representers -YamlLoader.add_constructor("!Binary", Binary.yaml_constructor) -YamlDumper.add_representer(Binary, Binary.yaml_representer) diff --git a/python/revng/model/v1/external.py b/python/revng/model/v1/external.py deleted file mode 100644 index c6de14b11..000000000 --- a/python/revng/model/v1/external.py +++ /dev/null @@ -1,5 +0,0 @@ -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -from ..metaaddress import MetaAddress # noqa: F401 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a4668d255..990389715 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,5 +4,4 @@ add_subdirectory(abi) add_subdirectory(pipeline) -add_subdirectory(tuple-tree-generator) add_subdirectory(unit) diff --git a/tests/tuple-tree-generator/CMakeLists.txt b/tests/tuple-tree-generator/CMakeLists.txt deleted file mode 100644 index fe26b69ee..000000000 --- a/tests/tuple-tree-generator/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -add_subdirectory(python) diff --git a/tests/tuple-tree-generator/python/CMakeLists.txt b/tests/tuple-tree-generator/python/CMakeLists.txt deleted file mode 100644 index bef549110..000000000 --- a/tests/tuple-tree-generator/python/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -revng_add_test(NAME tuple-tree-generator-python-test-multiple-versions COMMAND - "${CMAKE_CURRENT_SOURCE_DIR}/test.sh" "${CMAKE_SOURCE_DIR}") -set_tests_properties(tuple-tree-generator-python-test-multiple-versions - PROPERTIES LABELS "unit") diff --git a/tests/tuple-tree-generator/python/deserialize_multiple_versions.py b/tests/tuple-tree-generator/python/deserialize_multiple_versions.py deleted file mode 100755 index 6f475bf21..000000000 --- a/tests/tuple-tree-generator/python/deserialize_multiple_versions.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python3 -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -import yaml -from testmodule.v1 import RootType as RootV1 -from testmodule.v1 import YamlLoader as YamlLoaderV1 -from testmodule.v2 import RootType as RootV2 -from testmodule.v2 import YamlLoader as YamlLoaderV2 - - -def assert_parsing_fails(file, loader): - try: - yaml.load(file, Loader=loader) - except Exception: - return - - raise Exception("Parsing did not fail") - - -def test_deserialize_multiple_versions(): - """Tests that the custom YAML loaders can be used to deserialize multiple conflicting versions - at the same time. Also tests that deserializing using an invalid version fails. - """ - with open("v1.yml", encoding="utf-8") as f: - v1_serialized = yaml.load(f, Loader=YamlLoaderV1) - print(f"Type: {type(v1_serialized)}") - assert isinstance(v1_serialized, RootV1) - with open("v2.yml", encoding="utf-8") as f: - v2_serialized = yaml.load(f, Loader=YamlLoaderV2) - assert isinstance(v2_serialized, RootV2) - - with open("v1.yml", encoding="utf-8") as f: - assert_parsing_fails(f, YamlLoaderV2) - with open("v2.yml", encoding="utf-8") as f: - assert_parsing_fails(f, YamlLoaderV1) - - print("test_deserialize_multiple_versions: OK") - - -test_deserialize_multiple_versions() diff --git a/tests/tuple-tree-generator/python/test.sh b/tests/tuple-tree-generator/python/test.sh deleted file mode 100755 index 606525186..000000000 --- a/tests/tuple-tree-generator/python/test.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -set -euo pipefail - -function log() { - echo "$1" > /dev/stderr -} - -SOURCE_ROOT="$1" -SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) - -if ! test -d "$SOURCE_ROOT"; then - log "Usage: $0 SOURCE_ROOT" - exit 1 -fi - -rm -rf testmodule - -mkdir testmodule -mkdir testmodule/v1 -mkdir testmodule/v2 - -# Copy required files from the model module in the test module directory -cp -ar "$SOURCE_ROOT/python/revng/model/metaaddress.py" testmodule -touch testmodule/__init__.py - -# Generate python model for v1 and v2 -for INDEX in 1 2; do - "$SOURCE_ROOT/scripts/tuple_tree_generator/tuple-tree-generate-python.py" \ - --namespace dummy \ - --root-type RootType \ - --output "testmodule/v$INDEX/__init__.py" \ - --string-type "string" \ - "$SCRIPT_DIR/v${INDEX}_schema.yml" - - cp "$SOURCE_ROOT/python/revng/model/v1/external.py" "testmodule/v${INDEX}/external.py" -done - -export PYTHONPATH="$PWD:$SOURCE_ROOT/python:${PYTHONPATH:+:${PYTHONPATH}}" -cd "$SCRIPT_DIR" -"$SCRIPT_DIR/deserialize_multiple_versions.py" diff --git a/tests/tuple-tree-generator/python/v1_schema.yml b/tests/tuple-tree-generator/python/v1_schema.yml deleted file mode 100644 index fb745d44c..000000000 --- a/tests/tuple-tree-generator/python/v1_schema.yml +++ /dev/null @@ -1,11 +0,0 @@ -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -version: 1 -definitions: - - name: RootType - type: struct - fields: - - name: propertyA - type: string diff --git a/tests/tuple-tree-generator/python/v2.yml b/tests/tuple-tree-generator/python/v2.yml deleted file mode 100644 index f6fd94114..000000000 --- a/tests/tuple-tree-generator/python/v2.yml +++ /dev/null @@ -1,5 +0,0 @@ -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -propertyB: somethingsomething diff --git a/tests/tuple-tree-generator/python/v2_schema.yml b/tests/tuple-tree-generator/python/v2_schema.yml deleted file mode 100644 index 6bbfdfee2..000000000 --- a/tests/tuple-tree-generator/python/v2_schema.yml +++ /dev/null @@ -1,11 +0,0 @@ -# -# This file is distributed under the MIT License. See LICENSE.md for details. -# - -version: 1 -definitions: - - name: RootType - type: struct - fields: - - name: propertyB - type: string