Skip to content

Commit

Permalink
Add pre-commit for python formatting (#15)
Browse files Browse the repository at this point in the history
* Add pre-commit config
* Extended CONTRIBUTIN.md
* Exclude install, toolchain, and third_party directories from isort
  • Loading branch information
lukamac authored Nov 24, 2024
1 parent 7f33810 commit 7421833
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[settings]
line_length=120
multi_line_output=2
include_trailing_comma=false
include_trailing_comma=false
skip=TargetLibraries/CMSIS/third_party, TargetLibraries/PULPOpen/third_party, install, toolchain
32 changes: 32 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
exclude: .*third_party.*

# By default, all hooks will be installed as pre-push
default_stages: [pre-push]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-added-large-files
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.0
hooks:
- id: autoflake
args:
- "--remove-all-unused-imports"
- "--ignore-init-module-imports"
- "--in-place"
- repo: https://github.com/google/yapf
rev: v0.33.0
hooks:
- id: yapf
args:
- "--in-place"
- "--parallel"
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
32 changes: 27 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,40 @@ Deeploy mainly consists of code implemented in C, Makefile, and Python. To facil

To recursively format all Python files run:
```bash
$> autoflake -i -r --remove-all-unused-imports --ignore-init-module-imports --exclude "*/third_party/**" ./
$> yapf -ipr -e "third_party/" -e "install/" -e "toolchain/" ./
$> isort --sg "**/third_party/*" --sg "install/*" --sg "toolchain/*" ./
autoflake -i -r --remove-all-unused-imports --ignore-init-module-imports --exclude "*/third_party/**" .
yapf -ipr .
isort .
```

And for C files:
```bash
$> python scripts/run_clang_format.py -e "*/third_party/*" -e "*/install/*" -e "*/toolchain/*" -ir --clang-format-executable=${LLVM_INSTALL_DIR}/bin/clang-format ./
python scripts/run_clang_format.py -e "*/third_party/*" -e "*/install/*" -e "*/toolchain/*" -ir --clang-format-executable=${LLVM_INSTALL_DIR}/bin/clang-format ./
```

Note that third party applications should not be formatted. You can alternatively also run:
```
```bash
make format
```
to format all C and Python files.

### Pre-commit

Additionally, we provide the [pre-commit](https://pre-commit.com) configuration file which you can use to install github hooks that execute the formatting commands on your changes.

You will need to manually install pre-commit since it's not added as a dependency to the `pyproject.toml`:
```bash
pip install pre-commit
```

The configuration sets the default stage for all the hooks to `pre-push` so to install the git hooks run:
```bash
pre-commit install --hook-type pre-push
```
The hooks will run before each push, making sure the pushed code can pass linting checks and not fail the CI on linting.

If you change your mind and don't want the git hooks:
```bash
pre-commit uninstall
```

_Note:_ This configures only the python formatting git hooks. The c formatting is not supported at the moment.

0 comments on commit 7421833

Please sign in to comment.