wrapPythonProgram: use site.addsitedir instead of PYTHONPATH

This commit is contained in:
Nikolay Amiantov 2016-08-15 12:35:32 +03:00
parent edb9416228
commit 01624e1ac2
3 changed files with 19 additions and 4 deletions

View File

@ -503,9 +503,12 @@ and can be used as:
The `buildPythonPackage` mainly does four things: The `buildPythonPackage` mainly does four things:
* In the `buildPhase`, it calls `${python.interpreter} setup.py bdist_wheel` to build a wheel binary zipfile. * In the `buildPhase`, it calls `${python.interpreter} setup.py bdist_wheel` to
build a wheel binary zipfile.
* In the `installPhase`, it installs the wheel file using `pip install *.whl`. * In the `installPhase`, it installs the wheel file using `pip install *.whl`.
* In the `postFixup` phase, the `wrapPythonPrograms` bash function is called to wrap all programs in the `$out/bin/*` directory to include `$PYTHONPATH` and `$PATH` environment variables. * In the `postFixup` phase, the `wrapPythonPrograms` bash function is called to
wrap all programs in the `$out/bin/*` directory to include `$PATH`
environment variable and add dependent libraries to script's `sys.path`.
* In the `installCheck` phase, `${python.interpreter} setup.py test` is ran. * In the `installCheck` phase, `${python.interpreter} setup.py test` is ran.
As in Perl, dependencies on other Python packages can be specified in the As in Perl, dependencies on other Python packages can be specified in the

View File

@ -42,12 +42,12 @@ wrapPythonProgramsIn() {
# The magicalSedExpression will invoke a "$(basename "$f")", so # The magicalSedExpression will invoke a "$(basename "$f")", so
# if you change $f to something else, be sure to also change it # if you change $f to something else, be sure to also change it
# in pkgs/top-level/python-packages.nix! # in pkgs/top-level/python-packages.nix!
# It also uses $program_PYTHONPATH.
sed -i "$f" -re '@magicalSedExpression@' sed -i "$f" -re '@magicalSedExpression@'
# wrapProgram creates the executable shell script described # wrapProgram creates the executable shell script described
# above. The script will set PYTHONPATH and PATH variables.! # above. The script will set PYTHONPATH and PATH variables.!
# (see pkgs/build-support/setup-hooks/make-wrapper.sh) # (see pkgs/build-support/setup-hooks/make-wrapper.sh)
local -a wrap_args=("$f" local -a wrap_args=("$f"
--prefix PYTHONPATH ':' "$program_PYTHONPATH"
--prefix PATH ':' "$program_PATH:$dir/bin") --prefix PATH ':' "$program_PATH:$dir/bin")
# Add any additional arguments provided by makeWrapperArgs # Add any additional arguments provided by makeWrapperArgs

View File

@ -59,13 +59,25 @@ in modules // {
} }
''; '';
# This preamble does two things:
# * Sets argv[0] to the original application's name; otherwise it would be .foo-wrapped.
# Python doesn't support `exec -a`.
# * Adds all required libraries to sys.path via `site.addsitedir`. It also handles *.pth files.
preamble = ''
import sys
import site
import functools
sys.argv[0] = '"'$(basename "$f")'"'
functools.reduce(lambda k, p: site.addsitedir(p, k), ['"$([ -n "$program_PYTHONPATH" ] && (echo "'$program_PYTHONPATH'" | sed "s|:|','|g") || true)"'], site._init_pathinfo())
'';
in '' in ''
1 { 1 {
/^#!/!b; :r /^#!/!b; :r
/\\$/{N;br} /\\$/{N;br}
/__future__|^ *(#.*)?$/{n;br} /__future__|^ *(#.*)?$/{n;br}
${concatImapStrings mkStringSkipper quoteVariants} ${concatImapStrings mkStringSkipper quoteVariants}
/^ *[^# ]/i import sys; sys.argv[0] = '"'$(basename "$f")'"' /^ *[^# ]/i ${replaceStrings ["\n"] [";"] preamble}
} }
''; '';
} }