-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
213 lines (177 loc) · 5.18 KB
/
Copy pathMakefile
File metadata and controls
213 lines (177 loc) · 5.18 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# --
# ## Make Version Detection & Enforcement
# Detect if we're running under GNU Make
MAKE_VERSION := $(shell $(MAKE) --version 2>/dev/null | head -1)
IS_GMAKE := $(if $(findstring GNU Make,$(MAKE_VERSION)),1,0)
# If not GNU Make, check for gmake and provide guidance
ifneq ($(IS_GMAKE),1)
GMAKE_AVAILABLE := $(shell which gmake 2>/dev/null)
ifneq ($(GMAKE_AVAILABLE),)
$(error This Makefile requires GNU Make. Please run: gmake $(MAKECMDGOALS))
else
$(error This Makefile requires GNU Make. Please install it: brew install make (macOS) or apt-get install make (Linux), then run: gmake $(MAKECMDGOALS))
endif
endif
# --
# ## Make & Shell Configuration
SHELL:= bash
.SHELLFLAGS:= -eu -o pipefail -c
MAKEFLAGS+= --warn-undefined-variables
MAKEFLAGS+= --no-builtin-rules
# --
# ## Project Configuration
PROJECT:=sink
PYPI_PROJECT=sink
VERSION:=$(shell grep VERSION setup.py | head -n1 | cut -d '"' -f2)
# --
# ## Python Configuration
PYTHON=python3
PYTHON_MODULES=$(patsubst src/py/%,%,$(wildcard src/py/*))
PYTHON_MODULES_PIP=ruff bandit mypy flake8
PATH_PYTHON_LIB=run/lib/python
PYTHONPATH:=$(abspath $(PATH_PYTHON_LIB)$(if $(PYTHONPATH),:$(PYTHONPATH)))
export PYTHONPATH
# --
# ## Sources Configuration
SOURCES_BIN:=$(wildcard bin/*)
SOURCES_PY_PATH=src/py
SOURCES_PY:=$(wildcard $(SOURCES_PY_PATH)/*.py $(SOURCES_PY_PATH)/*/*.py $(SOURCES_PY_PATH)/*/*/*.py $(SOURCES_PY_PATH)/*/*/*/*.py)
MODULES_PY:=$(filter-out %/__main__,$(filter-out %/__init__,$(SOURCES_PY:$(SOURCES_PY_PATH)/%.py=%)))
PATH_LOCAL_PY=$(firstword $(shell $(PYTHON) -c "import sys,pathlib;sys.stdout.write(' '.join([_ for _ in sys.path if _.startswith(str(pathlib.Path.home()))] ))"))
PATH_LOCAL_BIN=$(HOME)/.local/bin
PREP_ALL=$(PYTHON_MODULES_PIP:%=build/py-install-%.task)
# --
# ## Commands
BANDIT=$(PYTHON) -m bandit
FLAKE8=$(PYTHON) -m flake8
MYPY=$(PYTHON) -m mypy
TWINE=$(PYTHON) -m twine
MYPYC=mypyc
cmd-check=if ! $$(which $1 &> /dev/null ); then echo "ERR Could not find command $1"; exit 1; fi; $1
.PHONY: prep
prep: $(PREP_ALL)
@
.PHONY: run
run:
@
.PHONY: ci
ci: check test
@
.PHONY: audit
audit: check-bandit
@echo "=== $@"
# NOTE: The compilation seems to create many small modules instead of a big single one
.PHONY: compile
compile:
@echo "=== $@"
echo "Compiling $(MODULES_PY): $(SOURCES_PY)"
# NOTE: Output is going to be like '$(PROJECT)/__init__.cpython-310-x86_64-linux-gnu.so'
mkdir -p "build"
$(foreach M,$(MODULES_PY),mkdir -p build/$M;)
env -C build MYPYPATH=$(realpath .)/src/py mypyc -p $(PROJECT)
.PHONY: check
check: check-bandit check-flakes check-strict
echo "=== $@"
.PHONY: check-compiled
check-compiled:
@
echo "=== $@"
COMPILED=$$(PYTHONPATH=build $(PYTHON) -c "import extra;print(extra)")
echo "Extra compiled at: $$COMPILED"
.PHONY: check-bandit
check-bandit: $(PREP_ALL)
@echo "=== $@"
$(BANDIT) -r -s B101 $(wildcard src/py/*)
.PHONY: check-flakes
check-flakes: $(PREP_ALL)
@echo "=== $@"
$(FLAKE8) --ignore=E1,E203,E231,E302,E401,E501,E704,E741,E266,F821,W $(SOURCES_PY)
.PHONY: check-mypyc
check-mypyc: $(PREP_ALL)
@$(call cmd-check,mypyc) $(SOURCES_PY)
.PHONY: check-strict
check-strict: $(PREP_ALL)
@count_ok=0
count_err=0
files_err=""
for item in $(SOURCES_PY); do
if $(MYPY) --strict $$item; then
count_ok=$$(($$count_ok+1))
else
count_err=$$(($$count_err+1))
files_err+=" $$item"
fi
done
summary="OK $$count_ok ERR $$count_err TOTAL $$(($$count_err + $$count_ok))"
if [ "$$count_err" != "0" ]; then
if [ -n "$$files_err" ]; then
for item in $$files_err; do
echo "ERR $$item"
done
fi
echo "EOS FAIL $$summary"
exit 1
else
echo "EOS OK $$summary"
fi
.PHONY: lint
lint: check-flakes
@
.PHONY: fmt
fmt:
@$(PYTHON) -m ruff format #$(SOURCES_PY)
.PHONY: release-prep
release-prep: $(PREP_ALL)
@
# git commit -a -m "[Release] $(PROJECT): $(VERSION)"; true
# git tag $(VERSION); true
# git push --all; true
.PHONY: release
release: $(PREP_ALL)
@
$(PYTHON) setup.py clean sdist bdist_wheel
$(TWINE) upload dist/$(subst -,_,$(PYPI_PROJECT))-$(VERSION)*
.PHONY: install
install:
@for file in $(SOURCES_BIN); do
echo "Installing $(PATH_LOCAL_BIN)/$$(basename $$file)"
ln -sfr $$file "$(PATH_LOCAL_BIN)/$$(basename $$file)"
mkdir -p "$(PATH_LOCAL_BIN)"
done
if [ ! -e "$(PATH_LOCAL_PY)" ]; then
mkdir -p "$(PATH_LOCAL_PY)"
fi
if [ -d "$(PATH_LOCAL_PY)" ]; then
for module in $(PYTHON_MODULES); do
echo "Installing $(PATH_LOCAL_PY)/$$module"
ln -sfr src/py/$$module "$(PATH_LOCAL_PY)"/$$module
done
else
echo "No local Python module path found: $(PATH_LOCAL_PY)"
fi
.PHONY: try-install
try-uninstall:
@for file in $(SOURCES_BIN); do
unlink $(PATH_LOCAL_BIN)/$$(basename $$file)
done
if [ -s "$(PATH_LOCAL_PY)" ]; then
for module in $(PYTHON_MODULES); do
unlink "$(PATH_LOCAL_PY)"/$$module
done
fi
build/py-install-%.task:
@
# Remove task file if it's older than 7 days to force weekly updates
if [ -f "$@" ] && [ $$(find "$@" -mtime +7 | wc -l) -gt 0 ]; then
echo "Refreshing $* (task file older than 7 days)";
rm -f "$@";
fi
mkdir -p "$(PATH_PYTHON_LIB)"
if $(PYTHON) -mpip install --target="$(PATH_PYTHON_LIB)" --upgrade '$*'; then
mkdir -p "$(dir $@)"
touch "$@"
fi
print-%:
$(info $*=$($*))
.ONESHELL:
# EOF