You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by "TomNewChao (via GitHub)" <gi...@apache.org> on 2026/10/13 09:44:44 UTC

[PR] build(plc4net): add model generation checks

TomNewChao opened a new pull request, #2814:
URL: https://github.com/apache/plc4x/pull/2814

   ## Summary
   
   Adds Maven profiles as the shared local and CI interface for regenerating PLC4Net protocol models and detecting checked-in model drift. The underlying Python script regenerates the Modbus and S7 models, while the check mode compares temporary output with the checked-in model files.
   
   The PR also removes the dangling `s7-test` project entry from `plc4net.sln`. The project is intentionally delivered in a later split PR; leaving its reference in the solution made the CI restore step fail with `MSB3202` after #2813 merged.
   
   ## Validation
   
   - `dotnet restore plc4net/plc4net.sln`
   - `dotnet build plc4net/plc4net.sln --configuration Debug --no-restore --no-incremental`
   - `dotnet test plc4net/plc4net.sln --configuration Debug --no-build` (657 tests)
   - `./mvnw -B -f plc4net/pom.xml -Pcheck-generated-code generate-sources`


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build(plc4net): add model generation checks

Posted by "TomNewChao (via GitHub)" <gi...@apache.org>.
TomNewChao commented on code in PR #2814:
URL: https://github.com/apache/plc4x/pull/2814#discussion_r4257925324


##########
plc4net/tools/code-gen/generate_protocol_models.py:
##########
@@ -0,0 +1,141 @@
+#!/usr/bin/env python3
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+"""Regenerate PLC4Net protocol models or verify that they are up to date."""
+
+import argparse
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+
+REPOSITORY_ROOT = Path(__file__).resolve().parents[3]
+PLC4NET_ROOT = REPOSITORY_ROOT / "plc4net"
+CODE_GENERATOR = PLC4NET_ROOT / "tools" / "code-gen"
+MODELS = (
+    (
+        "modbus",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "modbus"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "modbus"
+        / "modbus.mspec",
+        PLC4NET_ROOT / "drivers" / "modbus",
+        (),
+    ),
+    (
+        "s7",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "s7"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "s7"
+        / "s7.mspec",
+        PLC4NET_ROOT / "drivers" / "s7",
+        ("--manual-static-calls",),
+    ),
+)
+
+
+def generate(protocol, mspec, output, extra_arguments):
+    subprocess.run(
+        [
+            "dotnet",
+            "run",
+            "--project",
+            str(CODE_GENERATOR),
+            "--configuration",
+            "Release",
+            "--",
+            protocol,
+            str(mspec),
+            str(output),
+            *extra_arguments,
+        ],
+        check=True,
+        cwd=REPOSITORY_ROOT,
+    )
+
+
+def differs(expected, actual):
+    expected_files = {
+        path.relative_to(expected)
+        for path in expected.rglob("*")
+        if path.is_file()
+    }
+    actual_files = {
+        path.relative_to(actual)
+        for path in actual.rglob("*")
+        if path.is_file()
+    }
+    missing_files = expected_files - actual_files
+    unexpected_files = actual_files - expected_files
+    changed_files = [
+        path
+        for path in expected_files & actual_files
+        if (expected / path).read_bytes().replace(b"\r\n", b"\n")
+        != (actual / path).read_bytes().replace(b"\r\n", b"\n")
+    ]
+    if missing_files or unexpected_files or changed_files:
+        print(f"Generated model differs: {actual}", file=sys.stderr)
+        for path in sorted(missing_files):
+            print(f"  missing from generated output: {path}", file=sys.stderr)
+        for path in sorted(unexpected_files):
+            print(f"  unexpected generated output: {path}", file=sys.stderr)
+        for path in sorted(changed_files):
+            print(f"  changed generated file: {path}", file=sys.stderr)
+        return True
+    return False
+
+
+def main():
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument(
+        "--check",
+        action="store_true",
+        help="regenerate into a temporary directory and fail on checked-in model drift",
+    )
+    arguments = parser.parse_args()
+
+    if not arguments.check:
+        for protocol, mspec, driver_directory, extra_arguments in MODELS:
+            generate(protocol, mspec, driver_directory, extra_arguments)
+        return 0
+
+    with tempfile.TemporaryDirectory(prefix="plc4net-models-") as temporary_directory:
+        temporary_root = Path(temporary_directory)
+        has_drift = False
+        for protocol, mspec, driver_directory, extra_arguments in MODELS:
+            output = temporary_root / protocol
+            generate(protocol, mspec, output, extra_arguments)
+            has_drift = differs(output / "model", driver_directory / "model") or has_drift

Review Comment:
   Fixed in 35708b335. The comparison now explicitly takes checked-in output first and generated output second, and the call site follows that order. Missing and unexpected files are therefore reported in the correct direction.



##########
plc4net/pom.xml:
##########
@@ -38,6 +38,7 @@
 
   <properties>
     <project.build.outputTimestamp>2026-09-03T20:46:19Z</project.build.outputTimestamp>
+    <python.executable>python</python.executable>
   </properties>

Review Comment:
   Fixed in 35708b335. The README now documents the Maven property override: use -Dpython.executable=python3 when Python 3 is exposed only as python3.



##########
plc4net/tools/code-gen/generate_protocol_models.py:
##########
@@ -0,0 +1,141 @@
+#!/usr/bin/env python3
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+"""Regenerate PLC4Net protocol models or verify that they are up to date."""
+
+import argparse
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+
+REPOSITORY_ROOT = Path(__file__).resolve().parents[3]
+PLC4NET_ROOT = REPOSITORY_ROOT / "plc4net"
+CODE_GENERATOR = PLC4NET_ROOT / "tools" / "code-gen"
+MODELS = (
+    (
+        "modbus",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "modbus"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "modbus"
+        / "modbus.mspec",
+        PLC4NET_ROOT / "drivers" / "modbus",
+        (),
+    ),
+    (
+        "s7",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "s7"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "s7"
+        / "s7.mspec",
+        PLC4NET_ROOT / "drivers" / "s7",
+        ("--manual-static-calls",),
+    ),
+)
+
+
+def generate(protocol, mspec, output, extra_arguments):
+    subprocess.run(
+        [
+            "dotnet",
+            "run",
+            "--project",
+            str(CODE_GENERATOR),
+            "--configuration",
+            "Release",
+            "--",
+            protocol,
+            str(mspec),
+            str(output),
+            *extra_arguments,
+        ],
+        check=True,
+        cwd=REPOSITORY_ROOT,
+    )
+
+
+def differs(expected, actual):
+    expected_files = {
+        path.relative_to(expected)
+        for path in expected.rglob("*")
+        if path.is_file()
+    }
+    actual_files = {
+        path.relative_to(actual)
+        for path in actual.rglob("*")
+        if path.is_file()
+    }
+    missing_files = expected_files - actual_files
+    unexpected_files = actual_files - expected_files
+    changed_files = [
+        path
+        for path in expected_files & actual_files
+        if (expected / path).read_bytes().replace(b"\r\n", b"\n")
+        != (actual / path).read_bytes().replace(b"\r\n", b"\n")
+    ]
+    if missing_files or unexpected_files or changed_files:
+        print(f"Generated model differs: {actual}", file=sys.stderr)
+        for path in sorted(missing_files):
+            print(f"  missing from generated output: {path}", file=sys.stderr)
+        for path in sorted(unexpected_files):
+            print(f"  unexpected generated output: {path}", file=sys.stderr)
+        for path in sorted(changed_files):
+            print(f"  changed generated file: {path}", file=sys.stderr)

Review Comment:
   Fixed in 35708b335. Drift output now prints both the checked-in model path and the generated temporary path, alongside correctly directed missing, unexpected, and changed-file diagnostics.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build(plc4net): add model generation checks

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2814:
URL: https://github.com/apache/plc4x/pull/2814#discussion_r4257284312


##########
plc4net/tools/code-gen/generate_protocol_models.py:
##########
@@ -0,0 +1,141 @@
+#!/usr/bin/env python3
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+"""Regenerate PLC4Net protocol models or verify that they are up to date."""
+
+import argparse
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+
+REPOSITORY_ROOT = Path(__file__).resolve().parents[3]
+PLC4NET_ROOT = REPOSITORY_ROOT / "plc4net"
+CODE_GENERATOR = PLC4NET_ROOT / "tools" / "code-gen"
+MODELS = (
+    (
+        "modbus",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "modbus"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "modbus"
+        / "modbus.mspec",
+        PLC4NET_ROOT / "drivers" / "modbus",
+        (),
+    ),
+    (
+        "s7",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "s7"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "s7"
+        / "s7.mspec",
+        PLC4NET_ROOT / "drivers" / "s7",
+        ("--manual-static-calls",),
+    ),
+)
+
+
+def generate(protocol, mspec, output, extra_arguments):
+    subprocess.run(
+        [
+            "dotnet",
+            "run",
+            "--project",
+            str(CODE_GENERATOR),
+            "--configuration",
+            "Release",
+            "--",
+            protocol,
+            str(mspec),
+            str(output),
+            *extra_arguments,
+        ],
+        check=True,
+        cwd=REPOSITORY_ROOT,
+    )
+
+
+def differs(expected, actual):
+    expected_files = {
+        path.relative_to(expected)
+        for path in expected.rglob("*")
+        if path.is_file()
+    }
+    actual_files = {
+        path.relative_to(actual)
+        for path in actual.rglob("*")
+        if path.is_file()
+    }
+    missing_files = expected_files - actual_files
+    unexpected_files = actual_files - expected_files
+    changed_files = [
+        path
+        for path in expected_files & actual_files
+        if (expected / path).read_bytes().replace(b"\r\n", b"\n")
+        != (actual / path).read_bytes().replace(b"\r\n", b"\n")
+    ]
+    if missing_files or unexpected_files or changed_files:
+        print(f"Generated model differs: {actual}", file=sys.stderr)
+        for path in sorted(missing_files):
+            print(f"  missing from generated output: {path}", file=sys.stderr)
+        for path in sorted(unexpected_files):
+            print(f"  unexpected generated output: {path}", file=sys.stderr)
+        for path in sorted(changed_files):
+            print(f"  changed generated file: {path}", file=sys.stderr)
+        return True
+    return False
+
+
+def main():
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument(
+        "--check",
+        action="store_true",
+        help="regenerate into a temporary directory and fail on checked-in model drift",
+    )
+    arguments = parser.parse_args()
+
+    if not arguments.check:
+        for protocol, mspec, driver_directory, extra_arguments in MODELS:
+            generate(protocol, mspec, driver_directory, extra_arguments)
+        return 0
+
+    with tempfile.TemporaryDirectory(prefix="plc4net-models-") as temporary_directory:
+        temporary_root = Path(temporary_directory)
+        has_drift = False
+        for protocol, mspec, driver_directory, extra_arguments in MODELS:
+            output = temporary_root / protocol
+            generate(protocol, mspec, output, extra_arguments)
+            has_drift = differs(output / "model", driver_directory / "model") or has_drift

Review Comment:
   `differs(expected, actual)` computes “missing/unexpected/changed” as `expected` vs `actual`, but in check mode the call appears reversed: you pass the *generated* directory as `expected` and the *checked-in* directory as `actual`. This will invert “missing/unexpected” reporting and can make drift diagnostics misleading. Swap the argument order in the call (checked-in as expected, generated as actual), or rename parameters and update the set-diff logic/messages to match the intended semantics.



##########
plc4net/pom.xml:
##########
@@ -38,6 +38,7 @@
 
   <properties>
     <project.build.outputTimestamp>2026-09-03T20:46:19Z</project.build.outputTimestamp>
+    <python.executable>python</python.executable>
   </properties>

Review Comment:
   Defaulting `${python.executable}` to `python` can be brittle on systems where only `python3` is present (common in some Linux/macOS setups). Consider documenting the override (`-Dpython.executable=python3`) in the README section you added, or using a more portable default/resolution strategy so local regeneration doesn’t fail unexpectedly.



##########
plc4net/tools/code-gen/generate_protocol_models.py:
##########
@@ -0,0 +1,141 @@
+#!/usr/bin/env python3
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+"""Regenerate PLC4Net protocol models or verify that they are up to date."""
+
+import argparse
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+
+REPOSITORY_ROOT = Path(__file__).resolve().parents[3]
+PLC4NET_ROOT = REPOSITORY_ROOT / "plc4net"
+CODE_GENERATOR = PLC4NET_ROOT / "tools" / "code-gen"
+MODELS = (
+    (
+        "modbus",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "modbus"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "modbus"
+        / "modbus.mspec",
+        PLC4NET_ROOT / "drivers" / "modbus",
+        (),
+    ),
+    (
+        "s7",
+        REPOSITORY_ROOT
+        / "protocols"
+        / "s7"
+        / "src"
+        / "main"
+        / "resources"
+        / "protocols"
+        / "s7"
+        / "s7.mspec",
+        PLC4NET_ROOT / "drivers" / "s7",
+        ("--manual-static-calls",),
+    ),
+)
+
+
+def generate(protocol, mspec, output, extra_arguments):
+    subprocess.run(
+        [
+            "dotnet",
+            "run",
+            "--project",
+            str(CODE_GENERATOR),
+            "--configuration",
+            "Release",
+            "--",
+            protocol,
+            str(mspec),
+            str(output),
+            *extra_arguments,
+        ],
+        check=True,
+        cwd=REPOSITORY_ROOT,
+    )
+
+
+def differs(expected, actual):
+    expected_files = {
+        path.relative_to(expected)
+        for path in expected.rglob("*")
+        if path.is_file()
+    }
+    actual_files = {
+        path.relative_to(actual)
+        for path in actual.rglob("*")
+        if path.is_file()
+    }
+    missing_files = expected_files - actual_files
+    unexpected_files = actual_files - expected_files
+    changed_files = [
+        path
+        for path in expected_files & actual_files
+        if (expected / path).read_bytes().replace(b"\r\n", b"\n")
+        != (actual / path).read_bytes().replace(b"\r\n", b"\n")
+    ]
+    if missing_files or unexpected_files or changed_files:
+        print(f"Generated model differs: {actual}", file=sys.stderr)
+        for path in sorted(missing_files):
+            print(f"  missing from generated output: {path}", file=sys.stderr)
+        for path in sorted(unexpected_files):
+            print(f"  unexpected generated output: {path}", file=sys.stderr)
+        for path in sorted(changed_files):
+            print(f"  changed generated file: {path}", file=sys.stderr)

Review Comment:
   The drift output doesn’t print both paths being compared, and (given the current call site) the “missing from generated output / unexpected generated output” labels can be incorrect. After fixing the argument order, consider also printing both `expected` and `actual` roots (e.g., “expected checked-in: … / actual generated: …”) to make CI failures actionable and unambiguous.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build(plc4net): add model generation checks

Posted by "sruehl (via GitHub)" <gi...@apache.org>.
sruehl merged PR #2814:
URL: https://github.com/apache/plc4x/pull/2814


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@plc4x.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org