Browse Source

Merge pull request #111052 from Repiteo/ci/pre-commit-xml-hook

CI: Add pre-commit hook for XML linting
Thaddeus Crews 1 week ago
parent
commit
367f9bf9e3
3 changed files with 51 additions and 16 deletions
  1. 0 6
      .github/workflows/static_checks.yml
  2. 7 10
      .pre-commit-config.yaml
  3. 44 0
      misc/scripts/validate_xml.py

+ 0 - 6
.github/workflows/static_checks.yml

@@ -35,9 +35,3 @@ jobs:
         uses: pre-commit/[email protected]
         with:
           extra_args: --files ${{ env.CHANGED_FILES }}
-
-      - name: Class reference schema checks
-        run: |
-          sudo apt-get update
-          sudo apt-get install libxml2-utils
-          xmllint --quiet --noout --schema doc/class.xsd doc/classes/*.xml modules/*/doc_classes/*.xml platform/*/doc_classes/*.xml

+ 7 - 10
.pre-commit-config.yaml

@@ -64,16 +64,6 @@ repos:
         files: ^core/extension/gdextension_interface\.json$
         args: ["--schemafile", "core/extension/gdextension_interface.schema.json"]
 
-  ### Requires Docker; look into alternative implementation.
-  # - repo: https://github.com/comkieffer/pre-commit-xmllint.git
-  #   rev: 1.0.0
-  #   hooks:
-  #     - id: xmllint
-  #       language: docker
-  #       types_or: [text]
-  #       files: ^(doc/classes|.*/doc_classes)/.*\.xml$
-  #       args: [--schema, doc/class.xsd]
-
   - repo: local
     hooks:
       - id: make-rst
@@ -99,6 +89,13 @@ repos:
         pass_filenames: false
         files: ^(gles3|glsl)_builders\.py$
 
+      - id: validate-xml
+        name: validate-xml
+        language: python
+        entry: python misc/scripts/validate_xml.py
+        files: ^(doc/classes|.*/doc_classes)/.*\.xml$
+        additional_dependencies: [xmlschema]
+
       - id: eslint
         name: eslint
         language: node

+ 44 - 0
misc/scripts/validate_xml.py

@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+
+if __name__ != "__main__":
+    raise SystemExit(f'Utility script "{__file__}" should not be used as a module!')
+
+import argparse
+import sys
+
+import xmlschema  # Third-party module. Automatically installed in associated pre-commit hook.
+
+sys.path.insert(0, "./")
+
+try:
+    from methods import print_error
+except ImportError:
+    raise SystemExit(f"Utility script {__file__} must be run from repository root!")
+
+
+def main():
+    parser = argparse.ArgumentParser(description="Validate XML documents against `doc/class.xsd`")
+    parser.add_argument("files", nargs="+", help="A list of XML files to parse")
+    args = parser.parse_args()
+
+    SCHEMA = xmlschema.XMLSchema("doc/class.xsd")
+    ret = 0
+
+    for file in args.files:
+        try:
+            SCHEMA.validate(file)
+        except xmlschema.validators.exceptions.XMLSchemaValidationError as err:
+            print_error(f'Validation failed for "{file}"!\n\n{err}')
+            ret += 1
+
+    return ret
+
+
+try:
+    raise SystemExit(main())
+except KeyboardInterrupt:
+    import os
+    import signal
+
+    signal.signal(signal.SIGINT, signal.SIG_DFL)
+    os.kill(os.getpid(), signal.SIGINT)