-
Notifications
You must be signed in to change notification settings - Fork 13
/
clean.py
executable file
·112 lines (83 loc) · 3.32 KB
/
clean.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
#! /usr/bin/env python2.5
## PyRT: Python Routeing Toolkit
## Cleans a trace: removes any "dirty" messages
## Copyright (C) 2001 Richard Mortier <[email protected]>, Sprint ATL
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
## 02111-1307 USA
import os, time, struct, getopt, sys, bgp, isis, mrtd, pprint
from mutils import *
################################################################################
if __name__ == "__main__":
VERBOSE = 1
#---------------------------------------------------------------------------
def usage():
print """Usage: %s [ options ] <filenames>:
-h|--help : Help
-v|--verbose : Be verbose
-q|--quiet : Be quiet""" %\
(os.path.basename(sys.argv[0]),)
sys.exit(0)
#---------------------------------------------------------------------------
if len(sys.argv) < 2:
usage()
try:
opts, args = getopt.getopt(sys.argv[1:],
"hqv",
("help", "verbose", "quiet", ))
except (getopt.error):
usage()
for (x, y) in opts:
if x in ('-h', '--help'):
usage()
elif x in ('-q', '--quiet'):
VERBOSE = 0
elif x in ('-v', '--verbose'):
VERBOSE = 2
filenames = args
if not filenames:
usage()
#---------------------------------------------------------------------------
verbose = VERBOSE-1
if verbose < 0:
verbose = 0
for fn in filenames:
cnt = 0
try:
of = open(fn + '.clean', 'w+b')
mrt = mrtd.Mrtd(fn, "rb", mrtd.DEFAULT_SIZE)
error('[ %s ] cleaning...' % fn)
while 1:
cnt = cnt + 1
msg_tup = mrt.read()
msg = msg_tup[-2] + msg_tup[-1]
try:
rv = mrt.parse(msg_tup, verbose)
except (KeyboardInterrupt):
raise KeyboardInterrupt
except:
rv["T"] = None
if rv["T"]:
of.write(msg)
else:
if VERBOSE:
print prthex("msg %d: " % cnt, msg)
error('msg %d dirty...' % cnt)
except (mrtd.EOFExc):
error("end of file: %u messages\n" % cnt)
except (KeyboardInterrupt):
error("interrupted!\n")
mrt.close()
of.close()
sys.exit(0)
################################################################################
################################################################################