-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalidateSchemaVersions.py
executable file
·148 lines (111 loc) · 5.88 KB
/
validateSchemaVersions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
#**********************************************************************
# Copyright 2016 Crown Copyright
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#**********************************************************************
#**********************************************************************
# Script to ensure all the version numbers in the event-logging
# XMLSchema are valid
#**********************************************************************
import os
import re
import sys
import xml.etree.ElementTree as ET
SCHEMA_FILENAME = "event-logging.xsd"
root_path = os.path.dirname(os.path.realpath(__file__))
def getMinorVersion(versionStr):
minorVer = re.match("[0-9]*\\.([0-9]*)[.-].*", versionStr).group(1)
return minorVer
def validateVersions(newVersion, schemaFile):
if (newVersion):
newVersionNum = re.sub(r'^v', "", newVersion)
print("Gradle build version: %s" % newVersionNum)
if (not schemaFile):
schemaFile = SCHEMA_FILENAME
if (not os.path.isfile(schemaFile)):
print("ERROR - Schema file %s doesn't exist" % schemaFile)
exit(1)
print("")
print("Validating file %s" % schemaFile)
# pattern = re.compile("xmlns:evt\"event-logging:.*\"")
xsdFile = open(schemaFile, 'r')
filetext = xsdFile.read()
xsdFile.close()
matches = re.findall("xmlns:evt=\"event-logging:([^\"]*)\"", filetext)
if (len(matches) != 1):
raise ValueError("Unexpected matches for evt namespace", matches)
namespaceVersion = matches[0]
print("namespace version: [%s]" % namespaceVersion)
xml_root = ET.parse(schemaFile).getroot()
targetNamespaceAttr = xml_root.get("targetNamespace")
targetNamespaceVersion = re.match(".*:(.*)$", targetNamespaceAttr).group(1)
print("targetNamespace: [%s]" % targetNamespaceVersion)
versionAttrVersion = xml_root.get("version")
print("version: [%s]" % versionAttrVersion)
idAttr = xml_root.get("id")
idAttrVersion = re.match("event-logging-v?(.*)$", idAttr).group(1)
print("id: [%s]" % idAttrVersion)
ns = {'xs': 'http://www.w3.org/2001/XMLSchema'}
enumVersions = []
print("Version enumerations:")
for enumElm in xml_root.findall("./xs:simpleType[@name='VersionSimpleType']/xs:restriction/xs:enumeration", ns):
print(" %s" % enumElm.get("value"))
enumVersions.append(enumElm.get("value"))
print("")
versionRegex = "[0-9]+\\.[0-9]+(\\.[0-9]+|-(alpha|beta)\\.[0-9]+)"
if (newVersion and not re.match(".*SNAPSHOT", newVersionNum)):
if (versionAttrVersion != newVersionNum):
raise ValueError("version attribute and planned version do not match", versionAttrVersion, newVersionNum)
if (not re.match(versionRegex, versionAttrVersion)):
raise ValueError("version attribute does not match the valid regex", versionAttrVersion, versionRegex)
# In an ideal world we would increment the namespace version when we make
# a breaking change to the schema but that has all sorts of implications
# on existing pipelines, xslts and content packs that would all need changing.
if (not versionAttrVersion.startswith(targetNamespaceVersion)):
print("Major version of the version attribute %s does not match the targetNamespace version %s" % (versionAttrVersion, targetNamespaceVersion))
# raise ValueError("Major version of the version attribute does not match the targetNamespace version", versionAttrVersion, targetNamespaceVersion)
minorVer = getMinorVersion(versionAttrVersion)
for enumVer in enumVersions:
if (not enumVer == "SNAPSHOT"):
if (not enumVer.startswith(targetNamespaceVersion)):
# See comment above about namespace versioning
print("Major version of the enumeration version %s does not match the targetNamespace version %s" % (enumVer, targetNamespaceVersion))
# raise ValueError("Major version of the enumeration version does not match the targetNamespace version", enumVer, targetNamespaceVersion)
minorVerOfEnum = getMinorVersion(enumVer)
if (minorVerOfEnum > minorVer):
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)
# print enumVersions
if (namespaceVersion != targetNamespaceVersion):
raise ValueError("namespace version and targetNamespace version do not match", namespaceVersion, targetNamespaceVersion)
if (versionAttrVersion != idAttrVersion):
raise ValueError("version attribute and id attribute do not match", versionAttrVersion, idAttrVersion)
if (not versionAttrVersion in enumVersions):
raise ValueError("Schema version is not in the list of version enumerations", versionAttrVersion, enumVersions)
# Script starts here
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print(sys.argv)
if len(sys.argv) == 2:
newVersion = sys.argv[1]
schemaFile = None
elif len(sys.argv) == 3:
newVersion = sys.argv[1]
schemaFile = sys.argv[2]
else:
newVersion = None
schemaFile = None
print("version [%s], schema file [%s]" % (newVersion, schemaFile))
validateVersions(newVersion, schemaFile)
print("")
print("Done!")
exit(0)