From f26b986ed0803fb115b1541526d3353e1f5ffd18 Mon Sep 17 00:00:00 2001 From: vschaffn Date: Wed, 20 Nov 2024 17:18:01 +0100 Subject: [PATCH] fix: handling gdal dependency --- Makefile | 38 +++++++++++++++++++++++++++++++------- setup.cfg | 3 +-- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index a91e1dd5..fffad868 100644 --- a/Makefile +++ b/Makefile @@ -12,15 +12,16 @@ ifndef VENV endif # Python version requirement -PYTHON_VERSION_MIN = 3.9 +PYTHON_VERSION_REQUIRED = 3.10 ifndef PYTHON - PYTHON = "python3" + # Try to find python version required + PYTHON = "python$(PYTHON_VERSION_REQUIRED)" 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))') +PYTHON_VERSION_OK=$(shell $(PYTHON_CMD) -c 'import sys; req_ver = tuple(map(int, "$(PYTHON_VERSION_REQUIRED)".split("."))); cur_ver = sys.version_info[0:2]; print(int(cur_ver == req_ver))') ############### Check python version supported ############ @@ -29,7 +30,7 @@ ifeq (, $(PYTHON_CMD)) endif ifeq ($(PYTHON_VERSION_OK), 0) - $(error "Requires python version >= $(PYTHON_VERSION_MIN). Current version is $(PYTHON_VERSION_CUR)") + $(error "Requires Python version == $(PYTHON_VERSION_REQUIRED). Current version is $(PYTHON_VERSION_CUR)") endif ################ MAKE Targets ###################### @@ -44,6 +45,20 @@ venv: ## Create a virtual environment in 'venv' directory if it doesn't exist @touch ${VENV}/bin/activate @${VENV}/bin/python -m pip install --upgrade wheel setuptools pip +.PHONY: install-gdal +install-gdal: ## Install GDAL version matching the system's GDAL via pip + @if command -v gdalinfo >/dev/null 2>&1; then \ + GDAL_VERSION=$$(gdalinfo --version | awk '{print $$2}'); \ + echo "System GDAL version: $$GDAL_VERSION"; \ + ${VENV}/bin/pip install gdal==$$GDAL_VERSION; \ + else \ + echo "Warning: GDAL not found on the system. Proceeding without GDAL."; \ + echo "Try installing GDAL by running the following commands depending on your system:"; \ + echo "Debian/Ubuntu: sudo apt-get install -y gdal-bin libgdal-dev"; \ + echo "Red Hat/CentOS: sudo yum install -y gdal gdal-devel"; \ + echo "Then run 'make install-gdal' to proceed with GDAL installation."; \ + fi + .PHONY: install install: venv ## Install xDEM for development (depends on venv) @test -f ${VENV}/bin/xdem || echo "Installing xdem in development mode" @@ -51,12 +66,21 @@ install: venv ## Install xDEM for development (depends on venv) @test -f .git/hooks/pre-commit || echo "Installing pre-commit hooks" @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 + @echo "Attempting to install GDAL..." + @make install-gdal @echo "XDEM installed in development mode in virtualenv ${VENV}" @echo "To use: source ${VENV}/bin/activate; xdem -h" -#.PHONY: test -#test: ## run tests -# @${VENV}/bin/pytest + +.PHONY: test +test: ## run tests + @if ! ${VENV}/bin/python -m pip show gdal >/dev/null 2>&1; then \ + echo "Error: GDAL is not installed in the virtual environment. Tests require GDAL to run."; \ + echo "Please ensure GDAL is installed by running 'make install-gdal'."; \ + exit 1; \ + else \ + ${VENV}/bin/pytest; \ + fi ## Clean section diff --git a/setup.cfg b/setup.cfg index 54c5b767..43c27702 100644 --- a/setup.cfg +++ b/setup.cfg @@ -61,7 +61,6 @@ test = pylint scikit-learn richdem - gdal doc = sphinx sphinx-book-theme @@ -77,6 +76,6 @@ dev = pre-commit %(opt)s %(doc)s + %(test)s all = %(dev)s - %(test)s