-
Notifications
You must be signed in to change notification settings - Fork 0
153 lines (134 loc) · 5.23 KB
/
python.yml
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
on:
workflow_call:
inputs:
style-branch:
type: string
default: master
continue-on-error:
type: boolean
default: false
run-typecheck:
type: boolean
default: true
files:
type: string
flake8_extend_ignore: # list of comma separated flake8 error codes
type: string
outputs:
passed-lint:
value: ${{ jobs.lint.outputs.passed }}
passed-typecheck:
value: ${{ jobs.typecheck.outputs.passed }}
jobs:
lint:
runs-on: ubuntu-latest
outputs:
passed: ${{ steps.check.outputs.passed }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Remove ignored paths
run: find . -name .lint-ignore | xargs dirname | xargs rm -rf
- uses: actions/setup-python@v5
with:
python-version: '3.10'
architecture: x64
- name: Install flake8 and plugins
run: pip install --disable-pip-version-check flake8-black==0.3.5 flake8-isort==5.0.3
- name: Install flake8-annotations
if: inputs.run-typecheck
run: pip install --disable-pip-version-check flake8-annotations==2.9.1
- name: Run flake8
id: check
continue-on-error: ${{ inputs.continue-on-error }}
run: |
curl -LOJs https://raw.githubusercontent.com/seqsense/ros_style/${{ inputs.style-branch }}/.flake8
curl -LOJs https://raw.githubusercontent.com/seqsense/ros_style/${{ inputs.style-branch }}/pyproject.toml
extend_ignore="${{ inputs.flake8_extend_ignore }}"
if [ -n "${extend_ignore}" ]; then
sed -i "/^ignore/ s/$/, ${extend_ignore}/" .flake8
fi
cmd="flake8 --count --show-source --statistics"
files_to_check="${{ inputs.files }}"
if [ -z "${files_to_check}" ]; then
files_to_check=$(find . -type f -not -path '*/\.git/*' -not -path '*/__pycache__/*' -not -path '*/\.mypy_cache/*')
fi
exit_code=0
for file in ${files_to_check}; do
filetype=$(file -b --mime-type ${file})
if echo ${filetype} | grep -qE "text/x-python|text/x-script.python"; then
echo "checking ${file}"
${cmd} ${file} \
| sed 's|^\(\S\+\):\([0-9]\+\):\([0-9]\+\): \(.*\)$|::error file=\1,line=\2,col=\3::\4\n\0|'
exit_code=$(($exit_code | ${PIPESTATUS[0]}))
fi
done
if [ ${exit_code} -eq 0 ]; then
echo "passed=true" >> ${GITHUB_OUTPUT}
else
echo "passed=false" >> ${GITHUB_OUTPUT}
fi
exit $exit_code
typecheck:
runs-on: ubuntu-latest
if: inputs.run-typecheck
outputs:
passed: ${{ inputs.run-typecheck == 'false' || steps.check.outputs.passed }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Remove ignored paths
run: find . -name .lint-ignore | xargs dirname | xargs rm -rf
- uses: actions/setup-python@v5
with:
python-version: '3.10'
architecture: x64
- name: Install mypy
run: pip install --disable-pip-version-check mypy==0.981
- name: Run mypy
id: check
continue-on-error: ${{ inputs.continue-on-error }}
run: |
curl -LOJs https://raw.githubusercontent.com/seqsense/ros_style/${{ inputs.style-branch }}/mypy.ini
cmd="mypy --config-file mypy.ini --scripts-are-modules"
files_to_check="${{ inputs.files }}"
if [ -z "${files_to_check}" ]; then
files_to_check=$(find . -type f -not -path '*/\.git/*' -not -path '*/__pycache__/*' -not -path '*/\.mypy_cache/*')
fi
python_files=()
for file in ${files_to_check}; do
filetype=$(file -b --mime-type ${file})
if echo ${filetype} | grep -qE "text/x-python|text/x-script.python"; then
python_files+=(${file})
fi
done
echo "found ${#python_files[@]} python file(s)"
if [ ${#python_files[@]} -eq 0 ]; then
echo "passed=true" >> ${GITHUB_OUTPUT}
exit 0
fi
echo "checking ${python_files[@]}"
# install explicitly listed type stub packages
if [ -f ".mypy-stub-pkgs" ]; then
pip install -r .mypy-stub-pkgs
fi
# workaround to get mypy to figure out the types it should install
# note that it does not upgrade already installed packages
$(echo "${cmd} ${python_files[@]}") > /dev/null 2> /dev/null || true
mypy --install-types --non-interactive
output="$(${cmd} ${python_files[@]} | tee /dev/stderr)"
result="$(echo "${output}" | tail -n 1)"
echo "${output}" | sed -n 's|^\(\S\+\):\([0-9]\+\):\([0-9]\+\): error: \(.*\)$|::error file=\1,line=\2,col=\3::\4|p'
if [[ -n "$result" ]]; then
error="$(echo $result | awk '/^Found/{print $2}')"
if [[ -z "$error" ]]; then
echo "passed=true" >> ${GITHUB_OUTPUT}
exit 0
fi
fi
echo "passed=false" >> ${GITHUB_OUTPUT}
exit 1