1# Wrapper around wrapPythonProgramsIn, below. The $pythonPath 2# variable is passed in from the buildPythonPackage function. 3wrapPythonPrograms() { 4 wrapPythonProgramsIn $out "$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 for f in $(find "$dir" -type f -perm -0100); do 51 # Rewrite "#! .../env python" to "#! /nix/store/.../python". 52 # Strip suffix, like "3" or "2.7m" -- we don't have any choice on which 53 # Python to use besides one with this hook anyway. 54 if head -n1 "$f" | grep -q '#!.*/env.*\(python\|pypy\)'; then 55 sed -i "$f" -e "1 s^.*/env[ ]*\(python\|pypy\)[^ ]*^#! @executable@^" 56 fi 57 58 # catch /python and /.python-wrapped 59 if head -n1 "$f" | grep -q '/\.\?\(python\|pypy\)'; then 60 # dont wrap EGG-INFO scripts since they are called from python 61 if echo "$f" | grep -qv EGG-INFO/scripts; then 62 echo "wrapping \`$f'..." 63 patchPythonScript "$f" 64 # wrapProgram creates the executable shell script described 65 # above. The script will set PYTHONPATH and PATH variables.! 66 # (see pkgs/build-support/setup-hooks/make-wrapper.sh) 67 local -a wrap_args=("$f" 68 --prefix PATH ':' "$program_PATH") 69 70 # Add any additional arguments provided by makeWrapperArgs 71 # argument to buildPythonPackage. 72 local -a user_args="($makeWrapperArgs)" 73 local -a wrapProgramArgs=("${wrap_args[@]}" "${user_args[@]}") 74 wrapProgram "${wrapProgramArgs[@]}" 75 fi 76 fi 77 done 78} 79 80# Adds the lib and bin directories to the PYTHONPATH and PATH variables, 81# respectively. Recurses on any paths declared in 82# `propagated-native-build-inputs`, while avoiding duplicating paths by 83# flagging the directories it has visited in `pythonPathsSeen`. 84_addToPythonPath() { 85 local dir="$1" 86 # Stop if we've already visited here. 87 if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi 88 pythonPathsSeen[$dir]=1 89 # addToSearchPath is defined in stdenv/generic/setup.sh. It will have 90 # the effect of calling `export program_X=$dir/...:$program_X`. 91 addToSearchPath program_PYTHONPATH $dir/lib/@libPrefix@/site-packages 92 addToSearchPath program_PATH $dir/bin 93 94 # Inspect the propagated inputs (if they exist) and recur on them. 95 local prop="$dir/nix-support/propagated-native-build-inputs" 96 if [ -e $prop ]; then 97 local new_path 98 for new_path in $(cat $prop); do 99 _addToPythonPath $new_path 100 done 101 fi 102} 103 104createBuildInputsPth() { 105 local category="$1" 106 local inputs="$2" 107 if [ foo"$inputs" != foo ]; then 108 for x in $inputs; do 109 if $(echo -n $x |grep -q python-recursive-pth-loader); then 110 continue 111 fi 112 if test -d "$x"/lib/@libPrefix@/site-packages; then 113 echo $x/lib/@libPrefix@/site-packages \ 114 >> "$out"/lib/@libPrefix@/site-packages/${name}-nix-python-$category.pth 115 fi 116 done 117 fi 118}