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
|
#! /usr/bin/env python
# Copyright (c) 2017 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
import sys
import os
import tempfile
import subprocess
import difflib
import csv
import argparse
import LongBow
import ANSITerm
import FileUtil
import pprint
def getExemplar(fileName, command, config):
"""Create the exemplar formatted file into memory as a string"""
with open(fileName) as inputFile:
result = subprocess.check_output([command, "-q", "-c", config], stdin=inputFile)
return result;
def diff(exemplar, fileName):
d = difflib.Differ()
differ = d.compare(exemplar.splitlines(), fileName.splitlines())
return differ
class Ratchet:
def __init__(self):
self.currentValue = 0
self.signal = 0
def value(self):
return self.currentValue
def toggle(self, signal):
if self.signal == "-":
if signal == "-":
self.currentValue = self.currentValue + 1
elif signal == "?":
self.currentValue = self.currentValue + 1
self.signal = 0
else:
self.currentValue = self.currentValue + 1
self.signal = 0
pass
elif self.signal == "+":
if signal == "-":
self.currentValue = self.currentValue + 1
elif signal == "?":
self.currentValue = self.currentValue + 1
self.signal = 0
else:
self.currentValue = self.currentValue + 1
self.signal = 0
pass
else:
self.signal = signal;
return self.currentValue
def computeNonCompliantLines(differ):
lines = 0
changes = Ratchet()
for l in differ:
if l.startswith('-'):
changes.toggle(l[0])
lines = lines - 1
elif l.startswith('+'):
changes.toggle(l[0])
lines = lines + 1
elif l.startswith('?'):
pass
elif l.startswith(' '):
lines = lines +1
else:
print "What is this:", l
return changes.value()
def reportWhy(differ):
print '\n'.join(diff)
return
class SyntaxCompliance:
def __init__(self, fileName, exemplarCommand, exemplarConfig):
self.fileName = fileName
self.nonCompliantLines = 0
self.score = 0
self.exemplarCommand = exemplarCommand
self.exemplarConfig = exemplarConfig
try:
self.fileData = FileUtil.readFileString(self.fileName)
self.totalLines = len(self.fileData.splitlines())
except IOError, e:
print >> sys.stderr, e
sys.exit(1)
pass
def check(self):
self.exemplarData = getExemplar(self.fileName, self.exemplarCommand, self.exemplarConfig)
differ = diff(self.fileData, self.exemplarData)
self.nonCompliantLines = computeNonCompliantLines(differ)
return self
def report(self):
result = { "fileName" : self.fileName,
"label": "style",
"score": self.getScore(),
"totalLines" : self.getTotalLines(),
"nonCompliantLines" : self.getNonCompliantLines()
}
return result
def getFileName(self):
return self.fileName
def getExemplarCommand(self):
return self.exemplarCommand;
def getExemplarConfig(self):
return self.exemplarConfig;
def getScore(self):
result = 0
try:
result = int(100 * (1.0 - (float(self.getNonCompliantLines()) / float(self.getTotalLines()))))
except ZeroDivisionError:
pass
return result
def getTotalLines(self):
return self.totalLines
def getNonCompliantLines(self):
return self.nonCompliantLines
def explain(self):
self.exemplarData = getExemplar(self.fileName, self.exemplarCommand, self.exemplarConfig)
differ = diff(self.fileData, self.exemplarData)
ansiTerm = ANSITerm.ANSITerm()
for l in differ:
if l[0] == '-':
ansiTerm.printColorized("red", l)
elif l[0] == '+':
ansiTerm.printColorized("green", l)
elif l[0] == '?':
ansiTerm.printColorized("yellow", l[0:len(l)-1])
else:
print l
pass
return
def csvScore(distribution, report):
string = "style,%s,%d,%d,%.2f" % (report["fileName"], report["totalLines"], report["nonCompliantLines"], report["score"])
LongBow.scorePrinter(distribution, report["score"], string)
return
def csvAverage(distribution, complianceList):
scores = map(lambda target: target.getScore(), complianceList)
sum = reduce(lambda sum, score : sum + score, scores)
value = float(sum) / float(len(complianceList))
LongBow.scorePrinter(distribution, value, "%.2f" % (value))
return
def csvTotal(distribution, complianceList):
totalLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getTotalLines(), complianceList))
totalNonCompliantLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getNonCompliantLines(), complianceList))
value = 100.0 - (100.0 * float(totalNonCompliantLines) / float(totalLines))
LongBow.scorePrinter(distribution, value, "%.2f" % (value))
return
def csvSummary(distribution, complianceList):
map(lambda target: csvScore(distribution, target.report()), complianceList)
return
def textScore(distribution, report, maxFileNameLength, prefix=""):
'''
'''
format = "%s%-*s %6d %6d %6.2f"
string = format % (prefix, maxFileNameLength, report["fileName"], report["totalLines"], report["nonCompliantLines"], report["score"])
LongBow.scorePrinter(distribution, report["score"], string)
return
def textAverage(distribution, complianceList):
scores = map(lambda target: target.getScore(), complianceList)
sum = reduce(lambda sum, score : sum + score, scores)
value = float(sum) / float(len(complianceList))
LongBow.scorePrinter(distribution, value, "%.2f" % (value))
return
def textTotal(distribution, complianceList):
totalLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getTotalLines(), complianceList))
totalNonCompliantLines = reduce(lambda sum, x: sum + x, map(lambda element : element.getNonCompliantLines(), complianceList))
value = 100.0 - (100.0 * float(totalNonCompliantLines) / float(totalLines))
LongBow.scorePrinter(distribution, value, "%.2f" % (value))
return
def textSummary(distribution, complianceList, prefix=""):
if len(complianceList) > 0:
maxFileNameLength = max(max(map(lambda target: len(target.getFileName()), complianceList)), len("File Name"))
print "%s%-*s %6s %6s %6s" % (prefix, maxFileNameLength, "File Name", "Lines", "Errors", "Score")
map(lambda target: textScore(distribution, target.report(), maxFileNameLength, prefix), complianceList)
return
def textVisual(complianceList):
map(lambda target: target.explain(), complianceList)
return
def openDiff(sourceFile, exemplarCommand, exemplarConfig):
exemplar = getExemplar(sourceFile, exemplarCommand, exemplarConfig);
temporaryExemplarFile = tempfile.NamedTemporaryFile(suffix=".c", delete=False)
try:
with open(temporaryExemplarFile.name, "w") as exemplarOutput:
exemplarOutput.write(exemplar)
subprocess.check_output(["opendiff", sourceFile, temporaryExemplarFile.name, "-merge", sourceFile])
finally:
pass
return
def displaySummary(args, complianceList):
distribution = eval(args.distribution)
if args.output == "text":
textSummary(distribution, complianceList)
elif args.output == "gui":
textSummary(distribution, complianceList)
else:
csvSummary(distribution, complianceList)
return
def displayAverage(args, complianceList):
distribution = eval(args.distribution)
if args.output == "text":
textAverage(distribution, complianceList)
elif args.output == "gui":
textAverage(distribution, complianceList)
else:
csvAverage(distribution, complianceList)
return
def displayTotal(args, complianceList):
distribution = eval(args.distribution)
if args.output == "text":
textTotal(distribution, complianceList)
elif args.output == "gui":
textTotal(distribution, complianceList)
else:
csvTotal(distribution, complianceList)
return
def guiVisual(args, complianceList):
map(lambda target: openDiff(target.getFileName(), target.getExemplarCommand(), target.getExemplarConfig()), complianceList)
return
def displayVisual(args, complianceList):
if args.output == "text":
textVisual(complianceList)
elif args.output == "gui":
guiVisual(args, complianceList)
else:
print >> sys.stderr, "Unsupported output format '%s'. Expected 'text' or 'gui'." % (args.output)
sys.exit(1)
return
def sortComplianceList(args, complianceList):
sorter = {
"name" : { "function" : lambda k: k.getFileName(), "reverse" : False },
"descending-name" : { "function" : lambda k: k.getFileName(), "reverse" : True },
"score" : { "function" : lambda k: k.getScore(), "reverse" : False },
"descending-score" : { "function" : lambda k: k.getScore(), "reverse" : True },
"size" : { "function" : lambda k: k.getTotalLines(), "reverse" : False },
"descending-size" : { "function" : lambda k: k.getTotalLines(), "reverse" : True },
}
if args.key == "help":
print >> sys.stderr, "Supported sort keys:"
map(lambda k: sys.stderr.write("'" + k + "' "), sorted(sorter))
print
sys.exit(1)
if args.key in sorter:
complianceList = sorted(complianceList, key=sorter[args.key]["function"], reverse=sorter[args.key]["reverse"])
else:
print >> sys.stderr, "Unsupported sort key '%s'. Type '--key help'" % (args.key)
sys.exit(1)
return complianceList
def exclude(args, complianceList):
excluded = map(lambda token : token.strip(), args.exclude.split(","))
complianceList = filter(lambda entry: LongBow.score(eval(args.distribution), entry.getScore()) not in excluded, complianceList)
return complianceList
def gradeAndPrint(targets, exemplarCommand, exemplarConfig, problemsOnly=False, prefix=""):
complianceList = []
problemList = []
for target in targets:
try:
complianceList.append(SyntaxCompliance(target, exemplarCommand, exemplarConfig).check())
except:
problemList.append(target)
pass
complianceList = sorted(complianceList, key=lambda k: k.getFileName())
if problemsOnly:
complianceList = filter(lambda entry: entry.getScore() < 100, complianceList)
distribution=[99,90]
textSummary(distribution, complianceList, prefix)
for target in problemList:
print LongBow.buildRed("%s%s could not be evaluated" % (prefix, target))
def commandLineMain(args, targets, exemplarCommand, exemplarConfig):
complianceList = map(lambda target: SyntaxCompliance(target, exemplarCommand, exemplarConfig).check(), targets)
complianceList = sortComplianceList(args, complianceList)
complianceList = exclude(args, complianceList)
if args.summary:
displaySummary(args, complianceList)
elif args.average:
displayAverage(args, complianceList)
elif args.total:
displayTotal(args, complianceList)
elif args.visual:
displayVisual(args, complianceList)
return
|