-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpycharm-type-probes.sh
More file actions
executable file
·123 lines (110 loc) · 3.56 KB
/
pycharm-type-probes.sh
File metadata and controls
executable file
·123 lines (110 loc) · 3.56 KB
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
#!/usr/bin/env bash
set -Eeuo pipefail
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
repo_root="$(cd -- "${script_dir}/.." && pwd -P)"
inspect_sh="${PYCHARM_INSPECT_SH:-}"
if [[ -z "${inspect_sh}" ]]; then
toolbox_inspect="${HOME}/.local/share/JetBrains/Toolbox/apps/pycharm-professional/bin/inspect.sh"
if [[ -x "${toolbox_inspect}" ]]; then
inspect_sh="${toolbox_inspect}"
elif command -v inspect.sh >/dev/null 2>&1; then
inspect_sh="$(command -v inspect.sh)"
fi
fi
if [[ -z "${inspect_sh}" || ! -x "${inspect_sh}" ]]; then
printf 'PyCharm inspect.sh was not found. Set PYCHARM_INSPECT_SH to its absolute path.\n' >&2
exit 2
fi
tmp_root="$(mktemp -d "${TMPDIR:-/tmp}/alternative-pycharm-type-probes.XXXXXXXX")"
cleanup() {
case "${tmp_root}" in
"${TMPDIR:-/tmp}"/alternative-pycharm-type-probes.*)
rm -rf -- "${tmp_root}"
;;
esac
}
trap cleanup EXIT
profile="${tmp_root}/profile.xml"
results="${tmp_root}/results"
vmoptions="${tmp_root}/pycharm.vmoptions"
stdout_log="${tmp_root}/inspect.stdout"
stderr_log="${tmp_root}/inspect.stderr"
problems="${tmp_root}/problems.xml"
cat >"${profile}" <<'XML'
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Alternative Type Probe" />
<inspection_tool class="PyAssertTypeInspection" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="PyTypeCheckerInspection" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>
XML
cat >"${vmoptions}" <<EOF
-Didea.config.path=${tmp_root}/config
-Didea.system.path=${tmp_root}/system
-Didea.log.path=${tmp_root}/log
-Didea.plugins.path=${tmp_root}/plugins
EOF
if ! PYCHARM_VM_OPTIONS="${vmoptions}" "${inspect_sh}" "${repo_root}" "${profile}" "${results}" -v0 >"${stdout_log}" 2>"${stderr_log}"; then
printf 'PyCharm inspect.sh failed.\n' >&2
if [[ -s "${stderr_log}" ]]; then
printf '\n[stderr]\n' >&2
tail --lines=80 "${stderr_log}" >&2
fi
if [[ -s "${stdout_log}" ]]; then
printf '\n[stdout]\n' >&2
tail --lines=80 "${stdout_log}" >&2
fi
exit 2
fi
if [[ ! -d "${results}" ]]; then
printf 'PyCharm inspect.sh did not create an inspection results directory.\n' >&2
exit 2
fi
: >"${problems}"
# PyNestedDecoratorsInspection is intentionally excluded here. PyCharm reports
# a false positive for correctly typed decorators stacked over classmethod or
# staticmethod; see PyNestedDecoratorsInspection-issue.md and related YouTrack
# issue PY-34368.
for inspection in \
PyAssertTypeInspection \
PyTypeCheckerInspection \
PyUnresolvedReferencesInspection
do
report="${results}/${inspection}.xml"
if [[ ! -f "${report}" ]]; then
continue
fi
normalized_report="${tmp_root}/${inspection}.normalized.xml"
sed -e 's#<problem>#\
<problem>#g' -e 's#</problem>#</problem>\
#g' "${report}" >"${normalized_report}"
awk -v inspection="${inspection}" '
/<problem>/ {
in_problem = 1
relevant = 0
block = $0 ORS
next
}
in_problem {
block = block $0 ORS
if ($0 ~ /typing_tests\/type_probes\.py/) {
relevant = 1
}
if ($0 ~ /<\/problem>/) {
if (relevant) {
print "<!-- " inspection " -->"
printf "%s", block
}
in_problem = 0
relevant = 0
block = ""
}
}
' "${normalized_report}" >>"${problems}"
done
if [[ -s "${problems}" ]]; then
cat "${problems}"
exit 1
fi