python-virtualenv.mk 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # a typical argument could be "--system-site-packages"
  2. VIRTUALENV_CREATE_ARGUMENTS ?=
  3. # when using virtualenv sys.real_prefix is set and we use the path to the python
  4. # binary to determine the actual location of the virtualenv. In any other case
  5. # we fallback to the VIRTUAL_ENV variable or the `.venv` directory
  6. DIR_VIRTUALENV := $(shell echo "import sys; hasattr(sys, 'real_prefix') or sys.exit(1)" | "$(PYTHON_BIN)" && \
  7. echo "$$(dirname "$$(dirname "$$(which python)")")" || echo "$${VIRTUAL_ENV:-"$(DIR_BUILD)/venv"}")
  8. ACTIVATE_VIRTUALENV ?= $(DIR_VIRTUALENV)/bin/activate
  9. VIRTUALENV_STAMP = $(DIR_VIRTUALENV)/.stamp_virtualenv
  10. # Pre-dependencies need to be met before the virtualenv is generated.
  11. # The "wheel" dependency is necessary for many packages built via pip.
  12. VIRTUALENV_PREDEPENDENCIES ?= venv wheel
  13. # prepend all python calls with the virtualenv activation
  14. RUN_PYTHON = . "$(ACTIVATE_VIRTUALENV)" && "$(PYTHON_BIN)"
  15. # add dependencies for the virtualenv to related targets
  16. install-python: $(VIRTUALENV_STAMP)
  17. dist-python: $(VIRTUALENV_STAMP)
  18. test-python: $(VIRTUALENV_STAMP)
  19. report-python-coverage: $(VIRTUALENV_STAMP)
  20. .PHONY: help-python-virtualenv
  21. help-python-virtualenv:
  22. @echo "Targets related to virtualenv (Python):"
  23. @echo " virtualenv-check"
  24. @echo " virtualenv-update"
  25. @echo
  26. help-python: help-python-virtualenv
  27. # Check if all required modules for preparing a virtual environment are available.
  28. # Emit helpful errors in case of missing modules.
  29. .PHONY: virtualenv-dependency-check
  30. virtualenv-predependency-check:
  31. @for module in $(VIRTUALENV_PREDEPENDENCIES); do \
  32. if ! "$(PYTHON_BIN)" -c "import $$module"; then \
  33. echo >&2 "ERROR: missing python module '$$module'. Installation hint: apt install python3-$$module"; exit 1; \
  34. fi; done
  35. $(ACTIVATE_VIRTUALENV): virtualenv-predependency-check
  36. @if [ ! -e "$(ACTIVATE_VIRTUALENV)" ]; then "$(PYTHON_BIN)" -m venv $(VIRTUALENV_CREATE_ARGUMENTS) "$(DIR_VIRTUALENV)"; fi
  37. .PHONY: virtualenv-check
  38. virtualenv-check: $(ACTIVATE_VIRTUALENV)
  39. $(VIRTUALENV_STAMP): $(VIRTUALENV_REQUIREMENTS_FILE) $(ACTIVATE_VIRTUALENV)
  40. if [ -z "$${VIRTUAL_ENV:-}" ]; then . "$(ACTIVATE_VIRTUALENV)"; fi \
  41. && for dep in $(VIRTUALENV_PREDEPENDENCIES); do "$(PYTHON_BIN)" -c "import $$dep" 2>/dev/null || "$(PYTHON_BIN)" -m pip install "$$dep"; done \
  42. && "$(PYTHON_BIN)" -m pip install -r "$(VIRTUALENV_REQUIREMENTS_FILE)"
  43. touch "$(VIRTUALENV_STAMP)"
  44. .PHONY: virtualenv-update
  45. virtualenv-update: $(VIRTUALENV_STAMP)
  46. .PHONY: clean-python-virtualenv
  47. clean-python-virtualenv:
  48. $(RM) -r "$(DIR_VIRTUALENV)"
  49. python-clean: clean-python-virtualenv