# -*- mode: makefile-gmake -*- ## Définition des variables # Le nom de l'exécutable Python à utiliser ou son chemin absolu # (ex. : python ou python3). PYTHON_EXE := python3 # S'il faut utiliser un environnement virtuel (y ou n). USE_VENV := y # Configuration de l'environnement virtuel. VENV_DIR := venv VENV_OPT := --system-site-packages # Définis les chemins et options des exécutables. PYTHON_EXE_BASENAME := $(shell basename $(PYTHON_EXE)) VENV_PYTHON := --python=$(PYTHON_EXE_BASENAME) ifeq ($(USE_VENV), y) PYTHON := $(VENV_DIR)/bin/$(PYTHON_EXE_BASENAME) PIP := $(VENV_DIR)/bin/pip else PYTHON := $(shell which $(PYTHON_EXE)) PIP := $(shell which pip) endif # Détermine l'environnement à utiliser. ifndef ENV ifdef DJANGO_SETTINGS_MODULE ENV = $(shell echo $(DJANGO_SETTINGS_MODULE) | cut -d. -f3) else DEFAULT_ENV := production ENV = $(shell \ sed -n '/^ENV/s/[^=]*=\(.*\)/\1/p' config.env 2> /dev/null \ | tail -n 1 | grep -Ee '^..*' || echo "$(DEFAULT_ENV)") endif endif # Définis EDITOR pour l'édition interactive. ifndef EDITOR ifdef VISUAL EDITOR := $(VISUAL) else EDITOR := vi endif endif # Définition des cibles ------------------------------------------------------- .DEFAULT_GOAL := help # Commentaire d'une cible : #-> interne ##-> aide production+dev ###-> aide dev .PHONY: help help: ## affiche cette aide ifeq ($(ENV), production) @perl -nle'print $& if m{^[a-zA-Z_-]+:[^#]*?## .*$$}' $(MAKEFILE_LIST) \ | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' else @perl -nle'print $& if m{^[a-zA-Z_-]+:[^#]*?###? .*$$}' $(MAKEFILE_LIST) \ | sort | awk 'BEGIN {FS = ":.*?###? "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' endif .PHONY: clean clean: clean-build clean-pyc clean-static ## nettoie tous les fichiers temporaires .PHONY: clean-build clean-build: ### nettoie les fichiers de construction du paquet rm -rf build/ rm -rf dist/ rm -rf *.egg-info .PHONY: clean-pyc clean-pyc: ### nettoie les fichiers temporaires python find center_pilote/ \ \( -name '*.pyc' -o -name '*.pyo' -o -name '*~' \) -exec rm -f {} + .PHONY: clean-static clean-static: ### nettoie les fichiers "static" collectés rm -rf var/static .PHONY: init init: check-env create-venv config.env ## initialise l'environnement et l'application @$(MAKE) --no-print-directory update .PHONY: check-env check-env: bin/check_env.sh config.env: cp config.env.example config.env chmod go-rwx config.env $(EDITOR) config.env .PHONY: update update: check-config install-deps migrate static populate set-version job-restart ## mets à jour l'application et ses dépendances touch center_pilote/wsgi.py .PHONY: set-version set-version: ## met a jour _version.py avec setuptools_scm $(PYTHON) -m setuptools_scm .PHONY: check check: check-env check-config ## vérifie la configuration de l'instance $(PYTHON) manage.py check .PHONY: check-config check-config: @find . -maxdepth 1 -name config.env -perm /o+rwx -exec false {} + || \ { echo "\033[31mErreur :\033[0m les permissions de config.env ne sont pas bonnes, \ vous devriez au moins faire : chmod o-rwx config.env"; false; } .PHONY: check-migrations check-migrations: ## vérifie qu'il n'y a pas de difference entre les migrations et les models $(PYTHON) manage.py makemigrations --dry-run --noinput --check .PHONY: install-deps install-deps: ## installe les dépendances de l'application $(PIP) install --upgrade pip setuptools wheel pkg_resources $(PIP) install --upgrade --requirement requirements/$(ENV).txt .PHONY: migrate migrate: ## mets à jour le schéma de la base de données $(PYTHON) manage.py migrate .PHONY: static static: ## collecte les fichiers statiques @echo "Collecte des fichiers statiques..." $(PYTHON) manage.py generatemedia .PHONY: populate populate: ## peuple la base de donnee $(PYTHON) manage.py creme_populate ## Cibles liées à l'environnement virtuel .PHONY: create-venv create-venv: $(PYTHON) $(PYTHON): ifeq ($(USE_VENV), y) virtualenv $(VENV_OPT) $(VENV_PYTHON) $(VENV_DIR) else @echo "\033[31mErreur !\033[0m Impossible de trouver l'exécutable Python $(PYTHON)" @exit 1 endif .PHONY: clear-venv clear-venv: ## supprime l'environnement virtuel -rm -rf $(VENV_DIR) ## Cibles pour le développement .PHONY: serve serve: ### démarre un serveur local pour l'application $(PYTHON) manage.py runserver .PHONY: test test: ### lance les tests unitaires de l'application $(PYTHON) -m pytest --cov --cov-report=term:skip-covered --no-migrations .PHONY: test-wip test-wip: #### lance les tests unitaires marqués 'wip' $(PYTHON) -m pytest --capture=no --cov --cov-report=html -vv -m 'wip' --pdb --no-migrations .PHONY: test-failed test-failed: #### lance les tests qui ont échoué $(PYTHON) -m pytest --lf --no-migrations .PHONY: coverage coverage: test behave ### vérifie la couverture de code (test unitaires et de comportement) $(PYTHON) -m coverage html @echo open file:///$(PWD)/htmlcov/index.html .PHONY: behave behave: ### lance les tests de comportement (BDD) $(PYTHON) manage.py behave --settings=center_pilote.settings.test --simple .PHONY: behave-wip behave-wip: ### lance les tests (WIP) de comportement (BDD) $(PYTHON) manage.py behave --settings=center_pilote.settings.test --simple --wip .PHONY: test-migrations test-migrations: check-migrations ### lance un test sur les migrations $(PYTHON) -m pytest center_pilote/tests/migrations.py .PHONY: lint lint: ### vérifie la syntaxe du code Python @$(PYTHON) -m flake8 center_pilote || \ { echo "\033[31mErreur !\033[0m Veuillez corriger la syntaxe avec : make format"; false; } .PHONY: format format: ### formate le code Python $(PYTHON) -m isort center_pilote $(PYTHON) -m black center_pilote .PHONY: check-precommit check-precommit: lint check test check-migrations test-migrations ### check most before commit .PHONY: check-all check-all: format lint update check test check-migrations test-migrations ### check all before commit .PHONY: shell shell: ### lance un shell Python dans l'environnement ifeq ($(ENV), production) $(PYTHON) manage.py shell else $(PYTHON) manage.py shell_plus endif .PHONY: job-preinstall job-preinstall: $(HOME)/.config/systemd/user/cremecrm-job-manager.service @systemctl --user enable cremecrm-job-manager @systemctl --user start cremecrm-job-manager .PHONY: job-install job-install: job-preinstall job-check ## install creme_job_manager as user systemd service .PHONY: job-check job-check: ## check creme_job_manager is installed as user systemd service @systemctl --user is-enabled cremecrm-job-manager || { echo "cremecrm-job-manager not enabled"; exit 1; } @systemctl --user is-active cremecrm-job-manager || { echo "cremecrm-job-manager not active"; exit 1; } .PHONY: job-restart job-restart: ## restart creme_job_manager if already running (after a code update) @systemctl --user is-active cremecrm-job-manager && systemctl --user restart cremecrm-job-manager ; exit 0; .PHONY: job-start job-start: ## start creme-job-manager @systemctl --user start cremecrm-job-manager || { echo "cremecrm-job-manager failed to start" ; exit 1; } $(HOME)/.config/systemd/user/cremecrm-job-manager.service: mkdir -p $(HOME)/.config/systemd/user @echo "[Unit]\nDescription=CremeCRM Job Manager\n\n"\ "[Service]\n"\ "WorkingDirectory=$(PWD)\n"\ "ExecStart=$(PWD)/venv/bin/python manage.py creme_job_manager\n"\ "Restart=always\nRestartSec=10\n\n"\ "[Install]\nWantedBy=default.target" > $(HOME)/.config/systemd/user/cremecrm-job-manager.service .PHONY: job-uninstall job-uninstall: ## uninstall creme_job_manager as user systemd service @systemctl --user stop cremecrm-job-manager @systemctl --user disable cremecrm-job-manager rm $(HOME)/.config/systemd/user/cremecrm-job-manager.service