at master 5.5 kB view raw
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["@pythonHost@"]=1 20 addToSearchPath program_PATH @pythonHost@/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 if head -n1 "$f" | grep -q '#!.*'; then 60 # Cross-compilation hack: ensure shebangs are for the host 61 echo "Rewriting $(head -n 1 $f) to #!@pythonHost@" 62 sed -i "$f" -e "1 s^#!@python@^#!@pythonHost@^" 63 fi 64 65 # catch /python and /.python-wrapped 66 if head -n1 "$f" | grep -q '/\.\?\(python\|pypy\)'; then 67 # dont wrap EGG-INFO scripts since they are called from python 68 if echo "$f" | grep -qv EGG-INFO/scripts; then 69 echo "wrapping \`$f'..." 70 patchPythonScript "$f" 71 # wrapProgram creates the executable shell script described 72 # above. The script will set PYTHONPATH and PATH variables.! 73 # (see pkgs/build-support/setup-hooks/make-wrapper.sh) 74 local -a wrap_args=("$f" 75 --prefix PATH ':' "$program_PATH" 76 ) 77 78 if [ -z "$permitUserSite" ]; then 79 wrap_args+=(--set PYTHONNOUSERSITE "true") 80 fi 81 82 # Add any additional arguments provided by makeWrapperArgs 83 # argument to buildPythonPackage. 84 # We need to support both the case when makeWrapperArgs 85 # is an array and a IFS-separated string. 86 # TODO: remove the string branch when __structuredAttrs are used. 87 if [[ "${makeWrapperArgs+defined}" == "defined" && "$(declare -p makeWrapperArgs)" =~ ^'declare -a makeWrapperArgs=' ]]; then 88 local -a user_args=("${makeWrapperArgs[@]}") 89 else 90 local -a user_args="(${makeWrapperArgs:-})" 91 fi 92 93 local -a wrapProgramArgs=("${wrap_args[@]}" "${user_args[@]}") 94 wrapProgram "${wrapProgramArgs[@]}" 95 fi 96 fi 97 done 98 fi 99} 100 101# Adds the lib and bin directories to the PYTHONPATH and PATH variables, 102# respectively. Recurses on any paths declared in 103# `propagated-build-inputs`, while avoiding duplicating paths by 104# flagging the directories it has visited in `pythonPathsSeen`. 105_addToPythonPath() { 106 local dir="$1" 107 # Stop if we've already visited here. 108 if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi 109 pythonPathsSeen[$dir]=1 110 # addToSearchPath is defined in stdenv/generic/setup.sh. It will have 111 # the effect of calling `export program_X=$dir/...:$program_X`. 112 addToSearchPath program_PYTHONPATH $dir/@sitePackages@ 113 addToSearchPath program_PATH $dir/bin 114 115 # Inspect the propagated inputs (if they exist) and recur on them. 116 local prop="$dir/nix-support/propagated-build-inputs" 117 if [ -e $prop ]; then 118 local new_path 119 for new_path in $(cat $prop); do 120 _addToPythonPath $new_path 121 done 122 fi 123} 124 125createBuildInputsPth() { 126 local category="$1" 127 local inputs="$2" 128 if [ foo"$inputs" != foo ]; then 129 for x in $inputs; do 130 if $(echo -n $x |grep -q python-recursive-pth-loader); then 131 continue 132 fi 133 if test -d "$x"/@sitePackages@; then 134 echo $x/@sitePackages@ \ 135 >> "$out"/@sitePackages@/${name}-nix-python-$category.pth 136 fi 137 done 138 fi 139}