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