-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
178 lines (131 loc) · 4.89 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
SHELL=/bin/bash
.DEFAULT_GOAL := _help
# NOTE: must put a <TAB> character and two pound "\t##" to show up in this list. Keep it brief! IGNORE_ME
.PHONY: _help
_help:
@printf "\nUsage: make <command>, valid commands:\n\n"
@grep "##" $(MAKEFILE_LIST) | grep -v IGNORE_ME | sed -e 's/##//' | column -t -s $$'\t'
# ---------------------------------------
# init & venv
# ---------------------------------------
.PHONY: init
init: ## Set up a Python virtual environment
git submodule update --init
rm -rf .venv
${PY_SYS_INTERPRETER} -m venv .venv
- if [ -z "${CI}" ]; then ${PY_SYS_INTERPRETER} -m venv --upgrade-deps .venv; fi
direnv allow
# include .env
SKIP_VENV ?=
PYTHON ?= $(shell which python)
PWD ?= $(shell pwd)
.PHONY: _venv
_venv:
# Test to enforce venv usage across important make targets
[ "${SKIP_VENV}" ] || [ "${PYTHON}" = "${PWD}/.venv/bin/python" ]
# ---------------------------------------
# Install requirements
# ---------------------------------------
PY_SYS_INTERPRETER ?=
ifeq ($(PY_SYS_INTERPRETER),)
ifeq ($(OS),Windows_NT)
PY_SYS_INTERPRETER += python3
else
PY_SYS_INTERPRETER += /usr/bin/python3
endif
endif
PY_VIRTUAL_INTERPRETER ?= python
PIP ?= $(PY_VIRTUAL_INTERPRETER) -m pip
REQ_OPT := requirements-optional.txt
REQ_LINT := requirements-lint.txt
REQ_TEST := requirements-test.txt
REQ_TEST_OLD := requirements-test-old.txt
PIP_OPT_ARGS ?= $(shell if [ "$(SKIP_VENV)" ]; then echo "--user"; fi)
.PHONY: deps
deps: _venv ## Install requirements
${PIP} install wheel
${PIP} install ${PIP_OPT_ARGS} -r requirements.txt
- ${PIP} install ${PIP_OPT_ARGS} -r ${REQ_OPT}
- ${PIP} install ${PIP_OPT_ARGS} -r ${REQ_LINT}
${PIP} install ${PIP_OPT_ARGS} -r ${REQ_TEST} || ${PIP} install ${PIP_OPT_ARGS} -r ${REQ_TEST_OLD}
# ---------------------------------------
# Format, lint, test
# ---------------------------------------
.PHONY: format
format: _venv ## Format with isort & black
if [ "${CHANGED_FILES_PY_FLAG}" ]; then isort ${CHANGED_FILES_PY} ; fi
if [ "${CHANGED_FILES_PY_FLAG}" ]; then black ${CHANGED_FILES_PY} ; fi
LINT_LOCS := ntclient/ tests/ setup.py
CHANGED_FILES_RST ?= $(shell git diff origin/master --name-only --diff-filter=MACRU \*.rst)
CHANGED_FILES_PY ?= $(shell git diff origin/master --name-only --diff-filter=MACRU \*.py)
CHANGED_FILES_PY_FLAG ?= $(shell if [ "$(CHANGED_FILES_PY)" ]; then echo 1; fi)
.PHONY: lint
lint: _venv ## Lint code and documentation
# lint RST
if [ "${CHANGED_FILES_RST}" ]; then doc8 --quiet ${CHANGED_FILES_RST}; fi
# check formatting: Python
if [ "${CHANGED_FILES_PY_FLAG}" ]; then isort --diff --check ${CHANGED_FILES_PY} ; fi
if [ "${CHANGED_FILES_PY_FLAG}" ]; then black --check ${CHANGED_FILES_PY} ; fi
# lint Python
if [ "${CHANGED_FILES_PY_FLAG}" ]; then pycodestyle --statistics ${CHANGED_FILES_PY}; fi
if [ "${CHANGED_FILES_PY_FLAG}" ]; then bandit -q -c .banditrc -r ${CHANGED_FILES_PY}; fi
if [ "${CHANGED_FILES_PY_FLAG}" ]; then flake8 ${CHANGED_FILES_PY}; fi
if [ "${CHANGED_FILES_PY_FLAG}" ]; then mypy ${CHANGED_FILES_PY}; fi
if [ "${CHANGED_FILES_PY_FLAG}" ]; then pylint ${CHANGED_FILES_PY}; fi
.PHONY: pylint
pylint:
if [ "${CHANGED_FILES_PY_FLAG}" ]; then pylint ${CHANGED_FILES_PY}; fi
.PHONY: mypy
mypy:
if [ "${CHANGED_FILES_PY_FLAG}" ]; then mypy ${CHANGED_FILES_PY}; fi
.PHONY: test
test: _venv ## Run CLI unittests
coverage run
coverage report
- grep fail_under setup.cfg
# ---------------------------------------
# SQLite submodule: nt-sqlite
# ---------------------------------------
# TODO: why does this still work? Is this what `ntserv.ntdb.sql` should do?
.PHONY: ntsqlite/build
ntsqlite/build:
${PY_SYS_INTERPRETER} ntclient/ntsqlite/sql/__init__.py
# TODO: nt-sqlite/test
# ---------------------------------------
# Python build & install
# ---------------------------------------
.PHONY: _build
_build:
${PY_SYS_INTERPRETER} setup.py --quiet sdist
.PHONY: build
build: ## Create sdist binary *.tar.gz
build: _build clean
.PHONY: install
install: ## pip install .
${PY_SYS_INTERPRETER} -m pip install wheel
${PY_SYS_INTERPRETER} -m pip install . || ${PY_SYS_INTERPRETER} -m pip install --user .
${PY_SYS_INTERPRETER} -m pip show nutra
- ${PY_SYS_INTERPRETER} -c 'import shutil; print(shutil.which("nutra"));'
nutra -v
# ---------------------------------------
# Clean
# ---------------------------------------
RECURSIVE_CLEAN_LOCS ?= $(shell find ntclient/ tests/ \
-name __pycache__ \
-o -name .coverage \
-o -name .mypy_cache \
-o -name .pytest_cache)
.PHONY: clean
clean: ## Clean up __pycache__ and leftover bits
rm -f .coverage ntclient/ntsqlite/sql/nt.sqlite3
rm -rf build/
rm -rf nutra.egg-info/
rm -rf .pytest_cache/ .mypy_cache/
# Recursively find & remove
if [ "${RECURSIVE_CLEAN_LOCS}" ]; then rm -rf ${RECURSIVE_CLEAN_LOCS}; fi
# ---------------------------------------
# Extras
# ---------------------------------------
.PHONY: extras/cloc
extras/cloc: ## Count lines of source code
- cloc HEAD