Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Makefile 3.85 KiB
# bash sell to correctly interpret the double brackets in the conditions below
SHELL=/bin/bash
# https://www.gnu.org/software/make/manual/html_node/One-Shell.html
# Required to prevent having to use "conda init"

ifneq ($(shell docker compose version 2>/dev/null),)
  DOCKER_COMPOSE=docker compose
else ifneq ($(shell docker-compose --version 2>/dev/null),)
  DOCKER_COMPOSE=docker-compose
endif

# name for docker image to run the CI in
DOCKER_CI_TAG ?= web_reflectivity_test

# all the lines in a recipe are passed to a single invocation of the shell.
.ONESHELL:

# list of all phony targets, alphabetically sorted
.PHONY: all clean conda config dev docs devdocs help redev startdev static-checks test build-docker test-docker wheel wheel-publish

help:
    # this nifty perl one-liner collects all commnents headed by the double "#" symbols next to each target and recycles them as comments
	@perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-25s\033[0m %s\n", $$1, $$2}'

clean:  ## deletes containers, network, volumes, and images
	$(DOCKER_COMPOSE) down -v
	docker system prune -a --volumes --force

config:
	# create "docker" group if non-existing
	@if [ ! $$(getent group docker) ]; then echo "sudo group add docker"; sudo groupadd docker; newgrp docker; else echo "group 'docker' already exists"; fi
	# add yourself to "docker" group if you don't already belong
	@if ! [[ " $$(groups) " =~ ' docker ' ]]; then echo "sudo usermod -aG docker $$USER"; sudo usermod -aG docker $$USER; else echo "user already belongs to group 'docker'"; fi
	docker login regproxy.ornl.gov
	# create bind mounts for easy access to the logs
	for DIR in nginx web db redis worker;do mkdir -p /tmp/log/web_reflectivity/$$DIR;done
	chown -R $$USER:docker /tmp/log/web_reflectivity

startdev:  ## invoke docker-compose to create images, instantiate containers, and start services
	\cp ./cfg/docker-compose.local.yml ./docker-compose.yml
	$(DOCKER_COMPOSE) up --build

dev: config startdev  ## installs the local dev environment for the first time. Requires sudo privileges if the 'docker' group doesn't exist or if you don't belong nto such group.

redev: clean startdev  ## reinstalls the local dev environment after the cleaning step

# Note that the extra activate is needed to ensure that the activate floats env to the front of PATH
CONDA_ACTIVATE=source $$(conda info --base)/etc/profile.d/conda.sh ; conda activate
conda:  ## installs, then activates conda environment webrefl with all dependencies
	conda create --name webrefl python=3.8
	conda install --name webrefl -c conda-forge mamba
	$(CONDA_ACTIVATE) webrefl
	mamba env update --name webrefl --file ./src/environment.yml
	conda develop .

docs:  ## create HTML docs under docs/_build/html/. Requires activation of the webrefl conda environment
	# this will fail on a warning
	@cd docs&& make html SPHINXOPTS="-W --keep-going -n" && echo -e "##########\n DOCS point your browser to file://$$(pwd)/build/html/index.html\n##########"

test:  ## run unit tests
	cd src
	export APP_SECRET=secret
	export DJANGO_SETTINGS_MODULE='web_reflectivity.settings.unittest'
	conda develop .  # install source in devcelop mode
	python web_reflectivity/manage.py makemigrations --noinput
	python web_reflectivity/manage.py migrate --noinput
	coverage run web_reflectivity/manage.py test && coverage report

wheel:  ## create binary wheel package containing web-reflectivity and auxiliary packages datahandler, fitting, tools, and users
    # --no-isolation: do not build using a virtual environment
	cd src && python -m build --no-isolation --wheel

wheel-publish: wheel
	cfg/publish_wheel.sh


build-docker: ## build the image for the package under src/
	docker build --network host --cache-from=$(DOCKER_CI_TAG) --build-arg TWINE_USERNAME=${TWINE_USERNAME} --build-arg TWINE_REPO=${TWINE_REPO} -f cfg/Dockerfile.ci -t $(DOCKER_CI_TAG) .