Skip to content

Commit 3194c38

Browse files
committed
Enable Nix-provision Python venv testing environment
1 parent 38167b8 commit 3194c38

File tree

5 files changed

+248
-0
lines changed

5 files changed

+248
-0
lines changed

Makefile

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+

flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
description = "Python HD Wallet development environment with multiple Python versions";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/25.05";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils }:
10+
flake-utils.lib.eachDefaultSystem (system:
11+
let
12+
pkgs = nixpkgs.legacyPackages.${system};
13+
14+
# Create Python environments with required packages
15+
mkPythonEnv = pythonPkg: pythonPkg.withPackages (ps: with ps; [
16+
pytest
17+
tox
18+
numpy
19+
pandas
20+
wcwidth
21+
]);
22+
23+
python310Env = mkPythonEnv pkgs.python310;
24+
python311Env = mkPythonEnv pkgs.python311;
25+
python312Env = mkPythonEnv pkgs.python312;
26+
python313Env = mkPythonEnv pkgs.python313;
27+
python314Env = mkPythonEnv pkgs.python314;
28+
29+
in {
30+
# Single development shell with all Python versions
31+
devShells.default = pkgs.mkShell {
32+
buildInputs = with pkgs; [
33+
# Common tools
34+
cacert
35+
git
36+
gnumake
37+
openssh
38+
bash
39+
bash-completion
40+
41+
# All Python versions with packages
42+
#python310Env
43+
python311Env
44+
python312Env
45+
python313Env
46+
#python314Env
47+
];
48+
49+
shellHook = ''
50+
echo "Welcome to the multi-Python development environment!"
51+
echo "Available Python interpreters:"
52+
echo " python (default): $(python --version 2>&1 || echo 'not available')"
53+
#echo " python3.10: $(python3.10 --version 2>&1 || echo 'not available')"
54+
echo " python3.11: $(python3.11 --version 2>&1 || echo 'not available')"
55+
echo " python3.12: $(python3.12 --version 2>&1 || echo 'not available')"
56+
echo " python3.13: $(python3.13 --version 2>&1 || echo 'not available')"
57+
#echo " python3.14: $(python3.14 --version 2>&1 || echo 'not available')"
58+
echo ""
59+
echo "All versions have pytest and tox installed."
60+
'';
61+
};
62+
});
63+
}

nixpkgs.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import (fetchTarball {
2+
url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/25.05.tar.gz";
3+
sha256 = "1915r28xc4znrh2vf4rrjnxldw2imysz819gzhk9qlrkqanmfsxd";
4+
})

requirements/dev.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
build
2+
pre-commit
3+
setuptools >=77.0.3
4+
setuptools_scm[toml] >=3.4.3
5+
wheel
6+
flake8
7+
pytest
8+
tox
9+
# for benchmark/benchmark.py
10+
prettytable
11+
texttable

0 commit comments

Comments
 (0)