-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfw_mk.py
97 lines (88 loc) · 2.45 KB
/
fw_mk.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
# -*- coding: utf-8 -*-
import csv
# format each rule in one single line and append it to flines
flines = []
cad = ''
n = 1
with open('firewall.rsc') as fp:
for line in fp:
if line.endswith('\\\n'):
cad += line[:len(line) - 2].strip() + ' '
else:
cad += line[:len(line) - 1].strip()
cad = cad.replace('= ', '=')
# print cad
if cad.startswith('#'):
print "{} ERROR: {}".format(n, cad)
else:
flines.append(cad)
cad = ''
n += 1
rules = []
n = 1
in_quoted_str = False
for l in flines:
l = l.split(' ')
print "{} Analitzant {}".format(n, l)
n += 1
cad = ''
in_quoted_str = False
tokens = []
# for each token in list
for t in l:
# if the token has double quotes, we find the closing
if (('"' in t) and (not t.endswith('"'))):
in_quoted_str = True
cad += t
elif t.endswith('"'):
in_quoted_str = False
cad += t
print "\t{}".format(cad)
cad = ''
elif (not '"' in t) and (in_quoted_str):
cad += t + ' '
elif (not '"' in t) and (not in_quoted_str):
# equal sign without continuation
if t.endswith('='):
cad += t
else:
cad = t
print "\t{}".format(cad)
tokens.append(cad)
cad = ''
rules.append([n, l, tokens])
n += 1
# extract different labels
labels = {}
for r in rules:
for t in r[2]: # for each property
if '=' in t:
g = t.split('=')
# save the key value
# we use a dictionary to avoid duplicated values
labels[g[0]] = 1
# print all possible labels
print "Labels -----"
labels = labels.keys()
labels.insert(0, 'rule')
for l in labels:
print l
# create the csv
with open('mk-out.csv', 'wb') as csvfile:
w = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
# write header
w.writerow(labels)
# for each line
for r in rules:
# initialize void line
line = []
for l in labels:
line.append('')
# first column is always rule number
line[0] = r[0]
# for each rule
for t in r[2]:
if '=' in t:
g = t.split('=')
line[labels.index(g[0])] = g[1]
w.writerow(line)