-
Notifications
You must be signed in to change notification settings - Fork 1
/
sophie.sh
executable file
·388 lines (250 loc) · 12.4 KB
/
sophie.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/bin/sh
# Copyright ma-ilsi
# Sophie: A Sophisticated Copyright Linter
# Licensed under Apache 2.0 License - see LICENSE file.
# Note: double quotes (") inside filter values of the sophie.config file must be escaped (\")
# Note: pipes (|) inside filter values of the sophie.config file must be escaped (\|)
# -- Global -- #
# Config file
config=`cat $PWD/sophie.config`
# Sophie config commands
supported_commands=" filecabinet notices compliance "
# Current config command
curr_command=""
# Current identifier being talked about
curr_identifier=""
# Name of filter to apply
filter=""
# Value of filter to filter files using
condition=""
# Index used for keeping place in strings that are passed to `cut(1)`
cut_count=0
# -- File Cabinet -- #
# List of "identifier=find command string (that represents matching files)"
file_cabinet=""
# `find` command, taken from the file cabinet, representing the list of matching files
old_files=""
# `find` command, to be inserted to the file cabinet, representing the list of matching files for the newly adjusted filters
new_files=""
# -- Notices -- #
# List of "identifier=grep command string (that represents copyright notices)"
notices=""
# `grep` command, taken from the notices folder, representing copyright notices
old_notice=""
# `grep` command, to be inserted in to the notices folder, representing copyright notices
new_notice=""
# -- Compliance -- #
# Identifier of the current compliance test
compliance_identifier=""
# Sophie identifiers divide up the file cabinet, this variable holds the current one we are testing compliance for
compliance_files_identifier=""
# Sophie identifiers divide up the notices folder, this variable holds the current one we are testing compliance for
compliance_notice_identifier=""
# Linking preposition part of compliance commands
compliance_prepostion=""
# The list of files we are testing compliance for
compliance_notice_files=""
# The copyright notice template we use to test `compliance_files` with
compliance_notice=""
# No. of passing files
success_count=0
# No. of failing files
fail_count=0
refinement=""
refined_files=""
while read line; do
if [ -z "$line" ]; then
continue
fi
if printf "%s" "$supported_commands" | grep -q -F " $line "; then
curr_command=$line
continue
fi
if [ "$curr_command" = "filecabinet" ]; then
# Identifier
cut_count=`expr "$line" : "[^\[]*\["`
curr_identifier=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/[[:space:]]*\[$//"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
# TODO: refinements are detected as second time requests to alter this identifier and a marker to indicate a refinement is placed.
while [ "$line" != "]" ]; do
# Remove any whitespace at the beginning
line=`printf "%s" "$line" | sed "s/^[ ]*//"`
# Filter
cut_count=`expr "$line" : "[^=]*=\""`
filter=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/=\"//"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
# Filter condition
cut_count=`expr "$line" : "[^\<\";\>]*\";"`
condition=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/\";//"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
# Fetching current command that represents matching files
if printf "%s" "$file_cabinet" | grep -q -F "__SOPHIE_IDENTIFIER$curr_identifier="; then
old_files=`printf "%s" "$file_cabinet" | grep -F "__SOPHIE_IDENTIFIER$curr_identifier="`
old_files=`printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=//"`
# Need to remove leftover \n at the beginning from previous commands
old_files=`printf "%s" "$old_files" | tr -d "\n"`
else
old_files="find . -type f"
fi
# Applying filters
if [ "$filter" = "pattern" ]; then
new_files=`printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-regex $condition"`
# Remove old
file_cabinet=`printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//"`
# Put new
file_cabinet=`printf "%s\n%s" "$file_cabinet" "__SOPHIE_IDENTIFIER$curr_identifier=$new_files"`
# This filter is done
filter=""
fi
if [ "$filter" = "size" ]; then
new_files=`printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-size $condition"`
# Remove old
file_cabinet=`printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//"`
# Put new
file_cabinet=`printf "%s\n%s" "$file_cabinet" "__SOPHIE_IDENTIFIER$curr_identifier=$new_files"`
# This filter is done
filter=""
fi
if [ "$filter" = "mtime" ]; then
new_files=`printf "%s%s%s" "$old_files" " __SOPHIE_MARKER " "-mtime $condition"`
# Remove old
file_cabinet=`printf "%s" "$file_cabinet" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//"`
# Put new
file_cabinet=`printf "%s\n%s" "$file_cabinet" "__SOPHIE_IDENTIFIER$curr_identifier=$new_files"`
# This filter is done
filter=""
fi
done
# This identifier is now deifned. We put a refinement marker in case a new command to refine the result of the previous one will be defined.
# Put refinement marker
file_cabinet=`printf "%s%s" "$file_cabinet" " __SOPHIE_REFINEMENT "`
continue
fi
if [ "$curr_command" = "notices" ]; then
# Identifier
cut_count=`expr "$line" : ".*\["`
curr_identifier=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/[[:space:]]*\[$//"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
while [ "$line" != "]" ]; do
# Remove any whitespace at the beginning
line=`printf "%s" "$line" | sed "s/^[ ]*//"`
# Filter
cut_count=`expr "$line" : "[^=]*=\""`
filter=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/=\"//"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
# Filter condition
cut_count=`expr "$line" : "[^\<\";\>]*\";"`
condition=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/\";//"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
# Apply filter after fetching old filter condition
if printf "%s" "$notices" | grep -q "$curr_identifier"; then
old_notice=`printf "%s" "$notices" | grep -F "__SOPHIE_IDENTIFIER$curr_identifier="`
old_notice=`printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=//"`
# Need to remove leftover \n at the beginning from previous commands
old_notice=`printf "%s" "$old_notice" | tr -d "\n"`
else
old_notice="grep -q"
fi
if [ "$filter" = "literal" ]; then
new_notice=`printf "%s%s%s" "$old_notice" " __SOPHIE_MARKER " "-F \"$condition\""`
# Remove old
notices=`printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//"`
# Put new
notices=`printf "%s\n%s" "$notices" "__SOPHIE_IDENTIFIER$curr_identifier=$new_notice"`
# This filter is done
filter=""
fi
# The `literal` filter is mutually excl. with `pattern`. Must not use both in a definition.
if [ "$filter" = "pattern" ]; then
new_notice=`printf "%s%s%s" "$old_notice" " __SOPHIE_MARKER " "\"$condition\""`
# Remove old
notices=`printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//"`
# Put new
notices=`printf "%s\n%s" "$notices" "__SOPHIE_IDENTIFIER$curr_identifier=$new_notice"`
# This filter is done
filter=""
fi
if [ "$filter" = "location" ]; then
new_notice=`printf "%s%s" "sed -n \"$condition\"p | " "$old_notice"`
# Remove old
notices=`printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$curr_identifier=.*//"`
# Put new
notices=`printf "%s\n%s" "$notices" "__SOPHIE_IDENTIFIER$curr_identifier=$new_notice"`
# This filter is done
filter=""
fi
done
continue
fi
if [ "$curr_command" = "compliance" ]; then
# Hardcoded to only support the `in` preposition. Prepositions can be extended easily without breaking configs.
# Identifier
cut_count=`expr "$line" : ".*\["`
curr_identifier=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/[[:space:]]*\[$//"`
cut_count=$((cut_count+2))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
compliance_identifier=$curr_identifier
cut_count=`expr "$line" : "[^ ]*"`
compliance_notice_identifier=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/ //"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
cut_count=`expr "$line" : ".* "`
compliance_preposition=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/ //"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
cut_count=`expr "$line" : ".*[^\\]\";"`
compliance_files_identifier=`printf "%s" "$line" | cut -c1-"$cut_count" | sed "s/\";//"`
cut_count=$((cut_count+1))
line=`printf "%s" "$line" | cut -c"$cut_count"-`
if [ -n "$compliance_files_identifier" ]; then
if [ -n "$compliance_notice_identifier" ]; then
if [ -n "$compliance_preposition" ]; then
compliance_files=`printf "%s" "$file_cabinet" | grep -F "__SOPHIE_IDENTIFIER$compliance_files_identifier="`
compliance_files=`printf "%s" "$compliance_files" | sed "s/__SOPHIE_IDENTIFIER$compliance_files_identifier=//"`
compliance_files=`printf "%s" "$compliance_files" | sed "s/ __SOPHIE_MARKER / /g"`
compliance_files=`printf "%s" "$compliance_files" | sed "s/ __SOPHIE_REFINEMENT /\
/g"`
while read refinement; do
compliance_files=""
refined_files=`$refinement`
compliance_files=`printf "%s\n%s" "$compliance_files" "$refined_files"`
done <<EOF
$compliance_files
EOF
compliance_notice=`printf "%s" "$notices" | grep -F "__SOPHIE_IDENTIFIER$compliance_notice_identifier="`
compliance_notice=`printf "%s" "$notices" | sed "s/__SOPHIE_IDENTIFIER$compliance_notice_identifier=//"`
compliance_notice=`printf "%s" "$compliance_notice" | sed "s/ __SOPHIE_MARKER / /g"`
fi
fi
fi
if [ "$line" = "]" ]; then
# This identifier's value is done being manipulated
curr_identifier=""
printf "\nCompliance Check Begins For: %s\n" "$compliance_identifier"
success_count=0
fail_count=0
for file in $compliance_files; do
# TODO: Consider using placeholders in the command stirngs to be filled in her with the filename
if eval "$compliance_notice" < "$file"; then
printf "Success: %s\n" "$file"
success_count=$((success_count+1))
else
printf "Fail: %s\n" "$file"
fail_count=$((fail_count+1))
fi
done
fail_count=$((fail_count+=success_count))
printf "Compliance Results For %s: %s/%s files passed.\n" "$compliance_identifier" "$success_count" "$fail_count"
fi
continue
fi
done <<EOF
$config
EOF
printf "\n"