2
0

validate_xml.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. if __name__ != "__main__":
  3. raise SystemExit(f'Utility script "{__file__}" should not be used as a module!')
  4. import argparse
  5. import sys
  6. import xmlschema # Third-party module. Automatically installed in associated pre-commit hook.
  7. sys.path.insert(0, "./")
  8. try:
  9. from methods import print_error
  10. except ImportError:
  11. raise SystemExit(f"Utility script {__file__} must be run from repository root!")
  12. def main():
  13. parser = argparse.ArgumentParser(description="Validate XML documents against `doc/class.xsd`")
  14. parser.add_argument("files", nargs="+", help="A list of XML files to parse")
  15. args = parser.parse_args()
  16. SCHEMA = xmlschema.XMLSchema("doc/class.xsd")
  17. ret = 0
  18. for file in args.files:
  19. try:
  20. SCHEMA.validate(file)
  21. except xmlschema.validators.exceptions.XMLSchemaValidationError as err:
  22. print_error(f'Validation failed for "{file}"!\n\n{err}')
  23. ret += 1
  24. return ret
  25. try:
  26. raise SystemExit(main())
  27. except KeyboardInterrupt:
  28. import os
  29. import signal
  30. signal.signal(signal.SIGINT, signal.SIG_DFL)
  31. os.kill(os.getpid(), signal.SIGINT)