Skip to content

Commit

Permalink
Merge pull request #24 from rruiter87/product-version
Browse files Browse the repository at this point in the history
add product version check pre-commit
  • Loading branch information
ZLLentz authored Mar 27, 2024
2 parents ea33201 + 7d55f6c commit 771de97
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@
language: python
files: .*\.(TcPOU|TcGVL|TcDUT)$
additional_dependencies: ["pytmc"]
- id: no-product-version
name: Check for product version
description: Checks if the product version is saved in the TwinCAT source file.
entry: no-product-version
language: python
files: .*\.(TcPOU|TcDUT|TcGVL)$
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,31 @@ Pre-commit hooks for PCDS projects (https://pre-commit.com/)

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

If .pre-config-config.yaml does not already exist in the repository, copy
If `.pre-config-config.yaml` does not already exist in the repository, copy
the appropriate file from this repository to the top-level of your local
repository and commit the addition.
repository, or add the folowing to an existing `.pre-config-config.yaml`
file and commit the addition.

```yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v2.5.0
hooks:
- id: no-commit-to-branch
- id: trailing-whitespace
files: \.(TcPOU|TcDUT|TcGVL)$

- repo: https://github.com/pcdshub/pre-commit-hooks.git
rev: v1.4.0
hooks:
- id: twincat-leading-tabs-remover
- id: twincat-lineids-remover
- id: twincat-xml-format
- id: check-fixed-library-versions
- id: no-product-version
# Optional, if you use pytmc to generate EPICS IOCs:
# - id: pytmc-pragma-linter
```

Once the file is there, run the following from inside your repository:
```bash
Expand Down
3 changes: 2 additions & 1 deletion forTwinCatRepos/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ repos:
files: \.(TcPOU|TcDUT|TcGVL)$

- repo: https://github.com/pcdshub/pre-commit-hooks.git
rev: v1.2.0
rev: v1.4.0
hooks:
- id: twincat-leading-tabs-remover
- id: twincat-lineids-remover
- id: twincat-xml-format
- id: check-fixed-library-versions
- id: no-product-version
# Optional, if you use pytmc to generate EPICS IOCs:
# - id: pytmc-pragma-linter
42 changes: 42 additions & 0 deletions pre_commit_hooks/no_product_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/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()

tc_plc_object = list(parse_tree.iter("TcPlcObject"))[0].attrib
# Check if it contains a product version attribute
if 'ProductVersion' in tc_plc_object:
raise PreCommitException(
f"Detected product version ({tc_plc_object['ProductVersion']}) in {filename}. "
f"To disable this go to Project settings > Advanced and disable 'Write product version in files.'")


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())
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
hook_names = ['twincat-lineids-remover',
'leading-tabs-remover',
'xml-format',
'check-fixed-library-versions']
'check-fixed-library-versions',
'no-product-version',
]
console_scripts = []
for name in hook_names:
module = name.replace('-', '_')
Expand Down

0 comments on commit 771de97

Please sign in to comment.