1# Wrapper around wrapPythonProgramsIn, below. The $pythonPath 2# variable is passed in from the buildPythonPackage function. 3wrapPythonPrograms() { 4 wrapPythonProgramsIn "$out/bin" "$out $pythonPath" 5} 6 7# Builds environment variables like PYTHONPATH and PATH walking through closure 8# of dependencies. 9buildPythonPath() { 10 local pythonPath="$1" 11 local path 12 13 # Create an empty table of python paths (see doc on _addToPythonPath 14 # for how this is used). Build up the program_PATH and program_PYTHONPATH 15 # variables. 16 declare -A pythonPathsSeen=() 17 program_PYTHONPATH= 18 program_PATH= 19 pythonPathsSeen["@python@"]=1 20 addToSearchPath program_PATH @python@/bin 21 for path in $pythonPath; do 22 _addToPythonPath $path 23 done 24} 25 26# Patches a Python script so that it has correct libraries path and executable 27# name. 28patchPythonScript() { 29 local f="$1" 30 31 # The magicalSedExpression will invoke a "$(basename "$f")", so 32 # if you change $f to something else, be sure to also change it 33 # in pkgs/top-level/python-packages.nix! 34 # It also uses $program_PYTHONPATH. 35 sed -i "$f" -re '@magicalSedExpression@' 36} 37 38# Transforms any binaries generated by the setup.py script, replacing them 39# with an executable shell script which will set some environment variables 40# and then call into the original binary (which has been given a .wrapped 41# suffix). 42wrapPythonProgramsIn() { 43 local dir="$1" 44 local pythonPath="$2" 45 local f 46 47 buildPythonPath "$pythonPath" 48 49 # Find all regular files in the output directory that are executable. 50 if [ -d "$dir" ]; then 51 find "$dir" -type f -perm -0100 -print0 | while read -d "" f; do 52 # Rewrite "#! .../env python" to "#! /nix/store/.../python". 53 # Strip suffix, like "3" or "2.7m" -- we don't have any choice on which 54 # Python to use besides one with this hook anyway. 55 if head -n1 "$f" | grep -q '#!.*/env.*\(python\|pypy\)'; then 56 sed -i "$f" -e "1 s^.*/env[ ]*\(python\|pypy\)[^ ]*^#! @executable@^" 57 fi 58 59 # catch /python and /.python-wrapped 60 if head -n1 "$f" | grep -q '/\.\?\(python\|pypy\)'; then 61 # dont wrap EGG-INFO scripts since they are called from python 62 if echo "$f" | grep -qv EGG-INFO/scripts; then 63 echo "wrapping \`$f'..." 64 patchPythonScript "$f" 65 # wrapProgram creates the executable shell script described 66 # above. The script will set PYTHONPATH and PATH variables.! 67 # (see pkgs/build-support/setup-hooks/make-wrapper.sh) 68 local -a wrap_args=("$f" 69 --prefix PATH ':' "$program_PATH" 70 --set PYTHONNOUSERSITE "true" 71 ) 72 73 # Add any additional arguments provided by makeWrapperArgs 74 # argument to buildPythonPackage. 75 local -a user_args="($makeWrapperArgs)" 76 local -a wrapProgramArgs=("${wrap_args[@]}" "${user_args[@]}") 77 wrapProgram "${wrapProgramArgs[@]}" 78 fi 79 fi 80 done 81 fi 82} 83 84# Adds the lib and bin directories to the PYTHONPATH and PATH variables, 85# respectively. Recurses on any paths declared in 86# `propagated-build-inputs`, while avoiding duplicating paths by 87# flagging the directories it has visited in `pythonPathsSeen`. 88_addToPythonPath() { 89 local dir="$1" 90 # Stop if we've already visited here. 91 if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi 92 pythonPathsSeen[$dir]=1 93 # addToSearchPath is defined in stdenv/generic/setup.sh. It will have 94 # the effect of calling `export program_X=$dir/...:$program_X`. 95 addToSearchPath program_PYTHONPATH $dir/lib/@libPrefix@/site-packages 96 addToSearchPath program_PATH $dir/bin 97 98 # Inspect the propagated inputs (if they exist) and recur on them. 99 local prop="$dir/nix-support/propagated-build-inputs" 100 if [ -e $prop ]; then 101 local new_path 102 for new_path in $(cat $prop); do 103 _addToPythonPath $new_path 104 done 105 fi 106} 107 108createBuildInputsPth() { 109 local category="$1" 110 local inputs="$2" 111 if [ foo"$inputs" != foo ]; then 112 for x in $inputs; do 113 if $(echo -n $x |grep -q python-recursive-pth-loader); then 114 continue 115 fi 116 if test -d "$x"/lib/@libPrefix@/site-packages; then 117 echo $x/lib/@libPrefix@/site-packages \ 118 >> "$out"/lib/@libPrefix@/site-packages/${name}-nix-python-$category.pth 119 fi 120 done 121 fi 122}