-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathMakefile
More file actions
executable file
·245 lines (209 loc) · 6.64 KB
/
Makefile
File metadata and controls
executable file
·245 lines (209 loc) · 6.64 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
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
SHELL := /bin/bash
PY = python3
VENV = .venv
BIN=$(VENV)/bin
PYTHON_VERSION=$(shell cat .tool-versions|grep 'python' | cut -d " " -f 2)
.ONESHELL:
.DEFAULT_GOAL := dev
######################################
## INSTALL/CONFIGURE LOCAL ENV
######################################
check: check-asdf
## Check ASDF install
check-asdf:
@if ! command -v asdf &> /dev/null; then \
echo "Error: asdf is not installed. Please install asdf first."; \
echo " => https://asdf-vm.com/guide/getting-started.html"; \
exit 1; \
fi
## Init local environment file with exemple
init:
if [ ! -f ".env" ]; then \
cp env.example .env; \
fi
## Install dev environment
install: install-libdev install-asdf-full configure-poetry upgrade-pip
## Minimun install to speed run on Github
install-github:
@$(call title,"ASDF Install in .tool-versions")
asdf plugin-add poetry https://github.com/asdf-community/asdf-poetry.git
asdf install poetry
## Install libdev for custom python version with ASDF
install-libdev:
@$(call title, Install libdev requirement for python\n=> https://github.com/pyenv/pyenv/wiki#suggested-build-environment)
if [ "$(shell uname)" == "Darwin" ]; then \
brew install openssl readline sqlite3 xz zlib tcl-tk; \
else\
sudo apt update; \
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev; \
fi
## Upgrade PIP
upgrade-pip:
@$(call poetry, pip install --upgrade pip, "Upgrade pip")
## Full install for local dev
install-asdf-full:
@$(call title,"ASDF Install in .tool-versions")
asdf plugin-add poetry https://github.com/asdf-community/asdf-poetry.git
asdf plugin-add python
asdf plugin add pre-commit
asdf install poetry
asdf install python
asdf install pre-commit
## Install poetry venv
install-poetry:
poetry install
## Configure poetry for local dev
configure-poetry:
@$(call title,"Poetry set python version")
if [ "$(shell uname)" == "Darwin" ]; then \
echo "Update python version $(PYTHON_VERSION) in pyproject.toml"
sed -i '' 's/python = .*/python = "$(PYTHON_VERSION)"/g' pyproject.toml
echo "Update python version $(PYTHON_VERSION) in Dockerfile"
sed -i '' 's/FROM python:.*/FROM python:$(PYTHON_VERSION)-slim/g' Dockerfile
echo "Update python version $(PYTHON_VERSION) in tox.ini"
sed -i '' 's/min_python_version.*/min_python_version = $(PYTHON_VERSION)/g' tox.ini .flake8
else\
echo "Update python version $(PYTHON_VERSION) in pyproject.toml"
sed -i 's/python = .*/python = "$(PYTHON_VERSION)"/g' pyproject.toml
echo "Update python version $(PYTHON_VERSION) in Dockerfile"
sed -i 's/FROM python:.*/FROM python:$(PYTHON_VERSION)-slim/g' Dockerfile
echo "Update python version $(PYTHON_VERSION) in tox.ini & .flake8"
sed -i 's/min_python_version.*/min_python_version = $(PYTHON_VERSION)/g' tox.ini .flake8
fi
@$(call title,"Switch venv to $(PYTHON_VERSION)")
poetry env use ~/.asdf/installs/python/$(PYTHON_VERSION)/bin/python
@$(call title,"Poetry self plugin")
poetry self add poetry-plugin-export poetry-dotenv-plugin
poetry self update
@$(call title,"Poetry install")
poetry install
poetry update
######################################
## GIT
######################################
## Git Init
init-pre-commit:
@$(call poetry, pre-commit install -t pre-commit -t commit-msg, "Init Pre-Commit in Git Hooks")
## Run Pre-Commit
pre-commit: init-pre-commit
@$(call poetry, pre-commit run -a, "Run Pre-Commit")
######################################
## LOCAL RUNNING
######################################
## Run in local
run: init install-poetry disable-debug disable-dev
@$(call poetry, --ansi python src/main.py, "Run main.py")
## Run in dev mode
dev: init install-poetry enable-debug enable-dev
@$(call poetry, --ansi python src/main.py, "Run main.py")
## Enable debug mode
enable-debug:
@$(call poetry, python -c "$$set_env" DEBUG true, "Enable debug mode")
## Disable debug mode
disable-debug:
@$(call poetry, python -c "$$set_env" DEBUG False, "Disable debug mode")
## Enable debug mode
enable-dev:
@$(call poetry, python -c "$$set_env" DEV true, "Enable dev mode")
## Disable debug mode
disable-dev:
@$(call poetry, python -c "$$set_env" DEV false, "Disable dev mode")
######################################
## PYTHON PROCESSING
######################################
## Generate requirements.txt
generate-dependencies:
@$(call title,"Generate requirements.txt")
poetry export --without-hashes --output src/requirements.txt
clean: python-clean
## Clean venv
python-clean:
rm -Rf $(VENV)
######################################
## TESTS
######################################
## Run PyTest
pytest: init
if [ ! $$? -ne 0 ]; then \
$(call poetry, tox -e pytest, "Run PyTest"); \
fi
pytest-mock:
pytest-sandbox:
pytest-staging:
pytest-production:
pytest-%:
$(call poetry, tox -e pytest, "Run PyTest");
######################################
## CODE QUALITY
######################################
## Run regression testing
tox: init
@$(call poetry, tox, "Run TOX")
format: code-format
## Check code formatting
code-format: black flake8 pylint ruff
## Run black
black: init
@$(call poetry, task black, "Run Black")
## Run Black formater
black-format: init
@$(call poetry, task black-style, "Run Black formater")
##Run flake8
flake8: init
@$(call poetry, task flake8, "Run Flake8")
##Run pylint
pylint: init
@$(call poetry, task pylint, "Run pylint")
##Run Ruff
ruff: init
@$(call poetry, task ruff, "Run Ruff")
##Run Ruff fixe
ruff-fixe: init
@$(call poetry, task ruff-fix, "Run Ruff fix code")
##Run Vulture
vulture: init
@$(call poetry, task vulture, "Run Vulture")
######################################
## DOCKER
######################################
## Build image
build: generate-dependencies
@$(call title,"Build image in local")
docker build ./
######################################
## MAKEFILE FUNCTION
######################################
define title
/bin/echo -e "\n------------------------------------------------\n${1}\n------------------------------------------------\n"
endef
define poetry
$(call title, ${2})
touch .env
poetry run -v ${1}
endef
######################################
## PYTHON SCRIPTS
######################################
define set_env
from sys import argv
KEY = argv[1]
VALUE = argv[2]
found = False
new_file = []
print(f"Set {KEY}={VALUE}")
with open(f".env", 'r') as file:
env = file.read().splitlines()
for line in env:
if line.startswith(f"{KEY}="):
new_file.append(f"{KEY}={VALUE}")
found = True
else:
new_file.append(line)
if not found:
env.append(f"{KEY}={VALUE}")
else:
env = new_file
with open(f".env", 'w') as file:
file.write("\n".join(env))
endef
export set_env