summaryrefslogtreecommitdiff
path: root/share/spack/qa/run-flake8-tests
blob: 350ef3161f662409b6a393f5f6a329f627ed768b (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
#!/usr/bin/env bash
#
# Description:
#     Runs source code style checks on Spack.
#     See $SPACK_ROOT/.flake8 for a list of
#     approved exceptions.
#
# Usage:
#     run-flake8-tests
#
# Notes:
#     Requires flake8.
#

QA_DIR="$(dirname "$0")"
SPACK_ROOT="$QA_DIR/../../.."

# Array of dependencies
deps=(
    flake8
)

# Check for dependencies
"$QA_DIR/check_dependencies" "${deps[@]}" || exit 1

# Move to root directory of Spack
# Allows script to be run from anywhere
cd "$SPACK_ROOT"

# Gather array of changed files
changed=($("$QA_DIR/changed_files" "*.py"))

# Exit if no Python files were modified
if [[ ! "${changed[@]}" ]]; then
    echo "No Python files were modified."
    exit 0
fi

function cleanup {
    # Restore original package files after modifying them.
    for file in "${changed[@]}"; do
        if [[ -e "${file}.sbak~" ]]; then
            mv "${file}.sbak~" "${file}"
        fi
    done
}

# Cleanup temporary files upon exit or when script is killed
trap cleanup EXIT SIGINT SIGTERM

# Add approved style exemptions to the changed packages.
for file in "${changed[@]}"; do
    # Make a backup to restore later
    cp "$file" "$file.sbak~"

    #
    # Exemptions for package.py files
    #
    if [[ $file = *package.py ]]; then
        # Exempt lines with urls and descriptions from overlong line errors.
        perl -i -pe 's/^(\s*homepage\s*=.*)$/\1  # NOQA: ignore=E501/' "$file"
        perl -i -pe 's/^(\s*url\s*=.*)$/\1  # NOQA: ignore=E501/' "$file"
        perl -i -pe 's/^(\s*version\(.*\).*)$/\1  # NOQA: ignore=E501/' "$file"
        perl -i -pe 's/^(\s*variant\(.*\).*)$/\1  # NOQA: ignore=E501/' "$file"
        perl -i -pe 's/^(\s*depends_on\(.*\).*)$/\1  # NOQA: ignore=E501/' "$file"
        perl -i -pe 's/^(\s*extends\(.*\).*)$/\1  # NOQA: ignore=E501/' "$file"

        # Exempt '@when' decorated functions from redefinition errors.
        perl -i -pe 's/^(\s*\@when\(.*\).*)$/\1  # NOQA: ignore=F811/' "$file"
    fi

    #
    # Exemptions for all files
    #
    perl -i -pe 's/^(.*(https?|file)\:.*)$/\1  # NOQA: ignore=E501/' $file
done

echo =======================================================
echo  flake8: running flake8 code checks on spack.
echo
echo  Modified files:
echo  "${changed[@]}" | perl -pe 's/^/  /;s/ +/\n  /g'
echo =======================================================
if flake8 --format pylint "${changed[@]}"; then
    echo "Flake8 checks were clean."
else
    echo "Flake8 found errors."
    exit 1
fi