createzodb.py

Script for creating the ZODB with indexing database from a SIP trace file - Adrian Georgescu, 08/29/2010 01:34 pm

Download (1.7 kB)

 
1
#!/usr/bin/python2.6
2

    
3
import os
4
import re
5
from datetime import datetime
6

    
7
import transaction
8
from ZODB.FileStorage import FileStorage
9
from ZODB.DB import DB
10
from zope.index.text.textindex import TextIndex
11

    
12
from packet import Packet
13

    
14

    
15
try: os.remove('data.db')
16
except: pass
17
try: os.remove('data.db.index')
18
except: pass
19
try: os.remove('data.db.lock')
20
except: pass
21
try: os.remove('data.db.tmp')
22
except: pass
23

    
24
storage = FileStorage('data.db')
25
db = DB(storage)
26
conn = db.open()
27
root = conn.root()
28
root['packets'] = {}
29
root['packets_index'] = index = TextIndex()
30

    
31

    
32
second_line = False
33
second_line_re = re.compile(r'^(?P<date>.+?):\s+(?P<srcaddr>.+?):(?P<srcport>\d+?)\s*-\((?P<proto>.+?)\)->\s*(?P<dstaddr>.+?):(?P<dstport>\d+?)$')
34
data = None
35
packet = ''
36
direction = None
37
for line in open('../../../.sipclient/logs/sip_trace.txt.0'):
38
    line = line.strip()
39
    if second_line:
40
        second_line = False
41
        data = second_line_re.match(line).groupdict()
42
        data['date'] = datetime.strptime(data['date'], '%Y-%m-%d %H:%M:%S.%f')
43
        data['srcport'] = int(data['srcport'])
44
        data['dstport'] = int(data['dstport'])
45
    elif line.startswith('RECEIVED:'):
46
        direction = 'incoming'
47
        data = None
48
        packet = ''
49
        second_line = True
50
    elif line.startswith('SENDING:'):
51
        direction = 'outgoing'
52
        data = None
53
        packet = ''
54
        second_line = True
55
    elif line == '--':
56
        pobj = Packet(data['date'], direction, data['srcaddr'], data['srcport'], data['dstaddr'], data['dstport'], data['proto'], packet)
57
        index.index_doc(pobj.id, pobj.packet)
58
        root['packets'][pobj.id] = pobj
59
        transaction.commit()
60
        direction = None
61
    else:
62
        packet += line + '\r\n'
63

    
64
conn.close()