summaryrefslogtreecommitdiff
path: root/utils/generate-packet-lengths.py
blob: a88882e8fec2b667f22b8ada4c4f6b5229f1a799 (plain) (blame)
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
#!/usr/bin/python

"""
Generate a dict of packet lenghts, based on packetsin.inc from ManaPlus.
Author: Joseph Botosh <rumly111@gmail.com>
Licence: GPLv2.
"""

import os
import sys


def GeneratePacketLengths(infile):
    plength = {}
    with open(infile) as f:
        for l in f:
            if l.startswith('packet('):
                w = l[7:-2].split(',')
                opcode = int(w[1].strip(), 16)
                length = int(w[2].strip())
                plength[opcode] = length
    return plength


def PrettyPrint(plengths, width=80):
    s = 'packet_lengths = {\n    '
    curr_line_len = 4
    for opcode in sorted(plengths.keys()):
        r = '0x{:04x}: {:d},'.format(opcode, plengths[opcode]).ljust(12)
        if curr_line_len + len(r) > width:
            curr_line_len = 4
            s += '\n    '
        s += r
        curr_line_len += len(r)
    s += '}\n'
    return s


def PrintHelp():
    print('Usage: {} /path/to/packetsin.inc'.format(sys.argv[0]))

if __name__ == '__main__':
    if len(sys.argv) == 1:
        PrintHelp()
        sys.exit(0)
    filename = sys.argv[1]
    if os.path.isfile(filename):
        pl = GeneratePacketLengths(filename)
        print(PrettyPrint(pl))
        # print('packet_lengths = ',
        #       GeneratePacketLengths(filename))
    else:
        print('File not found:', filename, file=sys.stderr)
        sys.exit(1)