Skip to content

Commit ef6d6e9

Browse files
committed
Add new black-check and black-format Make targets which verify code
comforms to black formatting rules and run it on CI.
1 parent 03869c9 commit ef6d6e9

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

Makefile

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,63 @@ schemasgen: requirements .schemasgen
326326
. $(VIRTUALENV_DIR)/bin/activate; pylint -j $(PYLINT_CONCURRENCY) -E --rcfile=./lint-configs/python/.pylintrc --load-plugins=pylint_plugins.api_models tools/*.py || exit 1;
327327
. $(VIRTUALENV_DIR)/bin/activate; pylint -j $(PYLINT_CONCURRENCY) -E --rcfile=./lint-configs/python/.pylintrc pylint_plugins/*.py || exit 1;
328328

329+
# Black task which checks if the code comforts to black code style
330+
.PHONY: black-check
331+
black: requirements .black-check
332+
333+
.PHONY: .black-check
334+
.black:
335+
@echo
336+
@echo "================== black-check ===================="
337+
@echo
338+
# st2 components
339+
@for component in $(COMPONENTS); do\
340+
echo "==========================================================="; \
341+
echo "Running black on" $$component; \
342+
echo "==========================================================="; \
343+
. $(VIRTUALENV_DIR)/bin/activate ; black --check --config pyproject.toml $$component/ || exit 1; \
344+
done
345+
# runner modules and packages
346+
@for component in $(COMPONENTS_RUNNERS); do\
347+
echo "==========================================================="; \
348+
echo "Running black on" $$component; \
349+
echo "==========================================================="; \
350+
. $(VIRTUALENV_DIR)/bin/activate ; black --check --config pyproject.toml $$component/ || exit 1; \
351+
done
352+
# Python pack management actions
353+
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml contrib/* || exit 1;
354+
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml scripts/*.py || exit 1;
355+
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml tools/*.py || exit 1;
356+
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml pylint_plugins/*.py || exit 1;
357+
358+
# Black task which reformats the code using black
359+
.PHONY: black-format
360+
black: requirements .black-format
361+
362+
.PHONY: .black-format
363+
.black-format:
364+
@echo
365+
@echo "================== black ===================="
366+
@echo
367+
# st2 components
368+
@for component in $(COMPONENTS); do\
369+
echo "==========================================================="; \
370+
echo "Running black on" $$component; \
371+
echo "==========================================================="; \
372+
. $(VIRTUALENV_DIR)/bin/activate ; black --config pyproject.toml $$component/ || exit 1; \
373+
done
374+
# runner modules and packages
375+
@for component in $(COMPONENTS_RUNNERS); do\
376+
echo "==========================================================="; \
377+
echo "Running black on" $$component; \
378+
echo "==========================================================="; \
379+
. $(VIRTUALENV_DIR)/bin/activate ; black --config pyproject.toml $$component/ || exit 1; \
380+
done
381+
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml contrib/ || exit 1;
382+
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml scripts/*.py || exit 1;
383+
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml tools/*.py || exit 1;
384+
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml pylint_plugins/*.py || exit 1;
385+
329386
.PHONY: lint-api-spec
330387
lint-api-spec: requirements .lint-api-spec
331388

@@ -979,7 +1036,7 @@ debs:
9791036
ci: ci-checks ci-unit ci-integration ci-packs-tests
9801037

9811038
.PHONY: ci-checks
982-
ci-checks: .generated-files-check .pylint .flake8 check-requirements check-sdist-requirements .st2client-dependencies-check .st2common-circular-dependencies-check circle-lint-api-spec .rst-check .st2client-install-check check-python-packages
1039+
ci-checks: .generated-files-check .black-check .pylint .flake8 check-requirements check-sdist-requirements .st2client-dependencies-check .st2common-circular-dependencies-check circle-lint-api-spec .rst-check .st2client-install-check check-python-packages
9831040

9841041
.PHONY: .rst-check
9851042
.rst-check:

pyproject.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[tool.black]
2+
max-line-length = 100
3+
target_version = ['py36']
4+
include = '\.pyi?$'
5+
exclude = '''
6+
(
7+
/(
8+
| \.git
9+
| \.virtualenv
10+
| __pycache__
11+
)/
12+
)
13+
'''

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ st2flake8==0.1.0
55
astroid==2.4.2
66
pylint==2.6.0
77
pylint-plugin-utils>=0.4
8+
black==20.8b1
89
bandit==1.5.1
910
ipython<6.0.0
1011
isort>=4.2.5

0 commit comments

Comments
 (0)