forked from Checkmk/checkmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-shell-files
executable file
·191 lines (182 loc) · 5.91 KB
/
find-shell-files
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
#!/bin/sh
# Copyright (C) 2023 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.
SHEBANG_PATTERN='^\#!/.*sh'
usage() {
cat >&2 <<-HERE
usage: $(basename "${0}") [--filter]
--filter: read file candidates from lines of stdin (one file per line)
HERE
exit 1
}
get_candidates_from_stdin() {
while read -r line; do
# gracefully ignore non-existing files
[ -e "${line}" ] && printf "%s\000" "${line}"
done
}
_grep_with_excludes() {
# shellcheck disable=SC2046 # we want word splitting in the comment lines below
grep \
--recursive \
--files-with-matches \
--binary-files=without-match \
$(: file types which definitely contain no shell code ---------------) \
--exclude="*.Po" \
--exclude="*.a" \
--exclude="*.bat" \
--exclude="*.c" \
--exclude="*.cc" \
--exclude="*.cmake" \
--exclude="*.cmd" \
--exclude="*.cpp" \
--exclude="*.cs" \
--exclude="*.csproj" \
--exclude="*.css" \
--exclude="*.diff" \
--exclude="*.dll" \
--exclude="*.exe" \
--exclude="*.gif" \
--exclude="*.gpg" \
--exclude="*.groovy" \
--exclude="*.h" \
--exclude="*.hpp" \
--exclude="*.html" \
--exclude="*.ico" \
--exclude="*.idx" \
--exclude="*.imp" \
--exclude="*.info" \
--exclude="*.ipp" \
--exclude="*.jar" \
--exclude="*.java" \
--exclude="*.js" \
--exclude="*.json" \
--exclude="*.log" \
--exclude="*.lql" \
--exclude="*.m4" \
--exclude="*.map" \
--exclude="*.md" \
--exclude="*.msi" \
--exclude="*.o" \
--exclude="*.odt" \
--exclude="*.pc" \
--exclude="*.pdf" \
--exclude="*.pem" \
--exclude="*.php" \
--exclude="*.pl" \
--exclude="*.pm" \
--exclude="*.png" \
--exclude="*.po" \
--exclude="*.proto" \
--exclude="*.ps1" \
--exclude="*.puml" \
--exclude="*.py" \
--exclude="*.pyc" \
--exclude="*.pyi" \
--exclude="*.rs" \
--exclude="*.rst" \
--exclude="*.scss" \
--exclude="*.sln" \
--exclude="*.so" \
--exclude="*.svg" \
--exclude="*.t" \
--exclude="*.tar.bz2" \
--exclude="*.tar.gz" \
--exclude="*.tar.xz" \
--exclude="*.toml" \
--exclude="*.ts" \
--exclude="*.txt" \
--exclude="*.vbs" \
--exclude="*.vcxproj" \
--exclude="*.wav" \
--exclude="*.whl" \
--exclude="*.xml" \
--exclude="*.yaml" \
--exclude="*.yml" \
--exclude="*.zip" \
--exclude="Dockerfile" \
--exclude="README*" \
"$@"
}
get_files_from_filesystem() {
cd "$(dirname "$(dirname "$(realpath "$0")")")" || exit $?
# shellcheck disable=SC2046 # we want word splitting in the comment lines below
_grep_with_excludes \
$(: autoconf stuff --------------------------------------------------) \
--exclude-dir="autom4te.cache" \
--exclude="ar-lib" \
--exclude="compile" \
--exclude="config.guess" \
--exclude="config.status" \
--exclude="config.sub" \
--exclude="configure" \
--exclude="depcomp" \
--exclude="install-sh" \
--exclude="missing" \
$(: git stuff -------------------------------------------------------) \
--exclude-dir=".git" \
--exclude=".git*" \
$(: 3rd party C++ libraries -----------------------------------------) \
--exclude-dir="asio-*-patched" \
--exclude-dir="cpp-httplib-0.13.3" \
--exclude-dir="googletest-*-patched" \
$(: various artifact directories ------------------------------------) \
--exclude-dir="bazel-*" \
--exclude-dir="build" \
--exclude-dir="external" \
--exclude-dir="node_modules" \
--exclude-dir="shared_cargo_folder" \
$(: docker directories ----------------------------------------------) \
--exclude-dir="build_user_home" \
$(: search in omd separately, for more fine tuning ------------------) \
--exclude-dir="omd" \
"${SHEBANG_PATTERN}" ./* &&
_grep_with_excludes \
$(: just the status quo, feel free to fix stuff -----------------) \
--exclude="distro" \
--exclude="configure" \
--exclude="MKEVENTD*" \
--exclude=".f12" \
--exclude="AGENT_RECEIVER*" \
--exclude="MULTISITE_*" \
--exclude-dir="debian" \
--exclude-dir="apache-omd" \
--exclude-dir="appliance" \
--exclude-dir="build" \
--exclude-dir="maintenance" \
--exclude-dir="nagios" \
--exclude-dir="nagvis" \
--exclude-dir="navicli" \
--exclude-dir="perl-modules" \
--exclude-dir="enterprise" \
--exclude-dir="omd" \
--exclude-dir="pnp4nagios" \
--exclude-dir="mk-livestatus" \
"${SHEBANG_PATTERN}" ./omd/*
}
filter_for_shell_files() {
sort --zero-terminated |
xargs \
--no-run-if-empty \
--null \
grep --binary-files=without-match --files-with-matches "${SHEBANG_PATTERN}"
}
main() {
if [ $# -eq 0 ]; then
get_files_from_filesystem
elif [ $# -eq 1 ]; then
case $1 in
--filter)
# in the filtering case, no match by grep is OK!
get_candidates_from_stdin | filter_for_shell_files || true
;;
*)
usage
;;
esac
else
usage
fi
}
[ -z "${MK_SOURCE_ONLY}" ] && main "$@"