File: ci

package info (click to toggle)
lookatme 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,416 kB
  • sloc: python: 3,497; sh: 291; makefile: 16
file content (196 lines) | stat: -rwxr-xr-x 4,574 bytes parent folder | download | duplicates (2)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
THIS_PROG="$0"

set -o pipefail

# load utility functions
. "${DIR}/_utils.sh"

# -----------------------------------------------------------------
# Main Functions --------------------------------------------------
# -----------------------------------------------------------------

SUBCOMMAND="all"
AUTOFIX=false
COLOR=false
RUN_PREFIX="run_boxed"
FORWARDED_OPTS=()

CODE_DIRS=(
    lookatme
    tests
)

function run_flake8 {
    if [ "$AUTOFIX" == "true" ] ; then
        log "No autofix for flake8"
    else
        local exit_code=0
        local color_opt=""
        [ "$COLOR" == true ] && color_opt="--color always"

        $RUN_PREFIX flake8 "${CODE_DIRS[@]}" $color_opt \
            --count \
            --select=E9,F63,F7,F82 \
            --show-source \
            --statistics
        [ $? -ne 0 ] && exit_code=1

        $RUN_PREFIX flake8 "${CODE_DIRS[@]}" $color_opt \
            --count \
            --exit-zero \
            --max-complexity=10 \
            --max-line-length=127 \
            --statistics
        [ $? -ne 0 ] && exit_code=1

        return $exit_code
    fi
}

function run_autopep8 {
    if [ "$AUTOFIX" == "true" ] ; then
        $RUN_PREFIX autopep8 -r --in-place "${CODE_DIRS[@]}"
    else
        log "autopep8 is only used with --autofix"
    fi
}

function run_pytest {
    if [ "$AUTOFIX" == "true" ] ; then
        log "🤣 LOL, no autofix for pytest!"
    else
        local color_opt=""
        [ "$COLOR" == true ] && color_opt="--color=yes"

        $RUN_PREFIX pytest $color_opt "$@"
    fi
}

function run_isort {
    if [ "$AUTOFIX" == "true" ] ; then
        $RUN_PREFIX isort "${CODE_DIRS[@]}"
    else
        $RUN_PREFIX isort --check --diff "${CODE_DIRS[@]}"
    fi
}

function run_pyright {
    if [ "$AUTOFIX" == "true" ] ; then
        log "🤣 LOL, no autofix for pyright!"
    else
        if [ "$COLOR" == true ] ; then
            pyright "${CODE_DIRS[@]}"
        else
            $RUN_PREFIX pyright "${CODE_DIRS[@]}"
        fi
    fi
}

function run_all {
    check_deps --log \
        isort \
        flake8 \
        pytest \
        autopep8 \
        pyright
    if [ $? -ne 0 ] ; then
        log "Can't run all, did you forget to use a virtual environment?"
        exit 1
    fi

    run_with_summary \
        run_isort \
        run_flake8 \
        run_autopep8 \
        run_pyright \
        run_pytest
}

function show_help {
    cat <<-EOF
USAGE: ${THIS_PROG} [SUBCOMMAND] [--auto|--autofix] [--color] [-- fwd opts]

This is a basic way to run CI locally for lookatme. This is the same script
that lookatme's CI uses in GitHub actions. Running this script with zero
arguments (or the subcommand "all") will run all linting/analysis/tests that
CI runs.

SUBCOMMAND may be one of the below values. The default is "all":
       all - Run all commands
     isort - Run isort
    pytest - Run pytest tests
    flake8 - Run flake8
  autopep8 - Run autopep8

 --autofix  Run the command's autofix functionality
 --color    Run the command with color
 --plain    Do not box the output of the command, let it be printed plain
 --         Forward all remaining options to the subcommand
EOF
    exit 1
}

function parse_args {
    while [ $# -ne 0 ] ; do
        param="$1"
        shift

        case "$param" in
            --help|-h)
                show_help
                exit 1
                ;;
            all|pytest|flake8|test|isort|autopep8|pyright)
                SUBCOMMAND="$param"
                ;;
            --plain)
                RUN_PREFIX=""
                ;;
            --color)
                COLOR=true
                ;;
            --autofix|--auto)
                AUTOFIX=true
                ;;
            --)
                while [ $# -ne 0 ] ; do
                    FORWARDED_OPTS+=("$1")
                    shift
                done
                ;;
            *)
                echo "[!] Unrecognized parameter $param"
                echo
                show_help
                exit 1
                ;;
        esac
    done
}

parse_args "$@"

case "$SUBCOMMAND" in
    all)
        run_all
        ;;
    isort)
        run_isort "${FORWARDED_OPTS[@]}"
        ;;
    flake8)
        run_flake8 "${FORWARDED_OPTS[@]}"
        ;;
    autopep8)
        run_autopep8 "${FORWARDED_OPTS[@]}"
        ;;
    pyright)
        run_pyright "${FORWARDED_OPTS[@]}"
        ;;
    test|pytest)
        run_pytest "${FORWARDED_OPTS[@]}"
        ;;
esac