1# This function provides specific bits for building a setuptools-based Python package.
2
3{ lib
4, python
5}:
6
7{
8# passed to "python setup.py build_ext"
9# https://github.com/pypa/pip/issues/881
10 setupPyBuildFlags ? []
11# Execute before shell hook
12, preShellHook ? ""
13# Execute after shell hook
14, postShellHook ? ""
15, ... } @ attrs:
16
17let
18 # use setuptools shim (so that setuptools is imported before distutils)
19 # pip does the same thing: https://github.com/pypa/pip/pull/3265
20 setuppy = ./run_setup.py;
21
22in attrs // {
23 # we copy nix_run_setup over so it's executed relative to the root of the source
24 # many project make that assumption
25 buildPhase = attrs.buildPhase or ''
26 runHook preBuild
27 cp ${setuppy} nix_run_setup
28 ${python.pythonForBuild.interpreter} nix_run_setup ${lib.optionalString (setupPyBuildFlags != []) ("build_ext " + (lib.concatStringsSep " " setupPyBuildFlags))} bdist_wheel
29 runHook postBuild
30 '';
31
32 installCheckPhase = attrs.checkPhase or ''
33 runHook preCheck
34 ${python.pythonForBuild.interpreter} nix_run_setup test
35 runHook postCheck
36 '';
37
38 # Python packages that are installed with setuptools
39 # are typically distributed with tests.
40 # With Python it's a common idiom to run the tests
41 # after the software has been installed.
42 doCheck = attrs.doCheck or true;
43
44 shellHook = attrs.shellHook or ''
45 ${preShellHook}
46 if test -e setup.py; then
47 tmp_path=$(mktemp -d)
48 export PATH="$tmp_path/bin:$PATH"
49 export PYTHONPATH="$tmp_path/${python.pythonForBuild.sitePackages}:$PYTHONPATH"
50 mkdir -p $tmp_path/${python.pythonForBuild.sitePackages}
51 ${python.pythonForBuild.pkgs.bootstrapped-pip}/bin/pip install -e . --prefix $tmp_path >&2
52 fi
53 ${postShellHook}
54 '';
55}