forked from pcdshub/pre-commit-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_fixed_library_versions.py
More file actions
57 lines (44 loc) · 1.52 KB
/
check_fixed_library_versions.py
File metadata and controls
57 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
import argparse
from lxml import etree
from pre_commit_hooks.exceptions import PreCommitException
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 "
f"not fixed! File parsed: {filename}"
)
)
elif len(non_fixed_library_versions) > 1:
raise PreCommitException(
(
f"Library version of {', '.join(non_fixed_library_versions)} "
f"are not fixed! File parsed: {filename}"
)
)
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())