summaryrefslogtreecommitdiff
path: root/share/spack/qa/setup.sh
blob: 13e8e25b157846a62bc334e9950c866d90f7de4b (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash -e
#
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

#
# Description:
#     Common setup code to be sourced by Spack's test scripts.
#

QA_DIR="$(dirname ${BASH_SOURCE[0]})"
export SPACK_ROOT=$(realpath "$QA_DIR/../../..")

# Source the setup script
. "$SPACK_ROOT/share/spack/setup-env.sh"

# by default coverage is off.
coverage=""
coverage_run=""

# Set up some variables for running coverage tests.
if [[ "$COVERAGE" == "true" ]]; then
    # these set up coverage for Python
    coverage=coverage
    coverage_run="coverage run"

    # bash coverage depends on some other factors
    mkdir -p coverage
    bashcov=$(realpath ${QA_DIR}/bashcov)

    # instrument scripts requiring shell coverage
    sed -i "s@#\!/bin/bash@#\!${bashcov}@" "$SPACK_ROOT/lib/spack/env/cc"
    if [ "$(uname -o)" != "Darwin" ]; then
        # On darwin, #! interpreters must be binaries, so no sbang for bashcov
        sed -i "s@#\!/bin/sh@#\!${bashcov}@"   "$SPACK_ROOT/bin/sbang"
    fi
fi

#
# Description:
#     Check to see if dependencies are installed.
#     If not, warn the user and tell them how to
#     install these dependencies.
#
# Usage:
#     check-deps <dep> ...
#
# Options:
#     One or more dependencies. Must use name of binary.
check_dependencies() {
    for dep in "$@"; do
        if ! which $dep &> /dev/null; then
            # Map binary name to package name
            case $dep in
                sphinx-apidoc|sphinx-build)
                    spack_package=py-sphinx
                    pip_package=sphinx
                    ;;
                coverage)
                    spack_package=py-coverage
                    pip_package=coverage
                    ;;
                flake8)
                    spack_package=py-flake8
                    pip_package=flake8
                    ;;
                mypy)
                    spack_package=py-mypy
                    pip_package=mypy
                    ;;
                dot)
                    spack_package=graphviz
                    ;;
                git)
                    spack_package=git
                    ;;
                hg)
                    spack_package=mercurial
                    pip_package=mercurial
                    ;;
                kcov)
                    spack_package=kcov
                    ;;
                svn)
                    spack_package=subversion
                    ;;
                *)
                    spack_package=$dep
                    pip_package=$dep
                    ;;
            esac

            echo "ERROR: $dep is required to run this script."
            echo

            if [[ $spack_package ]]; then
                echo "To install with Spack, run:"
                echo "    $ spack install $spack_package"
            fi

            if [[ $pip_package ]]; then
                echo "To install with pip, run:"
                echo "    $ pip install $pip_package"
            fi

            if [[ $spack_package || $pip_package ]]; then
                echo "Then add the bin directory to your PATH."
            fi

            exit 1
        fi

        # Flake8 and Sphinx require setuptools in order to run.
        # Otherwise, they print out this error message:
        #
        #   Traceback (most recent call last):
        #     File: "/usr/bin/flake8", line 5, in <module>
        #       from pkg_resources import load_entry_point
        #   ImportError: No module named pkg_resources
        #
        # Print a more useful error message if setuptools not found.
        if [[ $dep == flake8 || $dep == sphinx* ]]; then
            # Find which Python is being run
            # Spack-installed packages have a hard-coded shebang
            python_cmd=$(head -n 1 $(which $dep) | cut -c 3-)
            # May not have a shebang
            if [[ $python_cmd != *python* ]]; then
                python_cmd=python
            fi
            # Check if setuptools is in the PYTHONPATH
            if ! $python_cmd -c "import setuptools" 2> /dev/null; then
                echo "ERROR: setuptools is required to run $dep."
                echo "Please add it to your PYTHONPATH."

                exit 1
            fi
        fi
    done
    echo "Dependencies found."
}