Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at release-19.03 128 lines 5.0 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 --set PYTHONNOUSERSITE "true" 77 ) 78 79 # Add any additional arguments provided by makeWrapperArgs 80 # argument to buildPythonPackage. 81 local -a user_args="($makeWrapperArgs)" 82 local -a wrapProgramArgs=("${wrap_args[@]}" "${user_args[@]}") 83 wrapProgram "${wrapProgramArgs[@]}" 84 fi 85 fi 86 done 87 fi 88} 89 90# Adds the lib and bin directories to the PYTHONPATH and PATH variables, 91# respectively. Recurses on any paths declared in 92# `propagated-build-inputs`, while avoiding duplicating paths by 93# flagging the directories it has visited in `pythonPathsSeen`. 94_addToPythonPath() { 95 local dir="$1" 96 # Stop if we've already visited here. 97 if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi 98 pythonPathsSeen[$dir]=1 99 # addToSearchPath is defined in stdenv/generic/setup.sh. It will have 100 # the effect of calling `export program_X=$dir/...:$program_X`. 101 addToSearchPath program_PYTHONPATH $dir/@sitePackages@ 102 addToSearchPath program_PATH $dir/bin 103 104 # Inspect the propagated inputs (if they exist) and recur on them. 105 local prop="$dir/nix-support/propagated-build-inputs" 106 if [ -e $prop ]; then 107 local new_path 108 for new_path in $(cat $prop); do 109 _addToPythonPath $new_path 110 done 111 fi 112} 113 114createBuildInputsPth() { 115 local category="$1" 116 local inputs="$2" 117 if [ foo"$inputs" != foo ]; then 118 for x in $inputs; do 119 if $(echo -n $x |grep -q python-recursive-pth-loader); then 120 continue 121 fi 122 if test -d "$x"/@sitePackages@; then 123 echo $x/@sitePackages@ \ 124 >> "$out"/@sitePackages@/${name}-nix-python-$category.pth 125 fi 126 done 127 fi 128}