Commit 6482e2db authored by canterburym's avatar canterburym

Updated XSD checking

parent ba83afab
Pipeline #3142 passed with stage
in 10 seconds
......@@ -70,7 +70,7 @@ def validateAllASN1FilesInPath (path):
if __name__ == '__main__':
parseErrors, compileErrors = validateAllASN1FilesInPath("./")
parseErrorCount = 0
print ("Parser checks:")
print ("ASN.1 Parser checks:")
print ("-----------------------------")
for filename, errors in parseErrors.items():
if len(errors) > 0:
......@@ -81,7 +81,7 @@ if __name__ == '__main__':
print (f"{filename}: OK")
print ("-----------------------------")
print ("Compilation:")
print ("ASN.1 Compilation:")
print ("-----------------------------")
if len(compileErrors) > 0:
for error in compileErrors:
import logging
logging.basicConfig(format="[%(levelname)s] %(message)s", level=logging.INFO)
import glob
import sys
from pathlib import Path
from pprint import pprint
if __name__ == '__main__':
from lxml import etree
from xml.etree.ElementTree import ParseError
from xmlschema import XMLSchema, XMLSchemaParseError
def BuildSchemaDictonary (fileList):
if len(fileList) == 0:"No schema files provided")
return []"Schema locations:")
schemaLocations = []
for schemaFile in fileList:
xs = XMLSchema(schemaFile, validation='skip')
schemaLocations.append((xs.default_namespace, str(Path(schemaFile).resolve())))" [ {0} -> {1} ]".format(xs.default_namespace, schemaFile))
except ParseError as ex:
logging.warning (" [ {0} failed to parse: {1} ]".format(schemaFile, ex))
return schemaLocations
def BuildSchema (coreFile, fileList = None):
schemaLocations = []
if fileList and len(fileList) > 0:
schemaLocations = BuildSchemaDictonary(fileList)
coreSchema = XMLSchema(str(Path(coreFile)), locations=schemaLocations)
return coreSchema
if sys.version_info <= (3, 5):
sys.exit('ERROR: You need at least Python 3.5 to run this tool')
from lxml import etree
except ImportError:
sys.exit('ERROR: You need to install the Python lxml library')
def ValidateXSDFiles (fileList):
if len(fileList) == 0:"No schema files provided")
return {}
schemaLocations = BuildSchemaDictonary(fileList)
errors = {}
import xmlschema
except ImportError:
sys.exit('ERROR: You need to install the xml schema library')"Schema validation:")
for schemaFile in fileList:
schema = XMLSchema(schemaFile, locations = schemaLocations) + ": OK")
errors[schemaFile] = []
except XMLSchemaParseError as ex:
logging.warning(schemaFile + ": Failed validation ({0})".format(ex.message))
if (ex.schema_url) and (ex.schema_url != ex.origin_url):
logging.warning(" Error comes from {0}, suppressing".format(ex.schema_url))
errors[schemaFile] = [ex]
return errors
schemaFiles = glob.glob('*.xsd')
def ValidateAllXSDFilesInPath (path):
globPattern = str(Path(path)) + '/*.xsd'"Searching: " + globPattern)
schemaGlob = glob.glob(globPattern, recursive=True)
return ValidateXSDFiles(schemaGlob)
def ValidateInstanceDocuments (coreFile, supportingSchemas, instanceDocs):
if (instanceDocs is None) or len(instanceDocs) == 0:
logging.warning ("No instance documents provided")
return []
schema = BuildSchema(coreFile, supportingSchemas)
errors = []
for instanceDoc in instanceDocs:
schema.validate(instanceDoc) ("{0} passed validation".format(instanceDoc))
except Exception as ex:
logging.error ("{0} failed validation: {1}".format(instanceDoc, ex))
return errors
if __name__ == '__main__':
results = ValidateAllXSDFilesInPath("./")
for schemaFile in schemaFiles:
print("Checking file: {0}".format(schemaFile), end="")
xs = xmlschema.XMLSchema(schemaFile)
print(" OK")
print ("XSD validation checks:")
print ("-----------------------------")
errorCount = 0
for fileName, errors in results.items():
if len(errors) > 0:
errorCount += len(errors)
print (f" {fileName}: {len(errors)} errors")
for error in errors:
if isinstance(error, XMLSchemaParseError):
print (error.msg)
print (f" {str(error)}")
print (f" {fileName}: OK")
print ("{0} XSD schemas checked".format(len(schemaFiles)))
\ No newline at end of file
print ("-----------------------------")
print (f"{errorCount} errors detected")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment