-
Notifications
You must be signed in to change notification settings - Fork 6
/
syntaxchecks.sh
executable file
·206 lines (172 loc) · 4.78 KB
/
syntaxchecks.sh
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
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
### configuration
path="."
last_commits=0
strict=0
# find files for syntaxchecks
regex_bash_files="^.*\.sh$"
regex_php_files="^.*\.(php|php.skel|php.skel.*)$"
regex_yaml_files="^.*\.(yaml|yml)$"
regex_python_files="^.*\.py$"
regex_ruby_files="^.*(\.rb|Vagrantfile)$"
regex_crontab_files="^.*\.cron$"
# exclude vendor and cache directories
regex_exclude="^.*/(vendor|cache)/.*$"
# script path
script_path=$(dirname "$0")
### functions
function usage() {
echo "Usage: $0 [-p <path>] [-c <last_commits>] [-r <overwrite regex exclude>] [-a] [-s]" 1>&2; exit 1;
}
function system_check() {
if [[ ! $(which php) ]]; then echo "PHP is not installed."; exit 1; fi
if [[ ! $(which python) ]]; then echo "Python is not installed."; exit 1; fi
if [[ ! $(which ruby) ]]; then echo "Ruby is not installed."; exit 1; fi
echo "ok"
}
function get_file_list() {
if [ $last_commits -eq 0 ]; then
filelist=$(find $path -type f -regextype posix-extended -regex "$1" | grep -v -E "$regex_exclude")
else
filelist=$(cd $path && git diff --name-only --diff-filter=ACMR HEAD~$last_commits..HEAD | grep -E "$1" | grep -v -E "$regex_exclude")
fi
}
function check_bash_files() {
echo "==> Check Bash files"
get_file_list "$regex_bash_files"
for file in $filelist
do
first_line=$(head -n 1 "$file")
if [[ $first_line =~ ^\#\! ]] && ! [[ $first_line =~ .+bin.+sh ]]; then
echo "WARNING: Skipped file, because shebang is missing in $file"
continue
fi
bash -n "$file" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "No syntax errors detected in $file"
else
echo "ERROR: Some syntax errors detected in $file"
if [ $strict -eq 1 ]; then
bash -n "$file"
exit 1;
fi
fi
done
echo ""
}
function check_php_files() {
echo "==> Check PHP files"
get_file_list "$regex_php_files"
for file in $filelist
do
php -l "$file" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "No syntax errors detected in $file"
else
echo "ERROR: Some syntax errors detected in $file"
if [ $strict -eq 1 ]; then
php -l "$file"
exit 1;
fi
fi
done
echo ""
}
function check_python_files() {
echo "==> Check Python files"
get_file_list "$regex_python_files"
for file in $filelist
do
python -m py_compile "$file" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "No syntax errors detected in $file"
else
echo "ERROR: Some syntax errors detected in $file"
if [ $strict -eq 1 ]; then
python -m py_compile "$file"
exit 1;
fi
fi
done
echo ""
}
function check_ruby_files() {
echo "==> Check Ruby files"
get_file_list "$regex_ruby_files"
for file in $filelist
do
ruby -c "$file" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "No syntax errors detected in $file"
else
echo "ERROR: Some syntax errors detected in $file"
if [ $strict -eq 1 ]; then
ruby -c "$file"
exit 1;
fi
fi
done
echo ""
}
function check_yaml_files() {
echo "==> Check YAML files"
get_file_list "$regex_yaml_files"
for file in $filelist
do
ruby -e "require 'yaml';puts YAML.load_file(\"$file\")" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "No syntax errors detected in $file"
else
echo "ERROR: Some syntax errors detected in $file"
if [ $strict -eq 1 ]; then
ruby -e "require 'yaml';puts YAML.load_file(\"$file\")"
exit 1;
fi
fi
done
echo ""
}
function check_crontab_files() {
echo "==> Check Crontab files"
get_file_list "$regex_crontab_files"
for file in $filelist
do
"$script_path"/bin/chkcrontab/chkcrontab "$file" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "No syntax errors detected in $file"
else
echo "ERROR: Some syntax errors detected in $file"
if [ $strict -eq 1 ]; then
"$script_path"/bin/chkcrontab/chkcrontab "$file"
exit 1;
fi
fi
done
echo ""
}
### main
while getopts ':p:c:r:as' OPTION ; do
case "$OPTION" in
p) path=$OPTARG;;
c) last_commits=$OPTARG;;
r) regex_exclude=$OPTARG;;
a) last_commits=0;;
s) strict=1;;
*) usage;;
esac
done
echo -n "==> Systemcheck: "
system_check
echo ""
echo "==> Config"
echo "Path: $path"
if [ $last_commits -eq 0 ]; then echo "Check files: all"; else echo "Check files: last $last_commits commit(s)"; fi
if [ $strict -eq 0 ]; then echo "Strict mode: off"; else echo "Strict mode: on"; fi
echo ""
check_bash_files
check_php_files
check_python_files
check_ruby_files
check_yaml_files
check_crontab_files
echo "==> Finished Syntaxchecks"