Skip to content

Commit

Permalink
Merge pull request #17 from rruiter87/fix-library-versions
Browse files Browse the repository at this point in the history
add check for fixed plc library version precommit
  • Loading branch information
ZLLentz authored Nov 22, 2022
2 parents 4995887 + c3b1fff commit b7301c5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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$
3 changes: 2 additions & 1 deletion forTwinCatRepos/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ repos:
files: \.(TcPOU|TcDUT|TcGVL)$

- repo: https://github.com/pcdshub/pre-commit-hooks.git
rev: v1.0.0
rev: v1.2.0
hooks:
- id: twincat-leading-tabs-remover
- id: twincat-lineids-remover
- id: twincat-xml-format
- id: check-fixed-library-versions
44 changes: 44 additions & 0 deletions pre_commit_hooks/check_fixed_library_versions.py
Original file line number Diff line number Diff line change
@@ -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())
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('-', '_')
Expand Down

0 comments on commit b7301c5

Please sign in to comment.