Commit a9edda0e authored by canterburym's avatar canterburym
Browse files

Restructing XSD compilation

parent 3d10ba4a
Pipeline #11026 failed with stage
in 19 seconds
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
</ns1:targetIdentifiers> </ns1:targetIdentifiers>
<ns1:deliveryType>X2andX3</ns1:deliveryType> <ns1:deliveryType>X2andX3</ns1:deliveryType>
<ns1:listOfDIDs> <ns1:listOfDIDs>
<ns1:dId>1eb4406a-8b28-41fd-8f0f-e1a0a3eb5ff9</ns1:dId> <ns1:dId>1eb4406a-8b28-41fd-8f0f-e1a0a3eb5ff9foooo</ns1:dId>
</ns1:listOfDIDs> </ns1:listOfDIDs>
</ns1:taskDetails> </ns1:taskDetails>
<ns1:taskStatus> <ns1:taskStatus>
......
[ [
{ {
"coreSchema" : "RDMessage.xsd", "coreSchema" : "102657/RDMessage.xsd",
"supportingSchemas" : [], "supportingSchemas" : [],
"exampleFiles" : [] "exampleFiles" : []
},
{
"coreSchema" : "103280/TS_103_280.xsd",
"supportingSchemas" : [],
"exampleFiles" : []
},
{
"coreSchema" : "103221-1/TS_103_221_01.xsd",
"supportingSchemas" : [
"103221-1/TS_103_221_01.xsd",
"103221-1/TS_103_221_01_HashedID.xsd",
"103280/TS_103_280.xsd",
"103221-1/examples/ExampleGenericObjects.xsd"
],
"exampleFiles" : [
"103221-1/examples"
]
} }
] ]
\ No newline at end of file
import json
import logging import logging
from pathlib import Path from pathlib import Path
...@@ -107,46 +108,110 @@ def processResults (results, stageName): ...@@ -107,46 +108,110 @@ def processResults (results, stageName):
return errorCount return errorCount
if __name__ == '__main__': def syntaxCheckXSD (fileList):
logging.basicConfig(level=logging.DEBUG) results = {}
logging.info('Searching for XSD files')
fileList = list(Path(".").rglob("*.xsd")) + list(Path(".").rglob("*.xsd"))
logging.info(f'{len(fileList)} XSD files found')
for file in fileList: for file in fileList:
logging.debug(f' {file}') try:
logging.info(f"Syntax checking {str(file)}")
ignoreList = Path('testing/xsd_ignore.txt').read_text().splitlines()
ignoredFiles = []
for ignore in ignoreList:
logging.debug(f'Ignoring pattern {ignore}')
for file in fileList:
if ignore in str(file):
ignoredFiles.append(file)
logging.debug(f" Ignoring {str(file)} as contains {ignore}")
ignoredFiles = list(set(ignoredFiles))
logging.info(f'{len(ignoredFiles)} files ignored')
for file in ignoredFiles:
logging.debug(f' {file}')
fileList = [file for file in fileList if file not in ignoredFiles]
logging.info(f'{len(fileList)} files to process')
for file in fileList:
logging.debug(f' {file}')
if len(fileList) == 0: schema = XMLSchema(str(file), validation="skip")
logging.warning ("No files specified") results[str(file)] = {
exit(0) 'ok' : len(schema.all_errors) == 0,
'message' : None if len(schema.all_errors) == 0 else [{'message' : f"{etree_tostring(e.elem, e.namespaces, ' ', 20)} - {e.message}"} for e in schema.all_errors]
logging.info("Parsing ASN1 files") }
parseResults = syntaxCheckXSD(fileList) except XMLSchemaParseError as ex:
if processResults(parseResults, "Parsing") > 0: logging.warning(str(file) + ": Failed validation ({0})".format(ex.message))
exit(-1) results[str(file)] = {
'ok' : False,
'message' : f"{ex!r}"
}
return results
logging.info ("Getting compile targets")
compileTargets = json.loads(Path('testing/asn_compile_targets.json').read_text())
logging.info (f"{len(compileTargets)} compile targets found")
compileResults = compileAllTargets(compileTargets) if __name__ == '__main__':
if processResults(compileResults, "Compiling") > 0: #logging.basicConfig(level=logging.DEBUG)
compileTargets = json.loads(Path('testing/xsd_compile_targets.json').read_text())
results = {}
for target in compileTargets:
coreFile = target['coreSchema']
logging.info(f"Attempting to compile {coreFile}")
schemaLocations = []
for supportSchema in target['supportingSchemas']:
logging.debug(f"Adding supporting schema {supportSchema}")
try:
xs = XMLSchema(supportSchema, validation='skip')
schemaLocations.append((xs.default_namespace, str(Path(supportSchema).resolve())))
logging.info(" [ {0} -> {1} ]".format(xs.default_namespace, supportSchema))
except Exception as ex:
logging.warning (" [ {0} exception parsing: {1} ]".format(supportSchema, ex))
results[coreFile] = {
'ok' : False,
'message' : f"{ex!r}"
}
break
try:
schema = XMLSchema(coreFile, locations = schemaLocations, validation="strict")
results[coreFile] = {
'ok' : len(schema.all_errors) == 0,
'message' : None if len(schema.all_errors) == 0 else [{'message' : f"{etree_tostring(e.elem, e.namespaces, ' ', 20)} - {e.message}"} for e in schema.all_errors]
}
target["schemaInstance"] = schema
except Exception as ex:
results[coreFile] = {
'ok' : False,
'message' : f"{ex!r}"
}
continue
if (processResults(results, "Compile") > 0):
exit(-1) exit(-1)
results = {}
for target in compileTargets:
schema = target["schemaInstance"]
testResults = {}
failureCount = 0
logging.info (f"Validating example {len(target['exampleFiles'])} entries for {target['coreSchema']}")
for example in target["exampleFiles"]:
examplePath = Path(example)
if examplePath.is_dir:
logging.debug (f"Expanding {str(examplePath)}")
testFiles = list(examplePath.rglob("./*.xml"))
else:
testFiles = [examplePath]
logging.debug(f"Found {len(testFiles)} test files")
for test in testFiles:
logging.debug(f"Validating {str(test)} against schema")
try:
errors = list(schema.iter_errors(str(test)))
testResults[test] = [f"{etree_tostring(e.elem, e.namespaces, ' ', 20)} - {e.message}" for e in errors]
failureCount += len(errors)
except Exception as ex:
testResults[test] = [f"{ex!r}"]
failureCount += 1
results[target['coreSchema']] = {
'ok' : failureCount == 0,
'testResults' : testResults,
'failureCount' : failureCount
}
print(f"{'-':-<75}")
print(f"Validation results:")
print(f"{'-':-<75}")
totalErrors = 0
for filename, result in results.items():
print (f"{filename:.<70}{'..OK' if result['ok'] else 'FAIL'}")
totalErrors += result['failureCount']
for testFile, testResult in result['testResults'].items():
print(f" {str(testFile):.<65}{'..OK' if len(testResult) == 0 else 'FAIL'}")
for tr in testResult:
print(f" {tr}")
print(f"{'-':-<75}")
print(f"Validation errors: {totalErrors}")
print(f"{'-':-<75}")
exit(totalErrors > 0)
Supports Markdown
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