-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakefile
251 lines (209 loc) · 8.25 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# configuration
# ==================================================
PACKAGE_NAME := zanj
# for checking you are on the right branch when publishing
PUBLISH_BRANCH := main
# where to put the coverage reports
COVERAGE_REPORTS_DIR := docs/coverage
# where the tests are (assumes pytest)
TESTS_DIR := tests/
# temp directory to clean up
TESTS_TEMP_DIR := tests/junk_data
# dev and lint requirements.txt files
REQ_DEV := .github/dev-requirements.txt
REQ_LINT := .github/lint-requirements.txt
# no zanj, it gets special treatment because it depends on muutils
# without urls since pytorch extra index breaks things
# no torch because we install it manually in CI
EXPORT_ARGS := -E pandas --with dev --with lint --without-hashes --without-urls
# probably don't change these:
# --------------------------------------------------
# will print this token when publishing
PYPI_TOKEN_FILE := .github/local/.pypi-token
# the last version that was auto-uploaded. will use this to create a commit log for version tag
LAST_VERSION_FILE := .github/.lastversion
# where the pyproject.toml file is
PYPROJECT := pyproject.toml
# base python to use. Will add `poetry run` in front of this if `RUN_GLOBAL` is not set to 1
PYTHON_BASE := python
# where the commit log will be stored
COMMIT_LOG_FILE := .github/local/.commit_log
# reading information and command line options
# ==================================================
# RUN_GLOBAL=1 to use global `PYTHON_BASE` instead of `poetry run $(PYTHON_BASE)`
# --------------------------------------------------
# for formatting, we might want to run python without setting up all of poetry
RUN_GLOBAL ?= 0
ifeq ($(RUN_GLOBAL),0)
PYTHON = poetry run $(PYTHON_BASE)
else
PYTHON = $(PYTHON_BASE)
endif
# reading version
# --------------------------------------------------
# assuming your pyproject.toml has a line that looks like `version = "0.0.1"`, will get the version
VERSION := NULL
# read last auto-uploaded version from file
LAST_VERSION := NULL
# get the python version, now that we have picked the python command
PYTHON_VERSION := NULL
.PHONY: gen-version-info
gen-version-info:
$(eval VERSION := $(shell python -c "import re; print('v'+re.search(r'^version\s*=\s*\"(.+?)\"', open('$(PYPROJECT)').read(), re.MULTILINE).group(1))") )
$(eval LAST_VERSION := $(shell [ -f $(LAST_VERSION_FILE) ] && cat $(LAST_VERSION_FILE) || echo NULL) )
$(eval PYTHON_VERSION := $(shell $(PYTHON) -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')") )
# getting commit log
.PHONY: gen-commit-log
gen-commit-log: gen-version-info
if [ "$(LAST_VERSION)" = "NULL" ]; then \
echo "LAST_VERSION is NULL, cant get commit log!"; \
exit 1; \
fi
$(shell python -c "import subprocess; open('$(COMMIT_LOG_FILE)', 'w').write('\n'.join(reversed(subprocess.check_output(['git', 'log', '$(LAST_VERSION)'.strip() + '..HEAD', '--pretty=format:- %s (%h)']).decode('utf-8').strip().split('\n'))))")
# default target (help)
# ==================================================
.PHONY: default
default: help
.PHONY: version
version: gen-commit-log
@echo "Current version is $(VERSION), last auto-uploaded version is $(LAST_VERSION)"
@echo "Commit log since last version:"
@cat $(COMMIT_LOG_FILE)
@if [ "$(VERSION)" = "$(LAST_VERSION)" ]; then \
echo "Python package $(VERSION) is the same as last published version $(LAST_VERSION), exiting!"; \
exit 1; \
fi
# options we might want to pass to pytest
# --------------------------------------------------
PYTEST_OPTIONS ?= # using ?= means you can pass extra options from the command line
COV ?= 1
ifdef VERBOSE
PYTEST_OPTIONS += --verbose
endif
ifeq ($(COV),1)
PYTEST_OPTIONS += --cov=.
endif
# formatting
# ==================================================
.PHONY: setup-format
setup-format:
@echo "install only packages needed for formatting, direct via pip (useful for CI)"
$(PYTHON) -m pip install -r $(REQ_LINT)
.PHONY: format
format:
@echo "format the source code"
$(PYTHON) -m ruff format --config $(PYPROJECT) .
$(PYTHON) -m ruff check --fix --config $(PYPROJECT) .
$(PYTHON) -m pycln --config $(PYPROJECT) --all .
.PHONY: check-format
check-format:
@echo "run format check"
$(PYTHON) -m ruff check --config $(PYPROJECT) .
$(PYTHON) -m pycln --check --config $(PYPROJECT) .
# coverage
# ==================================================
.PHONY: cov
cov:
@echo "generate coverage reports"
$(PYTHON) -m coverage report -m > $(COVERAGE_REPORTS_DIR)/coverage.txt
$(PYTHON) -m coverage_badge -f -o $(COVERAGE_REPORTS_DIR)/coverage.svg
$(PYTHON) -m coverage html
# tests
# ==================================================
# at some point, need to add back --check-untyped-defs to mypy call
# but it complains when we specify arguments by keyword where positional is fine
# not sure how to fix this
# python -m pylint $(PACKAGE_NAME)/
# python -m pylint tests/
.PHONY: typing
typing: clean
@echo "running type checks"
$(PYTHON) -m mypy --config-file $(PYPROJECT) $(TYPECHECK_ARGS) $(PACKAGE_NAME)/
$(PYTHON) -m mypy --config-file $(PYPROJECT) $(TYPECHECK_ARGS) tests/
.PHONY: test
test: clean
@echo "running tests"
$(PYTHON) -m pytest $(PYTEST_OPTIONS) $(TESTS_DIR)
.PHONY: check
check: clean check-format test typing
@echo "run format and lint checks, tests, and typing checks"
# build and publish
# ==================================================
.PHONY: verify-git
verify-git:
@echo "checking git status"
if [ "$(shell git branch --show-current)" != $(PUBLISH_BRANCH) ]; then \
echo "Git is not on the $(PUBLISH_BRANCH) branch, exiting!"; \
exit 1; \
fi; \
if [ -n "$(shell git status --porcelain)" ]; then \
echo "Git is not clean, exiting!"; \
exit 1; \
fi; \
.PHONY: dep-dev
dep-dev:
@echo "exporting dev and extras deps to $(REQ_DEV), lint/format deps to $(REQ_LINT)"
poetry update
poetry export $(EXPORT_ARGS) --output $(REQ_DEV)
poetry export --only lint --without-hashes --without-urls --output $(REQ_LINT)
.PHONY: check-dep-dev
check-dep-dev:
@echo "checking poetry lock is good, exported requirements match poetry"
poetry check --lock
poetry export $(EXPORT_ARGS) | diff - $(REQ_DEV)
poetry export --only lint --without-hashes --without-urls | diff - $(REQ_LINT)
.PHONY: build
build:
@echo "build via poetry, assumes checks have been run"
poetry build
.PHONY: publish
publish: gen-commit-log check build verify-git version gen-version-info
@echo "run all checks, build, and then publish"
@echo "Enter the new version number if you want to upload to pypi and create a new tag"
@echo "Now would also be the time to edit $(COMMIT_LOG_FILE), as that will be used as the tag description"
@read -p "Confirm: " NEW_VERSION; \
if [ "$$NEW_VERSION" = $(VERSION) ]; then \
echo "Version confirmed. Proceeding with publish."; \
else \
echo "Version mismatch, exiting: you gave $$NEW_VERSION but expected $(VERSION)"; \
exit 1; \
fi;
@echo "pypi username: __token__"
@echo "pypi token from '$(PYPI_TOKEN_FILE)' :"
echo $$(cat $(PYPI_TOKEN_FILE))
echo "Uploading!"; \
echo $(VERSION) > $(LAST_VERSION_FILE); \
git add $(LAST_VERSION_FILE); \
git commit -m "Auto update to $(VERSION)"; \
git tag -a $(VERSION) -F $(COMMIT_LOG_FILE); \
git push origin $(VERSION); \
twine upload dist/* --verbose
# cleanup
# ==================================================
.PHONY: clean
clean:
@echo "cleaning up"
rm -rf .mypy_cache
rm -rf .ruff_cache
rm -rf .pytest_cache
rm -rf .coverage
rm -rf dist
rm -rf build
rm -rf $(PACKAGE_NAME).egg-info
rm -rf $(TESTS_TEMP_DIR)
$(PYTHON_BASE) -Bc "import pathlib; [p.unlink() for p in pathlib.Path('.').rglob('*.py[co]')]"
$(PYTHON_BASE) -Bc "import pathlib; [p.rmdir() for p in pathlib.Path('.').rglob('__pycache__')]"
# listing targets, from stackoverflow
# https://stackoverflow.com/questions/4219255/how-do-you-get-the-list-of-targets-in-a-makefile
.PHONY: help
help: gen-version-info
@echo -n "list make targets"
@echo ":"
@cat Makefile | sed -n '/^\.PHONY: / h; /\(^\t@*echo\|^\t:\)/ {H; x; /PHONY/ s/.PHONY: \(.*\)\n.*"\(.*\)"/ make \1\t\2/p; d; x}'| sort -k2,2 |expand -t 25
@echo "# makefile variables:"
@echo " PYTHON = $(PYTHON)"
@echo " PYTHON_VERSION = $(PYTHON_VERSION)"
@echo " PACKAGE_NAME = $(PACKAGE_NAME)"
@echo " VERSION = $(VERSION)"
@echo " LAST_VERSION = $(LAST_VERSION)"
@echo " PYTEST_OPTIONS = $(PYTEST_OPTIONS)"