File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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$
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 ())
Original file line number Diff line number Diff line change 77
88hook_names = ['twincat-lineids-remover' ,
99 'leading-tabs-remover' ,
10- 'xml-format' ]
10+ 'xml-format' ,
11+ 'check-fixed-library-versions' ]
1112console_scripts = []
1213for name in hook_names :
1314 module = name .replace ('-' , '_' )
You can’t perform that action at this time.
0 commit comments