-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from rruiter87/fix-library-versions
add check for fixed plc library version precommit
- Loading branch information
Showing
4 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters