-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildScore.py
More file actions
82 lines (63 loc) · 2.54 KB
/
buildScore.py
File metadata and controls
82 lines (63 loc) · 2.54 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
from __future__ import print_function
import zipfile, os, re, subprocess, glob, math
import config
complexity = "/usr/local/bin/complexity"
def getRatio(scores):
sorted_s = sorted(scores)
quart_size = len(sorted_s)/4.0
quart = None
frac = quart_size - math.floor(quart_size)
quart = float(sum(sorted_s[-int(quart_size):]))
#TODO Add in the logic for non-4-divisible score lengths
return quart/sum(scores)
def buildFile(user, bracket, asgn):
# Filter out non-zip files
zip_filename = list(filter(lambda z: re.search("^\w+\.zip", z),
os.listdir(os.getcwd())))
if(len(zip_filename) != 1):
print("Skipping {} in {}: Found {} zip files".format(user,
bracket,
len(zip_filename)))
return
else:
zip_filename = os.path.join(os.getcwd(), zip_filename[0])
# extract the project
try:
with zipfile.ZipFile(zip_filename, 'r') as handle:
handle.extractall(os.getcwd())
except:
print("Skipping {} in {}: Extraction Failed".format(user, bracket))
return
# run complexity on the files
args = [complexity, "-t0", "-s1"] + glob.glob("*.c")
proc = subprocess.Popen(args,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
proc.wait()
out, err = proc.communicate()
if err.decode('utf-8') != '':
print("Skipping {} in {}: Complexity Failed".format(user, bracket))
print(err)
return
# Break out scores and get calculations
lines = out.decode('utf-8').split("\n")[2:-2] # Cut off the begining and end non-scores
scores = [int(x.split()[0]) for x in lines]
s_sum = sum(scores)
s_avg = s_sum / len(scores)
s_max = max(scores)
s_ratio = getRatio(scores)
# Build input for the template
template = [user, asgn]
user_data = [s_sum, s_avg, s_max, s_ratio]
ref_data = [config.REF_SUM, config.REF_AVG, config.REF_MAX, config.REF_RAT]
overall_score = 0
for i in range(len(user_data)):
template.append(user_data[i])
template.append(ref_data[i])
template.append(min(ref_data[i]/float(user_data[i]), 1) * 100)
overall_score += template[-1]
final_score = overall_score/len(user_data) * (float(bracket)/100)
template.append(final_score)
with open("score.txt", "w") as f:
f.write(config.template(tuple(template)))
return final_score