mpyhooks
This tool provides various hooks to check file content in a repository, with many originating from the OpenFOAM Foundation’s code style guide. The hooks can be easily integrated with pre-commit for automated checking.
Usage
mpyhooks <hook> [options] [files ...]
Available Hooks
encoding - Preview File Encoding
Check encoding for preview files in Rodare (Rossendorf Data Repository).
Usage:
mpyhooks encoding file1 file2 ...
line-length - Line Length Check
Check for line length less than 80 characters. Enforces maximum line length following OpenFOAM Foundation software style guide.
Usage:
mpyhooks line-length file1.C file2.H ...
ifndef - Header Guard Check
Check for #ifndef directive and #define statements in header files. Ensures
proper include guards in header files. The hook tries to detect regular header
files and will skip header files that contain pure source code only. The
detection is based on a test for #pragma once, #ifndef or #define.
Usage:
mpyhooks ifndef MyClass.H ...
Expected format:
#ifndef MyClass_H
#define MyClass_H
// ... content ...
#endif
tabs - Tab Detection
Check for tabs in files (should use 4 spaces instead).
Usage:
mpyhooks tabs file1.C file2.py ...
non-standard-code - Code Style Check
Checks for non-standard C++ patterns per OpenFOAM Foundation software guidelines.
Usage:
mpyhooks non-standard-code file1.C file2.H ...
copyright - Copyright Validation
Check and fix copyright header in source code files. The header is generated
based on JINJA templates. A template for HZDR and OpenFOAM Foundation software
for C++, shell and dictionaries is shipped with the package. Metadata for the
header has to be provided via codemeta.json and/or by reuse.toml. The hook
tries to load both automatically. The copyright holder for the source code file
is generated from the existing header, the copyrightHolder key in
codemeta.json and from reuse annotations. In case duplicates exists, they are
merged.
Usage:
mpyhooks copyright --template <template> <path/to/file>
keywords - Keyword Validation
Check keyword validity in case metadata.
Usage:
mpyhooks keywords --ref reference.yml case.yml
Pre-commit Integration
Detailed installation and usage instructions for pre-commit can be found
here. To use the hooks provided by this package you
have to have access to the repository for this package in Helmholtz Codebase.
For using the hooks in your own repository, create a .pre-commit-hooks.yaml
in the root of the repository:
repos:
- repo: https://codebase.helmholtz.cloud/fwdc/public/python.git
rev: 4.0.0
hooks:
- id: line-length
- id: preview-encoding
- id: ifndef
- id: tabs
- id: non-standard-code
- id: cpp-copyright
- id: shell-copyright
- id: config-copyright
- id: keywords
Install pre-commit:
pip install pre-commit
pre-commit install
Now hooks run automatically on git commit for all stashed files. For running
a single hook only use pre commit run <hook>, or for running all hooks on all
files pre commit run --all-files, or for running the hooks only on selected
files pre-commit run --files .... For more information on pre-commit the
reader is referred to its documentation.