Skip to content

Commit 771de97

Browse files
authored
Merge pull request #24 from rruiter87/product-version
add product version check pre-commit
2 parents ea33201 + 7d55f6c commit 771de97

5 files changed

Lines changed: 77 additions & 4 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@
4040
language: python
4141
files: .*\.(TcPOU|TcGVL|TcDUT)$
4242
additional_dependencies: ["pytmc"]
43+
- id: no-product-version
44+
name: Check for product version
45+
description: Checks if the product version is saved in the TwinCAT source file.
46+
entry: no-product-version
47+
language: python
48+
files: .*\.(TcPOU|TcDUT|TcGVL)$

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,31 @@ Pre-commit hooks for PCDS projects (https://pre-commit.com/)
1010

1111
### To install pre-commit hooks to a local repository:
1212

13-
If .pre-config-config.yaml does not already exist in the repository, copy
13+
If `.pre-config-config.yaml` does not already exist in the repository, copy
1414
the appropriate file from this repository to the top-level of your local
15-
repository and commit the addition.
15+
repository, or add the folowing to an existing `.pre-config-config.yaml`
16+
file and commit the addition.
17+
18+
```yaml
19+
repos:
20+
- repo: https://github.com/pre-commit/pre-commit-hooks.git
21+
rev: v2.5.0
22+
hooks:
23+
- id: no-commit-to-branch
24+
- id: trailing-whitespace
25+
files: \.(TcPOU|TcDUT|TcGVL)$
26+
27+
- repo: https://github.com/pcdshub/pre-commit-hooks.git
28+
rev: v1.4.0
29+
hooks:
30+
- id: twincat-leading-tabs-remover
31+
- id: twincat-lineids-remover
32+
- id: twincat-xml-format
33+
- id: check-fixed-library-versions
34+
- id: no-product-version
35+
# Optional, if you use pytmc to generate EPICS IOCs:
36+
# - id: pytmc-pragma-linter
37+
```
1638

1739
Once the file is there, run the following from inside your repository:
1840
```bash

forTwinCatRepos/.pre-commit-config.yaml

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

1111
- repo: https://github.com/pcdshub/pre-commit-hooks.git
12-
rev: v1.2.0
12+
rev: v1.4.0
1313
hooks:
1414
- id: twincat-leading-tabs-remover
1515
- id: twincat-lineids-remover
1616
- id: twincat-xml-format
1717
- id: check-fixed-library-versions
18+
- id: no-product-version
1819
# Optional, if you use pytmc to generate EPICS IOCs:
1920
# - id: pytmc-pragma-linter
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
tc_plc_object = list(parse_tree.iter("TcPlcObject"))[0].attrib
20+
# Check if it contains a product version attribute
21+
if 'ProductVersion' in tc_plc_object:
22+
raise PreCommitException(
23+
f"Detected product version ({tc_plc_object['ProductVersion']}) in {filename}. "
24+
f"To disable this go to Project settings > Advanced and disable 'Write product version in files.'")
25+
26+
27+
def main(args=None):
28+
if args is None:
29+
parser = argparse.ArgumentParser()
30+
parser.add_argument('filenames', nargs='*')
31+
args = parser.parse_args()
32+
try:
33+
for filename in args.filenames:
34+
check_file(filename)
35+
return 0
36+
except Exception as exc:
37+
print(exc)
38+
return 1
39+
40+
41+
if __name__ == "__main__":
42+
exit(main())

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
hook_names = ['twincat-lineids-remover',
99
'leading-tabs-remover',
1010
'xml-format',
11-
'check-fixed-library-versions']
11+
'check-fixed-library-versions',
12+
'no-product-version',
13+
]
1214
console_scripts = []
1315
for name in hook_names:
1416
module = name.replace('-', '_')

0 commit comments

Comments
 (0)