summary refs log tree commit diff
path: root/pkgs/development/python-modules/generic/wrap.sh
blob: fa6a4d0102f36935479708c62b6e7d92618aac33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Wrapper around wrapPythonProgramsIn, below. The $pythonPath
# variable is passed in from the buildPythonPackage function.
wrapPythonPrograms() {
    wrapPythonProgramsIn $out "$out $pythonPath"
}

# Transforms any binaries generated by the setup.py script, replacing them
# with an executable shell script which will set some environment variables
# and then call into the original binary (which has been given a .wrapped
# suffix).
wrapPythonProgramsIn() {
    local dir="$1"
    local pythonPath="$2"
    local python="@executable@"
    local path
    local f

    # Create an empty table of python paths (see doc on _addToPythonPath
    # for how this is used). Build up the program_PATH and program_PYTHONPATH
    # variables.
    declare -A pythonPathsSeen=()
    program_PYTHONPATH=
    program_PATH=
    for path in $pythonPath; do
        _addToPythonPath $path
    done

    # Find all regular files in the output directory that are executable.
    for f in $(find "$dir" -type f -perm -0100); do
        # Rewrite "#! .../env python" to "#! /nix/store/.../python".
        if head -n1 "$f" | grep -q '#!.*/env.*\(python\|pypy\)'; then
            sed -i "$f" -e "1 s^.*/env[ ]*\(python\|pypy\)^#! $python^"
        fi

        # catch /python and /.python-wrapped
        if head -n1 "$f" | grep -q '/\.\?\(python\|pypy\)'; then
            # dont wrap EGG-INFO scripts since they are called from python
            if echo "$f" | grep -qv EGG-INFO/scripts; then
                echo "wrapping \`$f'..."
                # The magicalSedExpression will invoke a "$(basename "$f")", so
                # if you change $f to something else, be sure to also change it
                # in pkgs/top-level/python-packages.nix!
                sed -i "$f" -re '@magicalSedExpression@'
                # wrapProgram creates the executable shell script described
                # above. The script will set PYTHONPATH and PATH variables.!
                # (see pkgs/build-support/setup-hooks/make-wrapper.sh)
                local wrap_args="$f \
                                 --prefix PYTHONPATH ':' $program_PYTHONPATH \
                                 --prefix PATH ':' $program_PATH:$dir/bin"

                # Add any additional arguments provided by makeWrapperArgs
                # argument to buildPythonPackage.
                for arg in $makeWrapperArgs; do
                    wrap_args="$wrap_args $arg"
                done
                wrapProgram $wrap_args
            fi
        fi
    done
}

# Adds the lib and bin directories to the PYTHONPATH and PATH variables,
# respectively. Recurses on any paths declared in
# `propagated-native-build-inputs`, while avoiding duplicating paths by
# flagging the directories it has visited in `pythonPathsSeen`.
_addToPythonPath() {
    local dir="$1"
    # Stop if we've already visited here.
    if [ -n "${pythonPathsSeen[$dir]}" ]; then return; fi
    pythonPathsSeen[$dir]=1
    # addToSearchPath is defined in stdenv/generic/setup.sh. It will have
    # the effect of calling `export program_X=$dir/...:$program_X`.
    addToSearchPath program_PYTHONPATH $dir/lib/@libPrefix@/site-packages
    addToSearchPath program_PATH $dir/bin

    # Inspect the propagated inputs (if they exist) and recur on them.
    local prop="$dir/nix-support/propagated-native-build-inputs"
    if [ -e $prop ]; then
        local new_path
        for new_path in $(cat $prop); do
            _addToPythonPath $new_path
        done
    fi
}

createBuildInputsPth() {
    local category="$1"
    local inputs="$2"
    if [ foo"$inputs" != foo ]; then
        for x in $inputs; do
            if $(echo -n $x |grep -q python-recursive-pth-loader); then
                continue
            fi
            if test -d "$x"/lib/@libPrefix@/site-packages; then
                echo $x/lib/@libPrefix@/site-packages \
                    >> "$out"/lib/@libPrefix@/site-packages/${name}-nix-python-$category.pth
            fi
        done
    fi
}