summaryrefslogtreecommitdiff
path: root/share/spack/qa/run-flake8
blob: c59bfc9490a0eb29e63fc385b83cdc6eb845b0ca (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
#!/bin/bash
#
# This script runs source code style checks on Spack.
#
# It should be executed from the top-level directory of the repo,
# e.g.:
#
#    share/spack/qa/run-flake8
#
# To run it, you'll need to have the Python flake8 installed locally.
#
PYTHONPATH=./lib/spack:$PYTHONPATH

flake8="$(which flake8)"
if [[ ! $flake8 ]]; then
    echo "ERROR: flake8 is required to run this script."
    exit 1
fi

# Check if changed files are flake8 conformant [framework]
changed=$(git diff --name-only --find-renames develop... | grep '.py$')

# 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

return_code=0
if [[ $changed ]]; then
    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."
        return_code=1
    fi
else
    echo No core framework files modified.
fi

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

exit $return_code