python.mk 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # The following python-related tools and processes are supported:
  2. # * coverage (requires ".coveragerc")
  3. # * flake8
  4. # * pypi upload (requires "~/.pypirc")
  5. # * tox / unittests (requires "tox.ini")
  6. #
  7. PYTHON_BIN ?= python3
  8. DIR_PYTHON_BUILD = $(DIR_BUILD)/python-lib
  9. PYTHON_BDIST_ARGS = --dist-dir $(DIR_BUILD)/python-packages
  10. PYTHON_BUILD_ARGS = --build-base "$(DIR_PYTHON_BUILD)"
  11. PYTHON_INSTALL_ARGS = --root "$(abspath $(DESTDIR))"
  12. PYPI_UPLOAD_TARGET = https://pypi.python.org/pypi
  13. DIR_PYTHON_COVERAGE_HTML = $(DIR_BUILD)/coverage-html
  14. PYTHON_COVERAGE_BIN ?= python3-coverage
  15. # detect if flake8 is available as an executable (e.g. Arch) or as a python module (e.g. Debian)
  16. FLAKE8_BIN ?= $(shell hash flake8 2>/dev/null && echo "flake8" || echo "$(PYTHON_BIN) -m flake8")
  17. .PHONY: help
  18. help: help-python
  19. .PHONY: help-python
  20. help-python:
  21. @echo "Python-specific packaging targets:"
  22. @echo " clean-python"
  23. @echo " dist-python"
  24. @echo " lint-python"
  25. @echo " report-python"
  26. @echo " test-python"
  27. @echo " upload-python"
  28. @echo
  29. .PHONY: dist
  30. dist: dist-python
  31. .PHONY: install
  32. install: install-python
  33. .PHONY: lint
  34. test: lint-python
  35. .PHONY: test
  36. test: test-python
  37. .PHONY: install-python
  38. install-python:
  39. cd "$(DIR_PYTHON_SETUP)" && $(PYTHON_BIN) setup.py install $(PYTHON_INSTALL_ARGS)
  40. .PHONY: dist-python
  41. dist-python: clean-python
  42. cd "$(DIR_PYTHON_SETUP)" \
  43. && $(PYTHON_BIN) setup.py build $(PYTHON_BUILD_ARGS) bdist $(PYTHON_BDIST_ARGS)
  44. .PHONY: check-pypi-config
  45. check-pypi-config:
  46. @# verify that the section for our internal target is not missing
  47. @if grep -q "^\[$(PYPI_UPLOAD_TARGET)\]$$" ~/.pypirc; then true; else \
  48. echo "ERROR: missing '$(PYPI_UPLOAD_TARGET)' section in ~/.pypirc: "\
  49. "take a look at pypirc.sample" >&2; \
  50. exit 1; \
  51. fi
  52. .PHONY: upload
  53. upload: upload-python
  54. .PHONY: upload-python
  55. upload-python: check-pypi-config
  56. cd "$(DIR_PYTHON_SETUP)" && $(PYTHON_BIN) setup.py sdist upload -r "$(PYPI_UPLOAD_TARGET)"
  57. .PHONY: lint-python
  58. lint-python:
  59. $(FLAKE8_BIN) "$(DIR_PYTHON_SETUP)"
  60. .PHONY: test-python
  61. test-python:
  62. if [ -e tox.ini ]; then tox; fi
  63. .PHONY: report
  64. report: report-python
  65. .PHONY: report-python
  66. report-python:
  67. if [ -e .coveragerc ]; then $(MAKE) report-python-coverage; fi
  68. .PHONY: report-python-coverage
  69. report-python-coverage:
  70. @# run unittests for coverage analysis
  71. "$(PYTHON_COVERAGE_BIN)" run -m unittest discover
  72. @# provide textual report
  73. "$(PYTHON_COVERAGE_BIN)" report
  74. @# run a visual browser if the DISPLAY variable is set
  75. @if [ -n "$$DISPLAY" ]; then \
  76. "$(PYTHON_COVERAGE_BIN)" report --directory "$(DIR_PYTHON_COVERAGE_HTML)" ;\
  77. x-www-browser "$(DIR_PYTHON_COVERAGE_HTML)"; fi
  78. .PHONY: clean-python
  79. clean-python:
  80. cd "$(DIR_PYTHON_SETUP)" && python3 setup.py clean || true
  81. @# datafile for python3-coverage
  82. $(RM) .coverage