|
| 1 | +#!/usr/bin/env python |
| 2 | +#********************************************************************** |
| 3 | +# Copyright 2016 Crown Copyright |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +#********************************************************************** |
| 17 | + |
| 18 | + |
| 19 | +#********************************************************************** |
| 20 | +# Script to ensure all the version numbers in the event-logging |
| 21 | +# XMLSchema are valid |
| 22 | +#********************************************************************** |
| 23 | + |
| 24 | + |
| 25 | +import os |
| 26 | +import re |
| 27 | +import xml.etree.ElementTree as ET |
| 28 | + |
| 29 | +SCHEMA_FILENAME = "event-logging.xsd" |
| 30 | + |
| 31 | +root_path = os.path.dirname(os.path.realpath(__file__)) |
| 32 | + |
| 33 | +def getMinorVersion(versionStr): |
| 34 | + minorVer = re.match("[0-9]*\.([0-9]*)\..*", versionStr).group(1) |
| 35 | + return minorVer |
| 36 | + |
| 37 | +def validateVersions(): |
| 38 | + print "Validating file %s" % SCHEMA_FILENAME |
| 39 | + print "" |
| 40 | + |
| 41 | + # pattern = re.compile("xmlns:evt\"event-logging:.*\"") |
| 42 | + xsdFile = open(SCHEMA_FILENAME, 'r') |
| 43 | + filetext = xsdFile.read() |
| 44 | + xsdFile.close() |
| 45 | + matches = re.findall("xmlns:evt=\"event-logging:(.*)\"", filetext) |
| 46 | + if (len(matches) != 1): |
| 47 | + raise ValueError("Unexpected matches for evt namespace", matches) |
| 48 | + namespaceVersion = matches[0] |
| 49 | + print "namespace version: %s" % namespaceVersion |
| 50 | + |
| 51 | + xml_root = ET.parse(SCHEMA_FILENAME).getroot() |
| 52 | + |
| 53 | + targetNamespaceAttr = xml_root.get("targetNamespace") |
| 54 | + targetNamespaceVersion = re.match(".*:([0-9]*)$", targetNamespaceAttr).group(1) |
| 55 | + print "targetNamespace: %s" % targetNamespaceVersion |
| 56 | + |
| 57 | + versionAttrVersion = xml_root.get("version") |
| 58 | + print "version: %s" % versionAttrVersion |
| 59 | + |
| 60 | + idAttr = xml_root.get("id") |
| 61 | + idAttrVersion = re.match("event-logging-v([0-9\.]*)", idAttr).group(1) |
| 62 | + print "id: %s" % idAttrVersion |
| 63 | + |
| 64 | + ns = {'xs': 'http://www.w3.org/2001/XMLSchema'} |
| 65 | + enumVersions = [] |
| 66 | + print "Version enumerations:" |
| 67 | + for enumElm in xml_root.findall("./xs:simpleType[@name='VersionSimpleType']/xs:restriction/xs:enumeration", ns): |
| 68 | + print " %s" % enumElm.get("value") |
| 69 | + enumVersions.append(enumElm.get("value")) |
| 70 | + |
| 71 | + # print enumVersions |
| 72 | + |
| 73 | + if (namespaceVersion != targetNamespaceVersion): |
| 74 | + raise ValueError("namespace version and targetNamespace version do not match", namespaceVersion, targetNamespaceVersion) |
| 75 | + |
| 76 | + versionRegex = "[0-9]+\.[0-9]+\.[0-9]+" |
| 77 | + if (not re.match(versionRegex, versionAttrVersion)): |
| 78 | + raise ValueError("version attribute does not match the valid regex", versionAttrVersion, versionRegex) |
| 79 | + |
| 80 | + if (versionAttrVersion != idAttrVersion): |
| 81 | + raise ValueError("version attribute and id attribute do not match", versionAttrVersion, idAttrVersion) |
| 82 | + |
| 83 | + if (not versionAttrVersion.startswith(targetNamespaceVersion)): |
| 84 | + raise ValueError("Major version of the version attribute does not match the targetNamespace version", versionAttrVersion, targetNamespaceVersion) |
| 85 | + |
| 86 | + if (not versionAttrVersion in enumVersions): |
| 87 | + raise ValueError("Schema version is not in the list of version enumerations", versionAttrVersion, enumVersions) |
| 88 | + |
| 89 | + minorVer = getMinorVersion(versionAttrVersion) |
| 90 | + |
| 91 | + for enumVer in enumVersions: |
| 92 | + if (not enumVer.startswith(targetNamespaceVersion)): |
| 93 | + raise ValueError("Major version of the enumeration version does not match the targetNamespace version", enumVer, targetNamespaceVersion) |
| 94 | + minorVerOfEnum = getMinorVersion(enumVer) |
| 95 | + |
| 96 | + if (minorVerOfEnum > minorVer): |
| 97 | + raise ValueError("Minor version of enumeration version is higher than the minor version of the schema version. Should be less than or equal to the schema version", enumVer, versionAttrVersion) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +validateVersions() |
| 102 | + |
| 103 | +print "" |
| 104 | +print "Done!" |
| 105 | +exit(0) |
0 commit comments