2009-05-24 22:02:59 +01:00
|
|
|
/* This function provides a generic Python package builder. It is
|
2014-01-06 22:24:05 +00:00
|
|
|
intended to work with packages that use `distutils/setuptools'
|
2009-05-24 22:02:59 +01:00
|
|
|
(http://pypi.python.org/pypi/setuptools/), which represents a large
|
|
|
|
number of Python packages nowadays. */
|
|
|
|
|
2016-02-01 11:31:17 +00:00
|
|
|
{ python, setuptools, unzip, wrapPython, lib, bootstrapped-pip
|
|
|
|
, ensureNewerSourcesHook }:
|
2009-05-24 22:02:59 +01:00
|
|
|
|
2014-01-06 22:24:05 +00:00
|
|
|
{ name
|
2009-05-24 22:02:59 +01:00
|
|
|
|
2014-01-11 10:44:24 +00:00
|
|
|
# by default prefix `name` e.g. "python3.3-${name}"
|
2014-01-06 22:24:05 +00:00
|
|
|
, namePrefix ? python.libPrefix + "-"
|
2010-07-28 14:05:04 +01:00
|
|
|
|
2014-01-06 22:24:05 +00:00
|
|
|
, buildInputs ? []
|
2012-12-03 04:20:50 +00:00
|
|
|
|
2014-01-11 10:44:24 +00:00
|
|
|
# propagate build dependencies so in case we have A -> B -> C,
|
2015-11-26 16:31:10 +00:00
|
|
|
# C can import package A propagated by B
|
2014-01-06 22:24:05 +00:00
|
|
|
, propagatedBuildInputs ? []
|
2013-01-17 13:19:14 +00:00
|
|
|
|
2015-11-26 16:31:10 +00:00
|
|
|
# passed to "python setup.py build_ext"
|
2015-11-17 10:38:26 +00:00
|
|
|
# https://github.com/pypa/pip/issues/881
|
2014-01-06 22:24:05 +00:00
|
|
|
, setupPyBuildFlags ? []
|
2009-05-24 22:02:59 +01:00
|
|
|
|
2015-11-17 10:38:26 +00:00
|
|
|
# DEPRECATED: use propagatedBuildInputs
|
2014-08-24 15:01:21 +01:00
|
|
|
, pythonPath ? []
|
|
|
|
|
|
|
|
# used to disable derivation, useful for specific python versions
|
|
|
|
, disabled ? false
|
2009-06-28 15:05:41 +01:00
|
|
|
|
2014-01-10 20:57:28 +00:00
|
|
|
, meta ? {}
|
2013-01-19 17:21:23 +00:00
|
|
|
|
2014-03-10 09:06:04 +00:00
|
|
|
# Execute before shell hook
|
|
|
|
, preShellHook ? ""
|
|
|
|
|
|
|
|
# Execute after shell hook
|
|
|
|
, postShellHook ? ""
|
|
|
|
|
2015-05-24 16:19:13 +01:00
|
|
|
# Additional arguments to pass to the makeWrapper function, which wraps
|
|
|
|
# generated binaries.
|
|
|
|
, makeWrapperArgs ? []
|
2015-05-22 05:32:03 +01:00
|
|
|
|
2016-02-16 14:45:15 +00:00
|
|
|
# Additional flags to pass to "pip install".
|
|
|
|
, installFlags ? []
|
|
|
|
|
2016-06-16 11:49:19 +01:00
|
|
|
# Raise an error if two packages are installed with the same name
|
|
|
|
, catchConflicts ? true
|
|
|
|
|
2016-04-26 16:57:13 +01:00
|
|
|
, format ? "setup"
|
|
|
|
|
2011-03-28 16:30:48 +01:00
|
|
|
, ... } @ attrs:
|
2009-05-24 22:02:59 +01:00
|
|
|
|
2014-08-24 15:01:21 +01:00
|
|
|
|
2014-01-06 22:24:05 +00:00
|
|
|
# Keep extra attributes from `attrs`, e.g., `patchPhase', etc.
|
2015-05-22 05:32:03 +01:00
|
|
|
if disabled
|
|
|
|
then throw "${name} not supported for interpreter ${python.executable}"
|
|
|
|
else
|
|
|
|
|
2015-11-17 10:38:26 +00:00
|
|
|
let
|
2015-11-26 16:31:10 +00:00
|
|
|
# use setuptools shim (so that setuptools is imported before distutils)
|
|
|
|
# pip does the same thing: https://github.com/pypa/pip/pull/3265
|
2015-11-18 10:44:37 +00:00
|
|
|
setuppy = ./run_setup.py;
|
2016-04-26 16:57:13 +01:00
|
|
|
|
|
|
|
formatspecific =
|
|
|
|
if format == "wheel" then
|
|
|
|
{
|
|
|
|
unpackPhase = ''
|
|
|
|
mkdir dist
|
|
|
|
cp $src dist/"''${src#*-}"
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Wheels are pre-compiled
|
|
|
|
buildPhase = attrs.buildPhase or ":";
|
|
|
|
installCheckPhase = attrs.checkPhase or ":";
|
|
|
|
|
|
|
|
# Wheels don't have any checks to run
|
|
|
|
doInstallCheck = attrs.doCheck or false;
|
|
|
|
}
|
|
|
|
else if format == "setup" then
|
|
|
|
{
|
|
|
|
# propagate python/setuptools to active setup-hook in nix-shell
|
|
|
|
propagatedBuildInputs =
|
|
|
|
propagatedBuildInputs ++ [ python setuptools ];
|
|
|
|
|
|
|
|
# we copy nix_run_setup.py over so it's executed relative to the root of the source
|
|
|
|
# many project make that assumption
|
|
|
|
buildPhase = attrs.buildPhase or ''
|
|
|
|
runHook preBuild
|
|
|
|
cp ${setuppy} nix_run_setup.py
|
|
|
|
${python.interpreter} nix_run_setup.py ${lib.optionalString (setupPyBuildFlags != []) ("build_ext " + (lib.concatStringsSep " " setupPyBuildFlags))} bdist_wheel
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
installCheckPhase = attrs.checkPhase or ''
|
|
|
|
runHook preCheck
|
|
|
|
${python.interpreter} nix_run_setup.py test
|
|
|
|
runHook postCheck
|
|
|
|
'';
|
|
|
|
|
2016-05-09 17:33:43 +01:00
|
|
|
# Python packages that are installed with setuptools
|
|
|
|
# are typically distributed with tests.
|
|
|
|
# With Python it's a common idiom to run the tests
|
|
|
|
# after the software has been installed.
|
|
|
|
|
2016-04-26 16:57:13 +01:00
|
|
|
# For backwards compatibility, let's use an alias
|
2016-05-09 17:33:43 +01:00
|
|
|
doInstallCheck = attrs.doCheck or true;
|
2016-04-26 16:57:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw "Unsupported format ${format}";
|
2015-11-17 10:38:26 +00:00
|
|
|
in
|
2015-11-26 16:31:10 +00:00
|
|
|
python.stdenv.mkDerivation (builtins.removeAttrs attrs ["disabled" "doCheck"] // {
|
2011-03-28 16:30:48 +01:00
|
|
|
name = namePrefix + name;
|
2009-05-24 22:02:59 +01:00
|
|
|
|
2015-11-17 10:38:26 +00:00
|
|
|
buildInputs = [ wrapPython bootstrapped-pip ] ++ buildInputs ++ pythonPath
|
2016-02-01 11:31:17 +00:00
|
|
|
++ [ (ensureNewerSourcesHook { year = "1980"; }) ]
|
2014-03-10 13:51:05 +00:00
|
|
|
++ (lib.optional (lib.hasSuffix "zip" attrs.src.name or "") unzip);
|
2009-05-24 22:02:59 +01:00
|
|
|
|
2015-11-27 10:44:34 +00:00
|
|
|
pythonPath = pythonPath;
|
|
|
|
|
2014-01-06 22:24:05 +00:00
|
|
|
configurePhase = attrs.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
|
2014-01-10 21:04:05 +00:00
|
|
|
# patch python interpreter to write null timestamps when compiling python files
|
2015-11-17 10:38:26 +00:00
|
|
|
# this way python doesn't try to update them when we freeze timestamps in nix store
|
2013-06-22 06:52:27 +01:00
|
|
|
export DETERMINISTIC_BUILD=1
|
2014-01-06 22:24:05 +00:00
|
|
|
|
|
|
|
runHook postConfigure
|
|
|
|
'';
|
|
|
|
|
2016-05-09 17:33:43 +01:00
|
|
|
# Python packages don't have a checkPhase, only an installCheckPhase
|
2016-04-26 16:57:13 +01:00
|
|
|
doCheck = false;
|
2009-05-24 22:02:59 +01:00
|
|
|
|
2014-01-06 22:24:05 +00:00
|
|
|
installPhase = attrs.installPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
2015-11-17 10:38:26 +00:00
|
|
|
mkdir -p "$out/${python.sitePackages}"
|
|
|
|
export PYTHONPATH="$out/${python.sitePackages}:$PYTHONPATH"
|
2012-11-23 14:54:55 +00:00
|
|
|
|
2015-11-17 10:38:26 +00:00
|
|
|
pushd dist
|
2016-02-16 14:45:15 +00:00
|
|
|
${bootstrapped-pip}/bin/pip install *.whl --no-index --prefix=$out --no-cache ${toString installFlags}
|
2015-11-17 10:38:26 +00:00
|
|
|
popd
|
2012-11-23 14:54:55 +00:00
|
|
|
|
2014-01-06 22:24:05 +00:00
|
|
|
runHook postInstall
|
2009-05-24 22:02:59 +01:00
|
|
|
'';
|
|
|
|
|
2014-05-21 13:46:37 +01:00
|
|
|
postFixup = attrs.postFixup or ''
|
2015-11-17 10:38:26 +00:00
|
|
|
wrapPythonPrograms
|
2016-06-16 11:49:19 +01:00
|
|
|
'' + lib.optionalString catchConflicts ''
|
2016-01-25 08:14:16 +00:00
|
|
|
# check if we have two packages with the same name in closure and fail
|
2015-11-18 10:46:21 +00:00
|
|
|
# this shouldn't happen, something went wrong with dependencies specs
|
2015-11-19 08:56:46 +00:00
|
|
|
${python.interpreter} ${./catch_conflicts.py}
|
2015-11-17 10:38:26 +00:00
|
|
|
'';
|
2014-01-06 22:24:05 +00:00
|
|
|
|
2014-03-10 09:06:04 +00:00
|
|
|
shellHook = attrs.shellHook or ''
|
2015-11-15 12:49:20 +00:00
|
|
|
${preShellHook}
|
2014-06-15 15:05:09 +01:00
|
|
|
if test -e setup.py; then
|
2015-11-15 12:49:20 +00:00
|
|
|
tmp_path=$(mktemp -d)
|
2014-09-19 13:23:45 +01:00
|
|
|
export PATH="$tmp_path/bin:$PATH"
|
2015-11-15 12:49:20 +00:00
|
|
|
export PYTHONPATH="$tmp_path/${python.sitePackages}:$PYTHONPATH"
|
2015-11-23 16:47:35 +00:00
|
|
|
mkdir -p $tmp_path/${python.sitePackages}
|
2015-11-21 21:17:04 +00:00
|
|
|
${bootstrapped-pip}/bin/pip install -e . --prefix $tmp_path
|
2014-06-15 15:05:09 +01:00
|
|
|
fi
|
2015-11-15 12:49:20 +00:00
|
|
|
${postShellHook}
|
2014-03-10 09:06:04 +00:00
|
|
|
'';
|
|
|
|
|
2014-01-22 09:15:26 +00:00
|
|
|
meta = with lib.maintainers; {
|
2014-01-06 22:24:05 +00:00
|
|
|
# default to python's platforms
|
|
|
|
platforms = python.meta.platforms;
|
2014-01-22 09:15:26 +00:00
|
|
|
} // meta // {
|
2014-01-06 22:24:05 +00:00
|
|
|
# add extra maintainer(s) to every package
|
2016-05-17 12:57:28 +01:00
|
|
|
maintainers = (meta.maintainers or []) ++ [ chaoflow domenkozar ];
|
2016-01-25 08:14:16 +00:00
|
|
|
# a marker for release utilities to discover python packages
|
2015-11-19 15:39:34 +00:00
|
|
|
isBuildPythonPackage = python.meta.platforms;
|
2014-01-06 22:24:05 +00:00
|
|
|
};
|
2016-04-26 16:57:13 +01:00
|
|
|
} // formatspecific)
|