GlacioHack / xdem

Analysis of digital elevation models (DEMs)
https://xdem.readthedocs.io/
MIT License
144 stars 41 forks source link

[POC] Makefile installations #582

Open adebardo opened 2 months ago

adebardo commented 2 months ago

Context

The purpose of this ticket is to set up the first makefile, which will aim to simplify the installation of xdem for developers using Linux.

To begin, we will draw inspiration from the makefile of demcompare.

Example

# Autodocumented Makefile
# see: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
# Dependencies : python3 venv
# Some Makefile global variables can be set in make command line
# Recall: .PHONY  defines special targets not associated with files

############### GLOBAL VARIABLES ######################
.DEFAULT_GOAL := help
# Set shell to BASH
SHELL := /bin/bash

# Set Virtualenv directory name
# Example: VENV="other-venv/" make install
ifndef VENV
    VENV = "venv"
endif

# Browser definition for sphinx and coverage
define BROWSER_PYSCRIPT
import os, webbrowser, sys

from urllib.request import pathname2url

webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT
BROWSER := python -c "$$BROWSER_PYSCRIPT"

# Python global variables definition
PYTHON_VERSION_MIN = 3.9
# Set PYTHON if not defined in command line
# Example: PYTHON="python3.10" make venv to use python 3.10 for the venv
# By default the default python3 of the system.
ifndef PYTHON
    PYTHON = "python3"
endif
PYTHON_CMD=$(shell command -v $(PYTHON))

PYTHON_VERSION_CUR=$(shell $(PYTHON_CMD) -c 'import sys; print("%d.%d"% sys.version_info[0:2])')
PYTHON_VERSION_OK=$(shell $(PYTHON_CMD) -c 'import sys; cur_ver = sys.version_info[0:2]; min_ver = tuple(map(int, "$(PYTHON_VERSION_MIN)".split("."))); print(int(cur_ver >= min_ver))')

############### Check python version supported ############

ifeq (, $(PYTHON_CMD))
    $(error "PYTHON_CMD=$(PYTHON_CMD) not found in $(PATH)")
endif

ifeq ($(PYTHON_VERSION_OK), 0)
    $(error "Requires python version >= $(PYTHON_VERSION_MIN). Current version is $(PYTHON_VERSION_CUR)")
endif

################ MAKE targets by sections ######################

help: ## this help
    @echo "      XDEM MAKE HELP"
    @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: venv
venv: ## create virtualenv in "venv" dir if not exists
    @test -d ${VENV} || $(PYTHON_CMD) -m venv ${VENV}
    @touch ${VENV}/bin/activate
    @${VENV}/bin/python -m pip install --upgrade wheel setuptools pip # no check to upgrade each time

.PHONY: install
install: venv  ## install environment for development target (depends venv)
    @test -f ${VENV}/bin/xdem || echo "Install xdem package from local directory"
    @test -f ${VENV}/bin/xdem || ${VENV}/bin/pip install -e .[dev]
    @test -f .git/hooks/pre-commit || echo "Install pre-commit"
    @test -f .git/hooks/pre-commit || ${VENV}/bin/pre-commit install -t pre-commit
    @test -f .git/hooks/pre-push || ${VENV}/bin/pre-commit install -t pre-push
    @chmod +x ${VENV}/bin/register-python-argcomplete
    @echo "Xdem installed in dev mode in virtualenv ${VENV} with Sphinx docs"
    @echo "Xdem venv usage : source ${VENV}/bin/activate; xdem -h"

## Clean section

.PHONY: clean
clean: clean-venv clean-build clean-precommit clean-pyc clean-test clean-lint clean-docs clean-notebook ## clean all

.PHONY: clean-venv
clean-venv: ## clean venv
    @echo "+ $@"
    @rm -rf ${VENV}

.PHONY: clean-build
clean-build: ## clean build artifacts
    @echo "+ $@"
    @rm -fr build/
    @rm -fr dist/
    @rm -fr .eggs/
    @find . -name '*.egg-info' -exec rm -fr {} +
    @find . -name '*.egg' -exec rm -f {} +

.PHONY: clean-precommit
clean-precommit: ## clean precommit hooks in .git/hooks
    @rm -f .git/hooks/pre-commit
    @rm -f .git/hooks/pre-push

.PHONY: clean-pyc
clean-pyc: ## clean Python file artifacts
    @echo "+ $@"
    @find . -type f -name "*.py[co]" -exec rm -fr {} +
    @find . -type d -name "__pycache__" -exec rm -fr {} +
    @find . -name '*~' -exec rm -fr {} +

Tests

adehecq commented 1 month ago

I'm struggling to understand all the commands in the Makefile. Could you please show an example of which commands would be run when typing make install? But in general, I'm fine with the suggested setup.

adebardo commented 1 month ago

If we run the make install command, we can see a dependency on the "make venv" command, which checks and creates the virtual environment if necessary, then performs installation commands such as "pip install -e .", the installation of pre-commits, etc.