python.mk 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # The following python-related tools and processes are supported:
  2. # * black
  3. # * coverage (requires ".coveragerc")
  4. # * flake8
  5. # * pypi upload (requires "~/.pypirc")
  6. # * tox / unittests (requires "tox.ini")
  7. #
  8. PYTHON_BIN ?= python3
  9. DIR_PYTHON_BUILD = $(DIR_BUILD)/python-lib
  10. PYTHON_BDIST_ARGS = --dist-dir $(DIR_BUILD)/python-packages
  11. PYTHON_BUILD_ARGS = --build-base "$(DIR_PYTHON_BUILD)"
  12. PYTHON_INSTALL_ARGS = --root "$(abspath $(DESTDIR))"
  13. DISABLE_PYTHON_TESTS ?= 0
  14. PYPI_UPLOAD_TARGET = https://pypi.python.org/pypi
  15. DIR_PYTHON_COVERAGE_HTML = $(DIR_BUILD)/coverage-html
  16. # detect if flake8 is available as an executable (e.g. Arch) or as a python module (e.g. Debian)
  17. FLAKE8_BIN ?= $(shell hash flake8 2>/dev/null && echo "flake8" || echo "$(PYTHON_BIN) -m flake8")
  18. FLAKE8_FILES ?= $(DIR_PYTHON_SETUP)
  19. RUN_PYTHON = $(PYTHON_BIN)
  20. BLACK_BIN ?= black
  21. # projects may want to override this with something like "--target-version=py39"
  22. BLACK_ARGS ?=
  23. BLACK_FILES ?= $(DIR_PYTHON_SETUP)
  24. .PHONY: help
  25. help: help-python
  26. .PHONY: help-python
  27. help-python:
  28. @echo "Python-specific packaging targets:"
  29. @echo " clean-python"
  30. @echo " dist-python"
  31. @echo " lint-python"
  32. @echo " report-python"
  33. @echo " style-python"
  34. @echo " test-python"
  35. @echo " upload-python"
  36. @echo
  37. .PHONY: dist
  38. dist: dist-python
  39. .PHONY: install
  40. install: install-python
  41. .PHONY: lint
  42. lint: lint-python
  43. .PHONY: style
  44. style: style-python
  45. ifneq ($(DISABLE_PYTHON_TESTS),1)
  46. .PHONY: test
  47. test: test-python
  48. endif
  49. .PHONY: install-python
  50. install-python:
  51. cd "$(DIR_PYTHON_SETUP)" && $(RUN_PYTHON) setup.py install $(PYTHON_INSTALL_ARGS)
  52. .PHONY: dist-python
  53. dist-python: clean-python
  54. cd "$(DIR_PYTHON_SETUP)" \
  55. && $(RUN_PYTHON) setup.py build $(PYTHON_BUILD_ARGS) bdist $(PYTHON_BDIST_ARGS)
  56. .PHONY: check-pypi-config
  57. check-pypi-config:
  58. @# verify that the section for our internal target is not missing
  59. @if grep -q "^\[$(PYPI_UPLOAD_TARGET)\]$$" ~/.pypirc; then true; else \
  60. echo "ERROR: missing '$(PYPI_UPLOAD_TARGET)' section in ~/.pypirc: "\
  61. "take a look at pypirc.sample" >&2; \
  62. exit 1; \
  63. fi
  64. .PHONY: upload
  65. upload: upload-python
  66. .PHONY: upload-python
  67. upload-python: check-pypi-config
  68. cd "$(DIR_PYTHON_SETUP)" && $(RUN_PYTHON) setup.py sdist upload -r "$(PYPI_UPLOAD_TARGET)"
  69. .PHONY: lint-python
  70. ifneq ($(DISABLE_PYTHON_BLACK),1)
  71. lint-python: lint-python-black
  72. endif
  73. ifneq ($(DISABLE_PYTHON_FLAKE8),1)
  74. lint-python: lint-python-flake8
  75. endif
  76. .PHONY: lint-python-black
  77. lint-python-black:
  78. $(BLACK_BIN) --check $(BLACK_ARGS) $(BLACK_FILES)
  79. .PHONY: lint-python-flake8
  80. lint-python-flake8:
  81. $(FLAKE8_BIN) $(FLAKE8_FILES)
  82. .PHONY: style-python
  83. style-python:
  84. $(BLACK_BIN) $(BLACK_ARGS) $(BLACK_FILES)
  85. .PHONY: test-python
  86. test-python:
  87. if [ -e tox.ini ]; then tox; fi
  88. $(RUN_PYTHON) $(PYTHON_TEST_ARGS)
  89. .PHONY: report
  90. report: report-python
  91. .PHONY: report-python
  92. report-python:
  93. if [ -e .coveragerc ]; then $(MAKE) report-python-coverage; fi
  94. .PHONY: report-python-coverage
  95. report-python-coverage:
  96. @# run tests for coverage analysis
  97. $(RUN_PYTHON) -m coverage run $(PYTHON_TEST_ARGS)
  98. @# provide textual report
  99. $(RUN_PYTHON) -m coverage report
  100. $(RUN_PYTHON) -m coverage html --directory "$(DIR_PYTHON_COVERAGE_HTML)"
  101. @printf "Browse the coverage report:\n\tfile://%s/index.html\n" \
  102. "$(abspath $(DIR_PYTHON_COVERAGE_HTML))"
  103. @# run a visual browser if the DISPLAY variable is set
  104. @if [ -n "$$DISPLAY" ] && [ -n "$(COVERAGE_BROWSER_BIN)" ]; then \
  105. "$(COVERAGE_BROWSER_BIN)" "$(DIR_PYTHON_COVERAGE_HTML)/index.html"; fi
  106. .PHONY: clean-python
  107. clean-python:
  108. cd "$(DIR_PYTHON_SETUP)" && python3 setup.py clean || true
  109. @# datafile for python3-coverage
  110. $(RM) .coverage
  111. @# workaround for https://github.com/pypa/setuptools/issues/436
  112. $(RM) "$(DIR_PYTHON_SETUP)"/*.egg-info/SOURCES.txt
  113. clean: clean-python