From eed2a917b83b75e46210d49a66f988dc7e720d68 Mon Sep 17 00:00:00 2001 From: Roald Ruiter Date: Fri, 7 Oct 2022 12:02:21 +0200 Subject: [PATCH 1/2] add check for fixed plc library version precommit --- .pre-commit-hooks.yaml | 6 +++ forTwinCatRepos/.pre-commit-config.yaml | 3 +- .../check_fixed_library_versions.py | 44 +++++++++++++++++++ setup.py | 3 +- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 pre_commit_hooks/check_fixed_library_versions.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index d3afdfd..93fef15 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -27,3 +27,9 @@ entry: xml-format language: python files: .*\.(tmc|tpy|xml)$ +- id: check-fixed-library-versions + name: Check fixed library versions + description: Checks if there are PLC libraries whos versions are not fixed. + entry: check-fixed-library-versions + language: python + files: .*\.plcproj$ diff --git a/forTwinCatRepos/.pre-commit-config.yaml b/forTwinCatRepos/.pre-commit-config.yaml index 44a6644..73d0cc7 100644 --- a/forTwinCatRepos/.pre-commit-config.yaml +++ b/forTwinCatRepos/.pre-commit-config.yaml @@ -9,8 +9,9 @@ repos: files: \.(TcPOU|TcDUT|TcGVL)$ - repo: https://github.com/pcdshub/pre-commit-hooks.git - rev: v1.0.0 + rev: v1.1.0 hooks: - id: twincat-leading-tabs-remover - id: twincat-lineids-remover - id: twincat-xml-format + - id: check-fixed-library-versions diff --git a/pre_commit_hooks/check_fixed_library_versions.py b/pre_commit_hooks/check_fixed_library_versions.py new file mode 100644 index 0000000..e5db9e9 --- /dev/null +++ b/pre_commit_hooks/check_fixed_library_versions.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +import argparse + +from lxml import etree + + +class PreCommitException(Exception): + pass + + +def check_file(filename): + with open(filename, 'rb') as fd: + original_xml = fd.read() + + xml_parser = etree.XMLParser(remove_blank_text=True) + parse_tree = etree.XML(original_xml, parser=xml_parser).getroottree() + + added_libraries = set(el.attrib["Include"] for el in parse_tree.iter("{*}PlaceholderReference")) + fixed_version_libraries = set(el.attrib["Include"] for el in parse_tree.iter("{*}PlaceholderResolution")) + + non_fixed_library_versions = added_libraries - fixed_version_libraries + + if len(non_fixed_library_versions) == 1: + raise PreCommitException(f"Library version of {list(non_fixed_library_versions)[0]} is not fixed!") + elif len(non_fixed_library_versions) > 1: + raise PreCommitException(f"Library version of {', '.join(non_fixed_library_versions)} are not fixed!") + +def main(args=None): + if args is None: + parser = argparse.ArgumentParser() + parser.add_argument('filenames', nargs='*') + args = parser.parse_args() + try: + for filename in args.filenames: + check_file(filename) + return 0 + except Exception as exc: + print(exc) + return 1 + + +if __name__ == "__main__": + exit(main()) diff --git a/setup.py b/setup.py index ba52783..f71765b 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,8 @@ hook_names = ['twincat-lineids-remover', 'leading-tabs-remover', - 'xml-format'] + 'xml-format', + 'check-fixed-library-versions'] console_scripts = [] for name in hook_names: module = name.replace('-', '_') From c3b1fffccdb6e22673a1bbc6b19a4b098ef414af Mon Sep 17 00:00:00 2001 From: Roald Ruiter <98890535+rruiter87@users.noreply.github.com> Date: Tue, 22 Nov 2022 08:10:13 +0100 Subject: [PATCH 2/2] bump version number Co-authored-by: Zachary Lentz --- forTwinCatRepos/.pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forTwinCatRepos/.pre-commit-config.yaml b/forTwinCatRepos/.pre-commit-config.yaml index 73d0cc7..78a539d 100644 --- a/forTwinCatRepos/.pre-commit-config.yaml +++ b/forTwinCatRepos/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: files: \.(TcPOU|TcDUT|TcGVL)$ - repo: https://github.com/pcdshub/pre-commit-hooks.git - rev: v1.1.0 + rev: v1.2.0 hooks: - id: twincat-leading-tabs-remover - id: twincat-lineids-remover