forked from irrdnet/irrd-legacy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
irr-prune
executable file
·56 lines (51 loc) · 1.74 KB
/
irr-prune
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
#!/usr/bin/env python3
# Quick and dirty script to minimize an IRR database to use only by
# bgpq3/bgpq4. The core idea is we have to only keep what is needed
# for "!i" (expand an AS-SET), "!gas" and "!6as" (get routes from
# specified AS) to work.
import sys
import re
START = 1
SKIPOBJECT = 2 # skip current object
KEEPATTRIBUTE = 3 # also, keep current object
SKIPATTRIBUTE = 4 # also, keep current object
names = re.compile(rb"^(?:as-set|aut-num|route|route6|route-set):")
attributes = re.compile(rb"^(?:origin|mnt-by|mbrs-by-ref|member-of|members|"
rb"mp-members|roa-status|source):")
comments = set(b"#%")
continuation = set(b" \t+")
state = START
for line in sys.stdin.buffer:
if line == b"" or line == b"\n" or line == b"\r\n":
# Empty line, end of the current object
if state not in {SKIPOBJECT, START}:
sys.stdout.buffer.write(line)
state = START
continue
if line[0] in comments:
# Skip comments
continue
if state == START:
# Outside of an object
if names.match(line):
sys.stdout.buffer.write(line)
state = KEEPATTRIBUTE
continue
state = SKIPOBJECT
continue
if state == SKIPOBJECT:
# Skip current object until the end
continue
if state in {SKIPATTRIBUTE, KEEPATTRIBUTE}:
if line[0] in continuation:
# Continuation
if state == KEEPATTRIBUTE:
sys.stdout.buffer.write(line)
continue
if attributes.match(line):
sys.stdout.buffer.write(line)
state = KEEPATTRIBUTE
continue
state = SKIPATTRIBUTE
continue
raise RuntimeError("Illegal state")