Skip to content

Commit 7f571e9

Browse files
committed
Add style checker and auto-formater to tox
Change-Id: Iea5ee9585f7e662580e8466250d2b7f33decef97
1 parent 8eeb1c1 commit 7f571e9

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

Diff for: .git-blame-ignore-revs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Black autoformater run
2+
1a7cf38f6fbac267d483597c95b3317cb1e1db10

Diff for: tox.ini

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
[tox]
2-
envlist=py{37,38,39}-{flake8,mypy}
2+
envlist=py{39,310}-{flake8,mypy}
33
skip_missing_interpreters = True
44
skipsdist = true
55

66
[testenv]
7+
allowlist_externals =
8+
{toxinidir}/utils/check-style.sh
9+
{toxinidir}/utils/format-code.sh
710
description =
11+
format: Autoformat the changed files to match the style
12+
style: Style consistency checker
813
flake8: Style consistency checker
914
mypy: Static analyzer for type annotations
1015
basepython =
11-
py37: python3.7
12-
py38: python3.8
1316
py39: python3.9
17+
py310: python3.10
1418
deps =
19+
format: black
20+
format: isort
21+
style: black
22+
style: isort
1523
flake8: flake8
1624
mypy: mypy
1725
mypy: types-requests
1826
commands =
27+
format: {toxinidir}/utils/format-code.sh
28+
style: {toxinidir}/utils/check-style.sh
1929
flake8: flake8
2030
mypy: mypy dns tools/ganeti-netbox-sync.py
2131

2232
[flake8]
2333
max-line-length = 120
2434
statistics = True
2535
ignore = W503
36+
37+
[testenv:py{39,310}-{format,style}]

Diff for: utils/check-style.sh

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash -e
2+
# Copied from the Spicerack repo
3+
4+
fail() {
5+
echo "The code is not formatted according to the current style. You can autoformat your code running:"
6+
echo " tox -e py3-format"
7+
exit 1
8+
}
9+
10+
UNSTAGED_FILES=(
11+
$(git diff HEAD --name-only --diff-filter=d | grep '\.py$' || true)
12+
)
13+
STAGED_FILES=(
14+
$(git diff HEAD --cached --name-only --diff-filter=d | grep '\.py$' || true)
15+
)
16+
17+
if [[ "$(git diff HEAD)" == "" ]] && [[ "$(git diff HEAD --cached)" == "" ]]; then
18+
echo "No local changes, testing the last commit."
19+
COMMITTED_FILES=(
20+
$(git diff HEAD^ --name-only --diff-filter=d | grep '\.py$' || true)
21+
)
22+
else
23+
COMMITTED_FILES=()
24+
fi
25+
26+
if [[ "${#UNSTAGED_FILES[@]}" -eq "0" && "${#STAGED_FILES[@]}" -eq "0" && "${#COMMITTED_FILES[@]}" -eq "0" ]]; then
27+
echo "No Python file modified, skipping black and isort checks."
28+
exit 0
29+
fi
30+
31+
black \
32+
--check \
33+
--diff \
34+
"${UNSTAGED_FILES[@]}" "${STAGED_FILES[@]}" "${COMMITTED_FILES[@]}" \
35+
|| fail
36+
37+
isort \
38+
--check-only \
39+
--diff \
40+
"${UNSTAGED_FILES[@]}" "${STAGED_FILES[@]}" "${COMMITTED_FILES[@]}" \
41+
|| fail

Diff for: utils/format-code.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash -e
2+
# Copied from the Spicerack repo
3+
4+
if [[ -n "$(git diff --name-only --diff-filter=d HEAD)" ]]; then
5+
echo "Using staged and unstaged files"
6+
REVISION="HEAD"
7+
else
8+
echo "Using files changed in the latest commit"
9+
REVISION="HEAD^"
10+
fi
11+
12+
FILES=( $(git diff --name-only --diff-filter=d "${REVISION}" | grep '\.py$' || true) )
13+
14+
if [[ "${#FILES[@]}" -eq "0" ]]; then
15+
echo "No Python files to format"
16+
exit 0
17+
fi
18+
19+
black "${FILES[@]}"
20+
isort --apply "${FILES[@]}"

0 commit comments

Comments
 (0)