diff --git a/pipeline.py b/pipeline.py index bbff9c0..ef88205 100644 --- a/pipeline.py +++ b/pipeline.py @@ -1,2 +1,36 @@ +import glob +import xml.etree.ElementTree as ET +import os + +VERSION_TAGS = {"**/*.Tc*": "ProductVersion", "**/*.tsproj": "TcVersion"} +CORRECT_VERSION = "3.1.4024.0" + + +def check_versions(): + """ + Checks the Twincat version used to create a file. + It assumes that the version is stored as an attribute on the top element of the file, the attribute names are + listed in VERSION_TAGS. + :return: A dictionary of incorrect files and their version numbers + """ + incorrect_files = dict() + for file_path, version_attrib in VERSION_TAGS.items(): + found_files = glob.glob(file_path, recursive=True) + if not found_files: + print("ERROR: No files of type {} found".format(file_path)) + for file in found_files: + tree = ET.parse(file) + try: + found_version = tree.getroot().attrib[version_attrib] + if found_version != CORRECT_VERSION: + incorrect_files[file] = found_version + except KeyError: + print("WARNING: No version found for {}".format(file)) + return incorrect_files + + if __name__ == "__main__": - exit(1) # Fail the pipeline + incorrect_files = check_versions() + for file, version in incorrect_files.items(): + print("ERROR: {} has incorrect version {}, expected version {}".format(file, version, CORRECT_VERSION)) + exit(len(incorrect_files)) # Exit with non-zero if any bad versions were found