This repository has been archived by the owner on Jun 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
143 lines (118 loc) · 4.79 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
SHELL = /bin/bash
PACKAGE_NAME = flask_caching_json
PYTHON_SOURCE_DIRS = $(PACKAGE_NAME)
PYTHON_IGNORE_DIRS := deps
PYTHON_TEST_DIRS = tests
VENV := env
PYTHONPATH=. pytest
PYLINT_JOBS = 4
UNITTEST_TIMEOUT = 2
INTEGRATIONTEST_TIMEOUT = 10
GUNICORNTEST_TIMEOUT = 60
PYTEST_TMP ?= /var/tmp/pytest-of-$(USER)
PYTEST_RAM_TMP ?= /tmp/pytest-of-$(USER)
PYTHON ?= PYTHONDONTWRITEBYTECODE=1 python3
PYTHON_VENV := $(VENV)/bin/python3
PYTEST_VENV ?= PYTHONDONTWRITEBYTECODE=1 $(PYTHON_VENV) -m pytest --asyncio-mode=strict --tb short --basetemp
PYTEST ?= $(PYTEST_VENV) "$(PYTEST_TMP)" $(PYTEST_ARGS)
PYTEST_RAM ?= $(PYTEST_VENV) "$(PYTEST_RAM_TMP)" $(PYTEST_ARGS)
generated = $(PYTEST_TMP) \
$(PYTEST_RAM_TMP)
CI_PY_TESTS = PYTHONDONTWRITEBYTECODE=1 $(PYTHON_VENV) -m ci.tests
MYPY ?= $(PYTHON_VENV) -m mypy --no-incremental
DOCFORMATTER = $(PYTHON_VENV) -m docformatter --wrap-summaries 0 --wrap-descriptions 0
PYDOCSTYLE = $(PYTHON_VENV) -m pydocstyle --count
PYDOC = $(PYTHON_VENV) -m pydoc
# Note: pylint will catch only some errors.
# For example: remove a method, and another file importing it will not be checked.
PYLINT = $(PYTHON_VENV) -m pylint --rcfile .pylintrc --jobs $(PYLINT_JOBS)
TOX = $(PYTHON_VENV) -m tox
BUILD = $(PYTHON_VENV) -m build
GIT_REMOTE = $(shell git remote -v | grep aiven/flask-caching-json | head -n 1 | cut -f 1)
GIT_CHECK_FROM ?= $(shell git merge-base $(GIT_REMOTE)/main HEAD)
GITHUB_HEAD_REF ?= $(shell git rev-parse --abbrev-ref HEAD)
GIT_MERGES = $(shell git log --merges $(GIT_CHECK_FROM)..$(GIT_REMOTE)/$(GITHUB_HEAD_REF) 2>&1)
SOURCE_AND_TEST_PATTERN = ^($(subst $(subst ,, ),(/|$$)|,$(PYTHON_SOURCE_DIRS) $(PYTHON_TEST_DIRS)))
SOURCE_PATTERN = ^($(subst $(subst ,, ),(/|$$)|,$(PYTHON_SOURCE_DIRS)))
CHANGED_PY_FILES = \
$(shell git diff --name-only --diff-filter=MRA $(GIT_CHECK_FROM) '*.py' | grep -E '$(SOURCE_AND_TEST_PATTERN)')
CHANGED_PY_SOURCE_FILES = \
$(shell git diff --name-only --diff-filter=MRA $(GIT_CHECK_FROM) '*.py' | grep -E '$(SOURCE_PATTERN)')
REQUIREMENTS_SYNC = "$(VENV)/requirements.sync"
# default target
.PHONY: dist
dist: venv-install-or-sync
$(BUILD) --sdist --wheel --outdir dist/ .
$(PYTEST_TMP):
mkdir -p "$(PYTEST_TMP)"
$(PYTEST_RAM_TMP):
mkdir -p "$(PYTEST_RAM_TMP)"
.PHONY: clean
clean: venv-clean pycache-clean
.PHONY: test
test: test-static-fast mypy-changed $(or $(TOX_JOB),tox)
.PHONY: test-static-fast
test-static-fast: copyright
.PHONY: test-lint-only
test-lint-only: mypy-full
.PHONY: tox
tox: $(generated) venv-install-or-sync
$(TOX) $(TOX_ARGS)
.PHONY: mypy-full
mypy-full: $(generated) venv-install-or-sync
$(MYPY) $(PYTHON_SOURCE_DIRS) $(PYTHON_TEST_DIRS)
.PHONY: mypy-changed
mypy-changed: $(generated) venv-install-or-sync
@ if test -n "$(CHANGED_PY_FILES)"; then \
$(MYPY) $(CHANGED_PY_FILES); \
fi
.PHONY: pycache-clean
pycache-clean:
$(info Removing __pycache__ folders.)
@ find . -type d -name '__pycache__' -exec rm -rf {} +
.PHONY: venv-clean
venv-clean:
$(info Removing virtual env folder: $(VENV))
@ rm -rf "$(VENV)"
.PHONY: venv-install
venv-install:
# Create a virtual environment to install newer Python tools without using root
$(MAKE) venv-clean
mkdir "$(VENV)"
chown "$(USER):$(shell id -gn)" "$(VENV)"
python3 -m venv "$(VENV)"
$(MAKE) venv-sync
.PHONY: venv-sync
venv-sync:
@ touch $(REQUIREMENTS_SYNC)
@ $(PYTHON_VENV) -m pip install --upgrade -r ci/requirements-pip.txt
@ $(PYTHON_VENV) -m piptools sync ci/requirements.txt ci/requirements-dev.txt
.PHONY: venv-install-or-sync
venv-install-or-sync:
@ if [ ! -x "$(PYTHON_VENV)" ]; then \
$(MAKE) venv-install; \
else \
for file in `git ls-files "ci/requirements*.txt"`; do \
if [[ `date -r "$$file" +%s` -gt `date -r $(REQUIREMENTS_SYNC) +%s` ]]; then \
requirements_outofsync=1; \
echo "Running 'make venv-sync' because $$file is newer than $(REQUIREMENTS_SYNC)"; \
fi; \
done; \
if [[ "$$requirements_outofsync" -eq 1 ]]; then \
$(MAKE) venv-sync; \
fi; \
fi
.PHONY: requirements
requirements: venv-install-or-sync
CUSTOM_COMPILE_COMMAND="make requirements" $(PYTHON_VENV) -m piptools compile ci/requirements-pip.in --generate-hashes --allow-unsafe
CUSTOM_COMPILE_COMMAND="make requirements" $(PYTHON_VENV) -m piptools compile ci/requirements.in --generate-hashes
CUSTOM_COMPILE_COMMAND="make requirements" $(PYTHON_VENV) -m piptools compile ci/requirements-dev.in --generate-hashes --allow-unsafe
$(MAKE) venv-sync
.PHONY: copyright
copyright:
$(eval MISSING_COPYRIGHT_PY := $(shell git ls-files "*.py" | xargs grep -EL "Copyright \(c\) 20.* Aiven|Aiven license OK"))
@if [ "$(MISSING_COPYRIGHT_PY)" != "" ]; then \
echo Adding missing default Copyright statement in files: $(MISSING_COPYRIGHT_PY); \
gawk -i inplace 'BEGINFILE{print"# Copyright (c) $(shell date +'%Y') Aiven, Helsinki, Finland. https://aiven.io/"} 1' $(MISSING_COPYRIGHT_PY); \
false; \
fi