|
| 1 | +# Minimal Makefile for Nix and venv |
| 2 | + |
| 3 | +SHELL := /bin/bash |
| 4 | + |
| 5 | + |
| 6 | +export PYTHON ?= $(shell python3 --version >/dev/null 2>&1 && echo python3 || echo python ) |
| 7 | + |
| 8 | +# Ensure $(PYTHON), $(VENV) are re-evaluated at time of expansion, when target $(PYTHON) are known to be available |
| 9 | +PYTHON_V = $(shell $(PYTHON) -c "import sys; print('-'.join((('venv' if sys.prefix != sys.base_prefix else next(iter(filter(None,sys.base_prefix.split('/'))))),sys.platform,sys.implementation.cache_tag)))" 2>/dev/null ) |
| 10 | + |
| 11 | +export TOX ?= tox |
| 12 | +export TOX_OPTS ?= -e py39-extra,py310-extra,py311-extra,py312-extra,py313-extra |
| 13 | +#export TOX_OPTS ?= -e py311-extra,py312-extra,py313-extra |
| 14 | +export PYTEST ?= $(PYTHON) -m pytest |
| 15 | +export PYTEST_OPTS ?= # -vv --capture=no |
| 16 | + |
| 17 | +VERSION = $(shell $(PYTHON) -c "exec(open('tabulate/version.py').read()); print('.'.join(map(str, __version_tuple__[:-2])))" ) |
| 18 | +VERSION_FULL = $(shell $(PYTHON) -c "exec(open('tabulate/version.py').read()); print(__version__)" ) |
| 19 | +WHEEL = dist/tabulate_slip39-$(VERSION_FULL)-py3-none-any.whl |
| 20 | +VENV = $(CURDIR)-$(VERSION)-$(PYTHON_V) |
| 21 | + |
| 22 | +# Force export of variables that might be set from command line |
| 23 | +export VENV_OPTS ?= |
| 24 | +export NIX_OPTS ?= |
| 25 | + |
| 26 | +# Put it first so that "make" without argument is like "make help". |
| 27 | +help: |
| 28 | + @echo "Build and test tabulate under Nix and Python venv" |
| 29 | + @echo |
| 30 | + @echo " nix-... Make a target in the Nix Flake develop environment" |
| 31 | + @echo " venv Create and start a Python venv using the available Python interpreter" |
| 32 | + @echo " venv-... Make a target using the venv environment" |
| 33 | + @echo |
| 34 | + @echo "For example, to run tox tests in a Nix-supplied Python venv:" |
| 35 | + @echo |
| 36 | + @echo " make nix-venv-test" |
| 37 | + @echo |
| 38 | + |
| 39 | +.PHONY: help wheel install test bench analyze types venv Makefile FORCE |
| 40 | + |
| 41 | +wheel: $(WHEEL) |
| 42 | + |
| 43 | +$(WHEEL): FORCE |
| 44 | + $(PYTHON) -m build |
| 45 | + @ls -last dist |
| 46 | + |
| 47 | +# Install from wheel, including all optional extra dependencies (doesn't include dev) |
| 48 | +install: $(WHEEL) FORCE |
| 49 | + $(PYTHON) -m pip install --force-reinstall $< |
| 50 | + |
| 51 | +# Install from requirements/*; eg. install-dev, always getting the latest version |
| 52 | +install-%: FORCE |
| 53 | + $(PYTHON) -m pip install --upgrade -r requirements/$*.txt |
| 54 | + |
| 55 | + |
| 56 | +unit-%: |
| 57 | + $(PYTEST) $(PYTEST_OPTS) -k $* |
| 58 | + |
| 59 | +test: |
| 60 | + $(TOX) $(TOX_OPTS) |
| 61 | + |
| 62 | +bench: |
| 63 | + $(PYTHON) benchmark/benchmark.py |
| 64 | + |
| 65 | +analyze: |
| 66 | + $(PYTHON) -m flake8 --color never -j 1 --max-line-length=250 \ |
| 67 | + --ignore=W503,W504,E201,E202,E223,E226 \ |
| 68 | + tabulate |
| 69 | + |
| 70 | +types: |
| 71 | + mypy . |
| 72 | + |
| 73 | +# |
| 74 | +# Nix and VirtualEnv build, install and activate |
| 75 | +# |
| 76 | +# Create, start and run commands in "interactive" shell with a python venv's activate init-file. |
| 77 | +# Doesn't allow recursive creation of a venv with a venv-supplied python. Alters the bin/activate |
| 78 | +# to include the user's .bashrc (eg. Git prompts, aliases, ...). Use to run Makefile targets in a |
| 79 | +# proper context, for example to obtain a Nix environment containing the proper Python version, |
| 80 | +# create a python venv with the current Python environment. |
| 81 | +# |
| 82 | +# make nix-venv-build |
| 83 | +# |
| 84 | +nix-%: |
| 85 | + @if [ -r flake.nix ]; then \ |
| 86 | + nix develop $(NIX_OPTS) --command make $*; \ |
| 87 | + else \ |
| 88 | + nix-shell $(NIX_OPTS) --run "make $*"; \ |
| 89 | + fi |
| 90 | + |
| 91 | +venv-%: $(VENV) |
| 92 | + @echo; echo "*** Running in $< VirtualEnv: make $*" |
| 93 | + @bash --init-file $</bin/activate -ic "make $*" |
| 94 | + |
| 95 | +venv: $(VENV) |
| 96 | + @echo; echo "*** Activating $< VirtualEnv for Interactive $(SHELL)" |
| 97 | + @bash --init-file $</bin/activate -i |
| 98 | + |
| 99 | +$(VENV): |
| 100 | + @[[ "$(PYTHON_V)" =~ "^venv" ]] && ( echo -e "\n\n!!! $@ Cannot start a venv within a venv"; false ) || true |
| 101 | + @echo; echo "*** Building $@ VirtualEnv..." |
| 102 | + @rm -rf $@ && $(PYTHON) -m venv $(VENV_OPTS) $@ && sed -i -e '1s:^:. $$HOME/.bashrc\n:' $@/bin/activate \ |
| 103 | + && source $@/bin/activate \ |
| 104 | + && make install-dev install |
| 105 | + |
| 106 | +print-%: |
| 107 | + @echo $* = $($*) |
| 108 | + @echo $*\'s origin is $(origin $*) |
| 109 | + |
0 commit comments