2015-05-24 09:55:24 +00:00
|
|
|
# Wrapper around wrapPythonProgramsIn, below. The $pythonPath
|
|
|
|
# variable is passed in from the buildPythonPackage function.
|
2011-03-28 16:33:33 +00:00
|
|
|
wrapPythonPrograms() {
|
2017-05-24 06:43:07 +00:00
|
|
|
wrapPythonProgramsIn "$out/bin" "$out $pythonPath"
|
2011-03-28 16:33:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 23:51:55 +00:00
|
|
|
# Builds environment variables like PYTHONPATH and PATH walking through closure
|
|
|
|
# of dependencies.
|
|
|
|
buildPythonPath() {
|
|
|
|
local pythonPath="$1"
|
2015-05-24 09:55:24 +00:00
|
|
|
local path
|
2011-03-28 16:33:33 +00:00
|
|
|
|
2015-05-24 09:55:24 +00:00
|
|
|
# Create an empty table of python paths (see doc on _addToPythonPath
|
|
|
|
# for how this is used). Build up the program_PATH and program_PYTHONPATH
|
|
|
|
# variables.
|
2011-03-28 16:33:33 +00:00
|
|
|
declare -A pythonPathsSeen=()
|
|
|
|
program_PYTHONPATH=
|
|
|
|
program_PATH=
|
2019-01-02 19:09:44 +00:00
|
|
|
pythonPathsSeen["@pythonHost@"]=1
|
|
|
|
addToSearchPath program_PATH @pythonHost@/bin
|
2015-05-24 09:55:24 +00:00
|
|
|
for path in $pythonPath; do
|
|
|
|
_addToPythonPath $path
|
2011-03-28 16:33:33 +00:00
|
|
|
done
|
2016-08-16 23:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Patches a Python script so that it has correct libraries path and executable
|
|
|
|
# name.
|
|
|
|
patchPythonScript() {
|
|
|
|
local f="$1"
|
|
|
|
|
|
|
|
# The magicalSedExpression will invoke a "$(basename "$f")", so
|
|
|
|
# if you change $f to something else, be sure to also change it
|
|
|
|
# in pkgs/top-level/python-packages.nix!
|
|
|
|
# It also uses $program_PYTHONPATH.
|
|
|
|
sed -i "$f" -re '@magicalSedExpression@'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Transforms any binaries generated by the setup.py script, replacing them
|
|
|
|
# with an executable shell script which will set some environment variables
|
|
|
|
# and then call into the original binary (which has been given a .wrapped
|
|
|
|
# suffix).
|
|
|
|
wrapPythonProgramsIn() {
|
|
|
|
local dir="$1"
|
|
|
|
local pythonPath="$2"
|
|
|
|
local f
|
|
|
|
|
|
|
|
buildPythonPath "$pythonPath"
|
2011-03-28 16:33:33 +00:00
|
|
|
|
2015-05-24 09:55:24 +00:00
|
|
|
# Find all regular files in the output directory that are executable.
|
2017-05-24 06:43:07 +00:00
|
|
|
if [ -d "$dir" ]; then
|
|
|
|
find "$dir" -type f -perm -0100 -print0 | while read -d "" f; do
|
|
|
|
# Rewrite "#! .../env python" to "#! /nix/store/.../python".
|
|
|
|
# Strip suffix, like "3" or "2.7m" -- we don't have any choice on which
|
|
|
|
# Python to use besides one with this hook anyway.
|
|
|
|
if head -n1 "$f" | grep -q '#!.*/env.*\(python\|pypy\)'; then
|
2019-01-02 19:09:44 +00:00
|
|
|
sed -i "$f" -e "1 s^.*/env[ ]*\(python\|pypy\)[^ ]*^#!@executable@^"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if head -n1 "$f" | grep -q '#!.*'; then
|
|
|
|
# Cross-compilation hack: ensure shebangs are for the host
|
|
|
|
echo "Rewriting $(head -n 1 $f) to #!@pythonHost@"
|
|
|
|
sed -i "$f" -e "1 s^#!@python@^#!@pythonHost@^"
|
2017-05-24 06:43:07 +00:00
|
|
|
fi
|
2015-05-24 09:55:24 +00:00
|
|
|
|
2017-05-24 06:43:07 +00:00
|
|
|
# catch /python and /.python-wrapped
|
|
|
|
if head -n1 "$f" | grep -q '/\.\?\(python\|pypy\)'; then
|
|
|
|
# dont wrap EGG-INFO scripts since they are called from python
|
|
|
|
if echo "$f" | grep -qv EGG-INFO/scripts; then
|
|
|
|
echo "wrapping \`$f'..."
|
|
|
|
patchPythonScript "$f"
|
|
|
|
# wrapProgram creates the executable shell script described
|
|
|
|
# above. The script will set PYTHONPATH and PATH variables.!
|
|
|
|
# (see pkgs/build-support/setup-hooks/make-wrapper.sh)
|
|
|
|
local -a wrap_args=("$f"
|
2017-07-31 11:16:29 +00:00
|
|
|
--prefix PATH ':' "$program_PATH"
|
|
|
|
)
|
2015-05-22 04:32:03 +00:00
|
|
|
|
2018-12-06 23:18:59 +00:00
|
|
|
if [ -z "$permitUserSite" ]; then
|
2018-11-22 06:20:38 +00:00
|
|
|
wrap_args+=(--set PYTHONNOUSERSITE "true")
|
|
|
|
fi
|
|
|
|
|
2017-05-24 06:43:07 +00:00
|
|
|
# Add any additional arguments provided by makeWrapperArgs
|
|
|
|
# argument to buildPythonPackage.
|
2019-12-23 16:16:16 +00:00
|
|
|
# We need to support both the case when makeWrapperArgs
|
|
|
|
# is an array and a IFS-separated string.
|
|
|
|
# TODO: remove the string branch when __structuredAttrs are used.
|
2019-12-29 17:12:51 +00:00
|
|
|
if [[ "${makeWrapperArgs+defined}" == "defined" && "$(declare -p makeWrapperArgs)" =~ ^'declare -a makeWrapperArgs=' ]]; then
|
2019-12-28 21:10:28 +00:00
|
|
|
local -a user_args=("${makeWrapperArgs[@]}")
|
2019-12-23 16:16:16 +00:00
|
|
|
else
|
2019-12-29 17:12:51 +00:00
|
|
|
local -a user_args="(${makeWrapperArgs:-})"
|
2019-12-23 16:16:16 +00:00
|
|
|
fi
|
|
|
|
|
2017-05-24 06:43:07 +00:00
|
|
|
local -a wrapProgramArgs=("${wrap_args[@]}" "${user_args[@]}")
|
|
|
|
wrapProgram "${wrapProgramArgs[@]}"
|
|
|
|
fi
|
2013-07-29 17:31:58 +00:00
|
|
|
fi
|
2017-05-24 06:43:07 +00:00
|
|
|
done
|
|
|
|
fi
|
2011-03-28 16:33:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 09:55:24 +00:00
|
|
|
# Adds the lib and bin directories to the PYTHONPATH and PATH variables,
|
|
|
|
# respectively. Recurses on any paths declared in
|
2017-11-17 18:26:21 +00:00
|
|
|
# `propagated-build-inputs`, while avoiding duplicating paths by
|
2015-05-24 09:55:24 +00:00
|
|
|
# flagging the directories it has visited in `pythonPathsSeen`.
|
2011-03-28 16:33:33 +00:00
|
|
|
_addToPythonPath() {
|
|
|
|
local dir="$1"
|
2015-05-24 09:55:24 +00:00
|
|
|
# Stop if we've already visited here.
|
2011-03-28 16:33:33 +00:00
|
|
|
if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi
|
|
|
|
pythonPathsSeen[$dir]=1
|
2015-05-24 09:55:24 +00:00
|
|
|
# addToSearchPath is defined in stdenv/generic/setup.sh. It will have
|
|
|
|
# the effect of calling `export program_X=$dir/...:$program_X`.
|
2018-12-20 14:03:23 +00:00
|
|
|
addToSearchPath program_PYTHONPATH $dir/@sitePackages@
|
2011-03-28 16:33:33 +00:00
|
|
|
addToSearchPath program_PATH $dir/bin
|
2015-05-24 09:55:24 +00:00
|
|
|
|
|
|
|
# Inspect the propagated inputs (if they exist) and recur on them.
|
2017-11-17 18:26:21 +00:00
|
|
|
local prop="$dir/nix-support/propagated-build-inputs"
|
2011-03-28 16:33:33 +00:00
|
|
|
if [ -e $prop ]; then
|
2015-05-24 09:55:24 +00:00
|
|
|
local new_path
|
|
|
|
for new_path in $(cat $prop); do
|
|
|
|
_addToPythonPath $new_path
|
2011-03-28 16:33:33 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
2012-07-20 23:55:50 +00:00
|
|
|
|
|
|
|
createBuildInputsPth() {
|
|
|
|
local category="$1"
|
|
|
|
local inputs="$2"
|
|
|
|
if [ foo"$inputs" != foo ]; then
|
|
|
|
for x in $inputs; do
|
2013-01-11 17:03:32 +00:00
|
|
|
if $(echo -n $x |grep -q python-recursive-pth-loader); then
|
|
|
|
continue
|
|
|
|
fi
|
2018-12-20 14:03:23 +00:00
|
|
|
if test -d "$x"/@sitePackages@; then
|
|
|
|
echo $x/@sitePackages@ \
|
|
|
|
>> "$out"/@sitePackages@/${name}-nix-python-$category.pth
|
2012-07-20 23:55:50 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|