Skip to content

Commit

Permalink
fix: handling gdal dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
vschaffn committed Nov 22, 2024
1 parent b9fe77a commit f26b986
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
38 changes: 31 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ############

Expand All @@ -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 ######################
Expand All @@ -44,19 +45,42 @@ 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"
@test -f ${VENV}/bin/xdem || ${VENV}/bin/pip install -e .[dev]
@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

Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ test =
pylint
scikit-learn
richdem
gdal
doc =
sphinx
sphinx-book-theme
Expand All @@ -77,6 +76,6 @@ dev =
pre-commit
%(opt)s
%(doc)s
%(test)s
all =
%(dev)s
%(test)s

0 comments on commit f26b986

Please sign in to comment.