Skip to content

Commit b7301c5

Browse files
authored
Merge pull request #17 from rruiter87/fix-library-versions
add check for fixed plc library version precommit
2 parents 4995887 + c3b1fff commit b7301c5

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@
2727
entry: xml-format
2828
language: python
2929
files: .*\.(tmc|tpy|xml)$
30+
- id: check-fixed-library-versions
31+
name: Check fixed library versions
32+
description: Checks if there are PLC libraries whos versions are not fixed.
33+
entry: check-fixed-library-versions
34+
language: python
35+
files: .*\.plcproj$

forTwinCatRepos/.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ repos:
99
files: \.(TcPOU|TcDUT|TcGVL)$
1010

1111
- repo: https://github.com/pcdshub/pre-commit-hooks.git
12-
rev: v1.0.0
12+
rev: v1.2.0
1313
hooks:
1414
- id: twincat-leading-tabs-remover
1515
- id: twincat-lineids-remover
1616
- id: twincat-xml-format
17+
- id: check-fixed-library-versions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
5+
from lxml import etree
6+
7+
8+
class PreCommitException(Exception):
9+
pass
10+
11+
12+
def check_file(filename):
13+
with open(filename, 'rb') as fd:
14+
original_xml = fd.read()
15+
16+
xml_parser = etree.XMLParser(remove_blank_text=True)
17+
parse_tree = etree.XML(original_xml, parser=xml_parser).getroottree()
18+
19+
added_libraries = set(el.attrib["Include"] for el in parse_tree.iter("{*}PlaceholderReference"))
20+
fixed_version_libraries = set(el.attrib["Include"] for el in parse_tree.iter("{*}PlaceholderResolution"))
21+
22+
non_fixed_library_versions = added_libraries - fixed_version_libraries
23+
24+
if len(non_fixed_library_versions) == 1:
25+
raise PreCommitException(f"Library version of {list(non_fixed_library_versions)[0]} is not fixed!")
26+
elif len(non_fixed_library_versions) > 1:
27+
raise PreCommitException(f"Library version of {', '.join(non_fixed_library_versions)} are not fixed!")
28+
29+
def main(args=None):
30+
if args is None:
31+
parser = argparse.ArgumentParser()
32+
parser.add_argument('filenames', nargs='*')
33+
args = parser.parse_args()
34+
try:
35+
for filename in args.filenames:
36+
check_file(filename)
37+
return 0
38+
except Exception as exc:
39+
print(exc)
40+
return 1
41+
42+
43+
if __name__ == "__main__":
44+
exit(main())

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
hook_names = ['twincat-lineids-remover',
99
'leading-tabs-remover',
10-
'xml-format']
10+
'xml-format',
11+
'check-fixed-library-versions']
1112
console_scripts = []
1213
for name in hook_names:
1314
module = name.replace('-', '_')

0 commit comments

Comments
 (0)