-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.py
343 lines (288 loc) · 11.6 KB
/
output.py
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
""" This file contains code for outputting results to both stdout and LaTeX files. """
import sys
import time
from stats import *
from filesystem import *
animation = ["-", "\\", "|", "/"]
animation_pos = 0
animation_last = time.time()
def animate():
""" Print animation. Note: this will overwrite the previous two characters. """
global animation_last
global animation_pos
# only animate every 100ms
if time.time() - animation_last > 0.1:
animation_last = time.time()
animation_pos += 1
sys.stdout.write("\b\b%s " % (animation[animation_pos % len(animation)]))
sys.stdout.flush()
def printTableRow(row, widths):
""" Print out a row from a table of data.
Arguments:
row -- a tuple of data points
width -- the widths of the columns
"""
format = ""
for width in widths:
format += "%%%ds" % width
print format % tuple(map(lambda x: str(x), row))
def printTexTableRow(row, file):
""" Print out a row from a table of data in LaTeX format.
Arguments:
row -- a tuple of data points
width -- the widths of the columns
"""
format = []
for col in row:
format.append("%s")
format = " & ".join(format)
format += " \\\\ \\hline\n"
file.write(format % tuple(map(lambda x: fixRow(str(x)), row)))
def printTexTable(table, filename, lastRowIsTotal = True):
""" Print out a table of data in LaTeX format to a file.
Arguments:
table -- list of tuples with data points, the first row is header and the last row is total
file -- file to print to
"""
min = 2 if lastRowIsTotal else 1
if len(table) < min:
raise Exception("error: table need to be at least "+str(min)+" rows")
file = open(filename, 'w')
cols = len(table[0])
file.write("\\begin{{tabular}}{{|l|{}|}} \n".format("|".join(['r' for i in range(cols-1)])))
file.write("\\hline \n")
printTexTableRow(map(lambda x: "\\textbf{%s}" % x, table[0]), file)
file.write("\\hline \n")
body = table[1:]
if lastRowIsTotal:
body = body[:-1]
for row in body:
printTexTableRow(row, file)
if lastRowIsTotal:
file.write("\\hline \n")
printTexTableRow(map(lambda x: "\\textbf{%s}" % x, table[-1]), file)
file.write("\\end{tabular}")
file.close()
def printTable(table, lastRowIsTotal = True):
""" Print out a table of data.
Arguments:
table -- a list of tuples with data points, the first row is header and the last row is total
"""
min = 2 if lastRowIsTotal else 1
if len(table) < min:
raise Exception("error: table need to be at least "+str(min)+" rows")
# calculate column widths
margin = 3
widths = map(lambda col: max(map(lambda row: len(str(row[col])), table))+margin, range(len(table[0])))
printTableRow(map(lambda x: x.upper(), table[0]), widths)
print "-" * sum(widths)
body = table[1:]
if lastRowIsTotal:
body = body[:-1]
for row in body:
printTableRow(row, widths)
if lastRowIsTotal:
print "-" * sum(widths)
printTableRow(table[-1], widths)
def printTexGraph(data, filename):
""" Print a LaTeX file with a bar graph.
Arguments:
data -- list of 3-tuples (name, absolute number, percentage)
filename -- filename of the latex file to print to
"""
file = open(filename, 'w')
xmax = roundUp(max(map(lambda x: x[1], data)))
labels = ",".join(map(lambda x: "{{{}}}".format(x[0]), data))
printLine = len(data[0]) == 3
if not printLine: # bars are percentage
xmax = 100
height = round(2.4 + 0.6 * len(data),2)
file.write("\\begin{tikzpicture}\n")
file.write("\\begin{axis}[\n")
file.write(" xbar, xmin=0, xmax={},\n".format(xmax))
file.write(" width=5cm, height={}cm,\n".format(height))
file.write(" axis x line*=bottom,\n")
file.write(" xlabel={Apps},\n")
file.write(" symbolic y coords={{{}}},\n".format(labels))
file.write(" ytick=data]\n")
file.write(" \\addplot coordinates {\n")
for d in data:
file.write(" ({},{{{}}})\n".format(d[1],d[0]))
file.write(" };\n")
file.write("\\end{axis}\n")
if printLine:
file.write("\\begin{axis}[\n")
file.write(" xmin=0, xmax=100,\n")
file.write(" width=5cm, height={}cm,\n".format(height))
file.write(" axis x line*=top,\n")
file.write(" axis y line*=none,\n")
file.write(" xlabel={Percentage}, xlabel near ticks,\n")
file.write(" symbolic y coords={{{}}},\n".format(labels))
file.write(" ytick=data,\n")
file.write(" yticklabels={,,}]\n")
file.write(" \\addplot+[sharp plot] coordinates {\n")
for d in data:
file.write(" ({},{{{}}})\n".format(d[2],d[0]))
file.write(" };\n")
file.write("\\end{axis}\n")
file.write("\\end{tikzpicture}\n")
file.close()
def printTexStackedGraph(legend, data, filename):
""" Print a LaTeX file with a stacked bar graph.
Arguments:
legend -- list of legend names and colors
data -- list of tuples (name, value1, value2, ..., valueN)
filename -- filename of the latex file to print to
Note that the length of the legend list must match the number of
values in the data tuple.
"""
file = open(filename, 'w')
stacks = len(legend)
xmax = roundUp(max(map(lambda x: sum(x[1:]), data)))
labels = ",".join(map(lambda x: "{{{}}}".format(x[0]), data))
legends = ",".join(map(lambda x: "{{{}}}".format(x[0]), legend))
height = round(2.4 + 0.6 * len(data),2)
file.write("\\begin{tikzpicture}\n")
file.write("\\begin{axis}[\n")
file.write(" xbar stacked, xmin=0, xmax={},\n".format(xmax))
file.write(" width=.8\\textwidth, height={}cm,\n".format(height))
file.write(" scaled ticks=false,\n")
file.write(" tick label style={/pgf/number format/fixed},\n")
file.write(" axis x line*=bottom,\n")
file.write(" axis y line*=none,\n")
file.write(" tick label style={font=\\footnotesize},\n")
file.write(" legend style={font=\\footnotesize},\n")
file.write(" label style={font=\\footnotesize},\n")
file.write(" symbolic y coords={{{}}},\n".format(labels))
file.write(" ytick=data,,\n")
file.write(" xlabel={Apps},\n")
file.write(" area legend,\n")
file.write(" legend style={{legend columns={},at={{(0,-0.1)}},anchor=north west,draw=none}},\n".format(len(legend)))
file.write(" enlarge y limits=0.1]\n")
file.write("\\legend{{{}}}\n".format(legends))
for i in xrange(0,len(legend)):
l = legend[i]
file.write(" \\addplot[fill={}] coordinates {{\n".format(l[1]))
for d in data:
file.write(" ({},{{{}}})\n".format(d[i+1],d[0]))
file.write(" };\n")
file.write("\\end{axis}\n")
file.write("\\end{tikzpicture}\n")
file.close()
def outputResults(args, apps):
""" Print statistics out to LaTeX files.
Arguments:
args -- command line argument object
apps -- dictionary with apps and their meta data
"""
if not args.skip_generating:
createTexFolder(args)
print "calculating statistics"
stats = calculateStatistics(apps)
_c = stats['categories']
_t = stats['total']
top_size = 50
table = map(lambda x: (fixName(x), _c[x]['total'], _c[x]['internet'], percentage(_c[x]['internet'], _c[x]['total'])), _c)
table = sorted(table, key=lambda x: str2float(x[3]))
table.reverse()
table.insert(0, ('Category','Total','Internet permission','Internet permission'))
table.append(('Total', _t['total'], _t['internet'], percentage(_t['internet'], _t['total'])))
if not args.skip_generating:
printTexTable(table, args.tex_dir + "table_internet.tex")
data = map(lambda x: (x[0], x[2], int(str2float(x[3]))), table[1:-1])
printTexGraph(data, args.tex_dir + "graph_internet.tex")
if not args.skip_printing:
print ""
printTable(table)
print ""
l = [
('trustmanagers','TrustManager','naive_trustmanagers','Naive TrustManager', 'trustmanagers'),
('custom_hostname_verifiers','HostnameVerifier','naive_hostname_verifiers','Naive HostnameVerifier', 'hostname_verifiers')
]
for t in l:
table = map(lambda x: (fixName(x), _c[x][t[0]], _c[x][t[2]], percentage(_c[x][t[0]], _c[x]['internet'] - _c[x]['unchecked']), percentage(_c[x][t[2]], _c[x]['internet'] - _c[x]['unchecked'])), _c)
table = sorted(table, key=lambda x: str2float(x[4]))
table.reverse()
table.insert(0, ('Category',t[1],t[3],t[1],t[3]))
table.append(("Total", _t[t[0]], _t[t[2]], percentage(_t[t[0]], _t['internet'] - _t['unchecked']), percentage(_t[t[2]], _t['internet'] - _t['unchecked'])))
if not args.skip_generating:
printTexTable(table, args.tex_dir + 'table_' + t[4] + '.tex')
data = map(lambda x: (x[0], x[1], int(str2float(x[3]))), table[1:-1])
printTexGraph(data, args.tex_dir + 'graph_' + t[4] + '.tex')
data = map(lambda x: (x[0], x[2], int(str2float(x[4]))), table[1:-1])
printTexGraph(data, args.tex_dir + 'graph_' + t[2] + '.tex')
if not args.skip_printing:
printTable(table)
print ""
l = [
('insecure_factories','Insecure SSLSocketFactory', 'ssl_socket_factories'),
('allow_all_hostname_verifiers','AllowAllHostnameVerifier', 'allow_all_hostname_verifiers'),
('ssl_error_handlers','onReceivedSslError', 'on_received_ssl_error_handlers'),
('native','Native', 'native'),
('custom','Custom', 'custom'),
('naive','Naive', 'naive'),
('bad','Bad', 'bad')
]
for t in l:
table = map(lambda x: (fixName(x), _c[x][t[0]], percentage(_c[x][t[0]], _c[x]['internet'] - _c[x]['unchecked'])), _c)
table = sorted(table, key=lambda x: str2float(x[2]))
table.reverse()
table.insert(0, ('Category',t[1],t[1]))
table.append(("Total", _t[t[0]], percentage(_t[t[0]], _t['internet'] - _t['unchecked'])))
if not args.skip_generating:
printTexTable(table, args.tex_dir + 'table_' + t[2] + ".tex")
data = map(lambda x: (x[0], x[1], int(str2float(x[2]))), table[1:-1])
printTexGraph(data, args.tex_dir + 'graph_' + t[2] + '.tex')
if not args.skip_printing:
printTable(table)
print ""
# sections
l = [
('years',('Year','Bad','Bad'),[]),
('downloads',('Downloads','Bad','Bad'),[]),
('ratings',('Rating','Bad','Bad'),[])
]
# sort sections
c = stats['years']
_l = map(lambda x: (x, c[x]['bad'], percentage(c[x]['bad'], c[x]['internet'] - c[x]['unchecked'])), c)
_l = sorted(_l, key=lambda x: str2float(x[2]))
l[0][2].extend(_l)
for x in ['0-100','100-10,000','10,000-1,000,000','1,000,000-100,000,000','100,000,000+']:
c = stats['downloads'][x]
l[1][2].append((x, c['bad'], percentage(c['bad'], c['internet']-c['unchecked'])))
for x in ['0-1','1-2','2-3','3-4','4-5','Unknown']:
c = stats['ratings'][x]
l[2][2].append((x, c['bad'], percentage(c['bad'], c['internet']-c['unchecked'])))
# output sections
for _l in l:
c = stats[_l[0]]
table = _l[2]
table.insert(0, _l[1])
if not args.skip_generating:
printTexTable(table, args.tex_dir + 'table_'+_l[0]+'.tex', False)
data = map(lambda x: (x[0], x[1], int(str2float(x[2]))), table[1:])
printTexGraph(data, args.tex_dir + 'graph_' +_l[0]+'.tex')
if not args.skip_printing:
printTable(table, False)
print ""
# verifier type
table = []
tot = _t['internet'] - _t['unchecked']
for type in [('native', 'Native or none'), ('custom', 'Custom'), ('naive', 'Naive'),('bad', 'Bad')]:
table.append((type[1], _t[type[0]], percentage(_t[type[0]], tot)))
table.insert(0, ("Verifier type", "Apps", "Percentage"))
if not args.skip_generating:
printTexTable(table, args.tex_dir + 'table_verifier_type.tex', False)
legend = [('Native', 'green'),('Custom', 'yellow'),('Naive', 'orange'),('Bad', 'red')]
sections = ['native','custom','naive','bad']
data = map(lambda x: stats['total'][x], sections)
data = [tuple(['Verifier type'] + data)]
printTexStackedGraph(legend, data, args.tex_dir + 'graph_verifier_type.tex')
c = stats['categories']
data = map(lambda x: tuple([fixName(x)] + map(lambda y: c[x][y], sections)),c)
data = map(lambda x: tuple([x[0]] + map(lambda y: round(100.0 * y / sum(x[1:]), 3), x[1:])), data)
data = sorted(data, key=lambda x: -1 * (x[3] + x[4]))
printTexStackedGraph(legend, data, args.tex_dir + 'graph_verifier_type_categories.tex')
if not args.skip_printing:
printTable(table, False)
print ""