-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·574 lines (486 loc) · 13.8 KB
/
run.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/bin/bash
# Author: Denton Wood
# Description: This script runs clone tools and compares outputs of each
# Tools: PyClone (https://github.com/calebdehaan/codeDuplicationParser)
# NiCad (https://www.txl.ca/txl-nicaddownload.html)
# Moss (https://theory.stanford.edu/~aiken/moss/)
###
# run_single()
#
# Description: Runs the script in single-repository mode. Each repository is
# only compared to itself.
# Arguments:
# repositories - the array of repositories to compare
# outputFile - file to print output to
#
run_single() {
repositories=$1
outputFile=$2
tempFiles=$3
# Get temp files
oxygenFilename="${tempFiles[0]}"
chlorineFilename="${tempFiles[1]}"
iodineFilename="${tempFiles[2]}"
nicadBlocksFilename="${tempFiles[3]}"
nicadFunctionsFilename="${tempFiles[4]}"
mossFilename="${tempFiles[5]}"
header=$(java -jar tools/clone-comparer/target/clone-comparer-1.0-SNAPSHOT.jar -M h pO pC nB nF m)
headerLine="Results,$header,OXYGEN Time,CHLORINE Time,NICAD_BLOCKS Time,NICAD_FUNCTIONS Time"
echo $headerLine > $outputFile
i=0
while [ $i -lt $num ]
do
# remove existing output
rm -f $TOOL_OUTPUT/*
repoName=${repositories[i]}
dirName="${repoName##*/}"
# Declare timestamp array & index
declare -a timestamps
t=0
# Process PyClone
cd tools/codeDuplicationParser
# Oxygen
start_time=$(date +%s.%6N)
$py_bin/pypy3 -m cli -a oxygen $repoName
exit_code=$?
end_time=$(date +%s.%6N)
if [ $? -ne 0 ]
then
echo "$repoName,PyClone (Oxygen) - unable to process" >> $outputFile
((i=i+1))
cd ../..
continue
fi
timestamps[t]=$($py_bin/pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
((t=t+1))
jsonFiles=(*.json)
oxygenFile="${jsonFiles[0]}"
mv $oxygenFile $oxygenFilename
cp $oxygenFilename $TOOL_OUTPUT
rm $oxygenFilename
# Chlorine
start_time=$(date +%s.%6N)
$py_bin/pypy3 -m cli -a chlorine $repoName
exit_code=$?
end_time=$(date +%s.%6N)
if [ $exit_code -ne 0 ]
then
echo "$repoName,PyClone (Chlorine) - unable to process" >> $outputFile
((i=i+1))
cd ../..
continue
fi
timestamps[t]=$($py_bin/pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
((t=t+1))
jsonFiles=(*.json)
chlorineFile="${jsonFiles[0]}"
mv $chlorineFile $chlorineFilename
cp $chlorineFilename $TOOL_OUTPUT
rm $chlorineFilename
cd ..
# Process NiCad
cd NiCad
# Blocks
start_time=$(date +%s.%6N)
./nicad6 blocks py ../../repos/$dirName
exit_code=$?
end_time=$(date +%s.%6N)
if [[ $exit_code -ne 0 ]]
then
echo "$repoName,NiCad Blocks - unable to process" >> $outputFile
((i=i+1))
cd ../..
continue
fi
cp ../../repos/$dirName\_blocks-blind-clones/$dirName\_blocks-blind-clones-0.30.xml $TOOL_OUTPUT
mv $TOOL_OUTPUT/$dirName\_blocks-blind-clones-0.30.xml $TOOL_OUTPUT/$nicadBlocksFilename
timestamps[t]=$($py_bin/pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
((t=t+1))
# Functions
start_time=$(date +%s.%6N)
./nicad6 functions py ../../repos/$dirName
exit_code=$?
end_time=$(date +%s.%6N)
if [[ $? -ne 0 ]]
then
echo "$repoName,NiCad Functions - unable to process" >> $outputFile
((i=i+1))
cd ../..
continue
fi
cp ../../repos/$dirName\_functions-blind-clones/$dirName\_functions-blind-clones-0.30.xml $TOOL_OUTPUT
mv $TOOL_OUTPUT/$dirName\_functions-blind-clones-0.30.xml $TOOL_OUTPUT/$nicadFunctionsFilename
timestamps[t]=$($py_bin/pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
((t=t+1))
cd ..
# Process Moss
cd Moss
echo "Flattening repository"
./flatten.sh ../../repos/$dirName
echo "Sending to Moss"
mossLink=$(./moss -l python ../../repos/$dirName/*.py | tail -n 1)
echo $mossLink > $TOOL_OUTPUT/$mossFilename
cd ../..
# Process Results
echo "Processing Results"
stats=$(java -jar tools/clone-comparer/target/clone-comparer-1.0-SNAPSHOT.jar -M s -pO $TOOL_OUTPUT/$oxygenFilename -pC $TOOL_OUTPUT/$chlorineFilename -nB $TOOL_OUTPUT/$nicadBlocksFilename -nF $TOOL_OUTPUT/$nicadFunctionsFilename -m $TOOL_OUTPUT/$mossFilename)
results="$repoName,$stats,${timestamps[0]},${timestamps[1]},${timestamps[2]},${timestamps[3]}"
echo $results >> $outputFile
echo "Results Processed"
((i=i+1))
done
}
###
# run_double()
#
# Description: Runs the script in dual-repository mode. Each repository is
# compared to every other repository
# Arguments:
# repositories - the array of repositories to compare
# outputFile - file to print output to
#
run_double() {
repositories=$1
outputFile=$2
tempFiles=$3
# Get temp files
oxygenFilename="${tempFiles[0]}"
chlorineFilename="${tempFiles[1]}"
iodineFilename="${tempFiles[2]}"
nicadBlocksFilename="${tempFiles[3]}"
nicadFunctionsFilename="${tempFiles[4]}"
mossFilename="${tempFiles[5]}"
# header=$(java -jar tools/clone-comparer/target/clone-comparer-1.0-SNAPSHOT.jar -M h pC pI nB nF)
# headerLine="Results,$header,CHLORINE Time,IODINE Time,NICAD_BLOCKS Time,NICAD_FUNCTIONS Time"
header=$(java -jar tools/clone-comparer/target/clone-comparer-1.0-SNAPSHOT.jar -M h pC nB nF)
headerLine="Results,$header,CHLORINE Time,NICAD_BLOCKS Time,NICAD_FUNCTIONS Time"
echo $headerLine > $outputFile
i=0
while [ $i -lt $num ]
do
repo1=${repositories[i]}
dir1="${repo1##*/}"
((j=i+1))
if [ $j -gt $num ]
then
# There's an odd number of repositories - skip the last one
((i=i+2))
continue
fi
repo2=${repositories[j]}
dir2="${repo2##*/}"
# remove existing output
rm -f $TOOL_OUTPUT/*
# Declare timestamp array & index
declare -a timestamps
t=0
# Process PyClone
cd tools/codeDuplicationParser
# Chlorine
start_time=$(date +%s.%6N)
$py_bin/pypy3 -m cli -a chlorine $repo1 $repo2
exit_code=$?
end_time=$(date +%s.%6N)
if [ $exit_code -ne 0 ]
then
echo "$repoName,PyClone (Chlorine) - unable to process" >> $outputFile
((j=j+1))
cd ../..
continue
fi
timestamps[t]=$(pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
((t=t+1))
jsonFiles=(*.json)
chlorineFile="${jsonFiles[0]}"
mv $chlorineFile $chlorineFilename
cp $chlorineFilename $TOOL_OUTPUT
rm $chlorineFilename
# Iodine
#start_time=$(date +%s.%6N)
#$py_bin/pypy3 -m cli -a iodine $repo1 $repo2
#exit_code=$?
#end_time=$(date +%s.%6N)
#if [ $exit_code -ne 0 ]
#then
# echo "$repoName,PyClone (Iodine) - unable to process" >> $outputFile
# ((j=j+1))
# cd ../..
# continue
#fi
#timestamps[t]=$($py_bin/pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
#((t=t+1))
#jsonFiles=(*.json)
#iodineFile="${jsonFiles[0]}"
#mv $iodineFile $iodineFilename
#cp $iodineFilename $TOOL_OUTPUT
#rm $iodineFilename
cd ..
# Process NiCad
cd NiCad
# Blocks
start_time=$(date +%s.%6N)
./nicad6cross blocks py ../../repos/$dir1 ../../repos/$dir2
exit_code=$?
end_time=$(date +%s.%6N)
if [ $exit_code -ne 0 ]
then
echo "$repoName,NiCad Blocks - unable to process" >> $outputFile
((j=j+1))
cd ../..
continue
fi
timestamps[t]=$($py_bin/pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
((t=t+1))
cp ../../repos/$dir1\_blocks-blind-crossclones/$dir1\_blocks-blind-crossclones-0.30.xml $TOOL_OUTPUT
mv $TOOL_OUTPUT/$dir1\_blocks-blind-crossclones-0.30.xml $TOOL_OUTPUT/$nicadBlocksFilename
# Functions
start_time=$(date +%s.%6N)
./nicad6cross functions py ../../repos/$dir1 ../../repos/$dir2
exit_code=$?
end_time=$(date +%s.%6N)
if [ $exit_code -ne 0 ]
then
echo "$repoName,NiCad Functions - unable to process" >> $outputFile
((j=j+1))
cd ../..
continue
fi
timestamps[t]=$($py_bin/pypy3 -c "print(\"%.6f\" % (${end_time} - ${start_time}))")
((t=t+1))
cp ../../repos/$dir1\_functions-blind-crossclones/$dir1\_functions-blind-crossclones-0.30.xml $TOOL_OUTPUT
mv $TOOL_OUTPUT/$dir1\_functions-blind-crossclones-0.30.xml $TOOL_OUTPUT/$nicadFunctionsFilename
cd ../..
# Process Results
echo "Processing Results"
#stats=$(java -jar tools/clone-comparer/target/clone-comparer-1.0-SNAPSHOT.jar -M d -pC $TOOL_OUTPUT/$chlorineFilename -pI $TOOL_OUTPUT/$iodineFilename -nB $TOOL_OUTPUT/$nicadBlocksFilename -nF $TOOL_OUTPUT/$nicadFunctionsFilename)
stats=$(java -jar tools/clone-comparer/target/clone-comparer-1.0-SNAPSHOT.jar -M d -pC $TOOL_OUTPUT/$chlorineFilename -nB $TOOL_OUTPUT/$nicadBlocksFilename -nF $TOOL_OUTPUT/$nicadFunctionsFilename)
# results="$repo1,$repo2,$stats,${timestamps[0]},${timestamps[1]},${timestamps[2]},${timestamps[3]}"
results="$repo1,$repo2,$stats,${timestamps[0]},${timestamps[1]},${timestamps[2]}"
echo $results >> $outputFile
echo "Results Processed"
((i=i+2))
done
}
###
# run_pyclone()
#
# Description: Runs the Pyclone tool pulling from a set of repositories
# Arguments:
# repositories - the array of repositories to use
#
run_pyclone() {
repositories=$1
outputFile=$2
tempFiles=$3
# Get temp files
chlorineFilename="${tempFiles[1]}"
iodineFilename="${tempFiles[2]}"
# Print header file
header=$(java -jar tools/clone-comparer/target/clone-comparer-1.0-SNAPSHOT.jar -M h -c pC pI)
headerLine="Results,$header"
echo $headerLine > $outputFile
# Find does not natively put files into an array, so we use a loop
# See https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash
pyfiles=()
while IFS= read -r -d $'\0'
do
pyfiles+=("$REPLY")
done < <(find $base_path/repos -name "*.py" -print0)
pyfilenum="${#pyfiles[@]}"
cd tools/PycloneTestBankCreator
# Remove existing clones
rm -f type1clones/*
i=0
while [ $i -lt $pyfilenum ]
do
filename=$(basename "${pyfiles[i]}")
cp "${pyfiles[i]}" $filename
$py_bin/pypy3 creator.py $filename
# Initialize local "repos"
repo1="${pyfiles[i]}.d"
repo2="${pyfiles[i]}-clone.py.d"
mkdir -p "$repo1"
mkdir -p "$repo2"
mv $filename $repo1
mv type1clones/*.py $repo2/$filename-clone.py
cd ../codeDuplicationParser
# Chlorine
$py_bin/pypy3 -m cli -a chlorine $repo1 $repo2
if [ $? -ne 0 ]
then
echo "$repoName,PyClone (Chlorine) - unable to process" >> $outputFile
((i=i+1))
cd ../PycloneTestBankCreator
rm $repo1/*.py
rm $repo2/*.py
continue
fi
jsonFiles=(*.json)
chlorineFile="${jsonFiles[0]}"
mv $chlorineFile $chlorineFilename
cp $chlorineFilename $TOOL_OUTPUT
rm $chlorineFilename
# Iodine
$py_bin/pypy3 -m cli -a iodine $repo1 $repo2
if [ $? -ne 0 ]
then
echo "$repoName,PyClone (Iodine) - unable to process" >> $outputFile
((i=i+1))
cd ../PycloneTestBankCreator
rm $repo1/*.py
rm $repo2/*.py
continue
fi
jsonFiles=(*.json)
iodineFile="${jsonFiles[0]}"
mv $iodineFile $iodineFilename
cp $iodineFilename $TOOL_OUTPUT
rm $iodineFilename
# Process Results
cd ../clone-comparer
echo "Processing Results"
stats=$(java -jar target/clone-comparer-1.0-SNAPSHOT.jar -M d -c -pC $TOOL_OUTPUT/$chlorineFilename -pI $TOOL_OUTPUT/$iodineFilename)
results="$repo1,$repo2,$stats"
echo $results >> $outputFile
echo "Results Processed"
cd ../PycloneTestBankCreator
rm -r $repo1
rm -r $repo2
rm -f type1clones/*
((i=i+1))
done
cd ../..
}
###
# print_usage()
#
# Description: Prints a quick help message describing how to use the
# script
#
print_usage() {
echo "Usage: ./run.sh [-hkp] [-m <mode>] -f <repository URL file>"
}
###
# print_full_usage()
#
# Description: Prints a full help message describing each argument
# of the function and its usage
#
print_full_usage() {
print_usage
echo "Options:"
echo " -f <file>: file containing repositories to scan (required)"
echo " -h: print this help"
echo " -k: keep temporary files when the analysis finishes"
echo " -m <mode>: mode in which to run comparison (required if -p is not set, must be single or double)"
echo " -p: use the Pyclone tool to generate clones and run comparison"
}
###
# Begin main script
while getopts 'f:hkm:p' flag
do
case "${flag}" in
f) repository_file=${OPTARG} ;;
h) print_full_usage
exit 0;;
k) keep=true ;;
m) mode=${OPTARG} ;;
p) pyclone=true ;;
*) print_usage
exit 1;;
esac
done
if [ $# == 0 ]
then
print_usage
exit 1
fi
if [ -z "$repository_file" ]
then
echo "Must supply a file containing repositories to check (-f)"
exit 3
fi
if [ ! -z "$mode" ]
then
if [ "$mode" != "single" ] && [ "$mode" != "double" ]
then
echo "Mode (-m) must be single or double"
exit 2
fi
fi
source ./.env
# Set output directories
base_path="$(pwd)"
OUTPUT="$base_path/output"
TOOL_OUTPUT="$base_path/toolOutput"
# Uncommend to Build the Maven project
# cd tools/clone-comparer
# mvn clean verify -q
# if [ $? -ne 0 ]
# then
# exit $?
# fi
# cd ../..
declare -a repositories
# Declare output files
outputFile=$OUTPUT/output-$mode-`date +%s`.csv
pycloneOutputFile=$OUTPUT/output-pyclone-`date +%s`.csv
# Declare temporary Cyclone output files
declare -a tempFiles
tempFiles[0]=oxygen.json
tempFiles[1]=chlorine.json
tempFiles[2]=iodine.json
tempFiles[3]=nicad-blocks.xml
tempFiles[4]=nicad-functions.xml
tempFiles[5]=moss.txt
# Get the repositories from the file
num=0
while read line
do
if [ ! -z $line ]
then
repositories[num]=$line
((num=num+1))
fi
done < $repository_file
# Iterate over the repositories
mkdir -p repos
cd repos
i=0
while [ $i -lt $num ]
do
git clone ${repositories[i]}
((i=i+1))
done
cd ..
# Clear out any existing JSON files in PyClone
rm -f tools/codeDuplicationParser/*.json
py_bin="$(pwd)/venv/bin"
# Activate Python virtual environment
source "$py_bin/activate"
# Create output directories
mkdir -p $OUTPUT
mkdir -p $TOOL_OUTPUT
# Run the clone tools
if [ ! -z "$mode" ]
then
if [ $mode == 'single' ]
then
run_single $repositories $outputFile $tempFiles
elif [ $mode == 'double' ]
then
run_double $repositories $outputFile $tempFiles
fi
fi
# Run Pyclone
if [ $pyclone ] && [ -z "$mode" ]
then
run_pyclone $repositories $pycloneOutputFile $tempFiles
fi
# Clean up flattened repositories
if ! $keep
then
rm -rf ./repos
else
echo "Repositories kept per flag given"
fi