Completed
Push — master ( 0f3e04...1c5edf )
by Thomas
30:23 queued 15:11
created

exabgp.application.decode.setargs()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
# encoding: utf-8
2
3
import sys
4
import syslog
5
import string
6
import argparse
7
8
from exabgp.environment import Env
9
from exabgp.environment import getenv
10
from exabgp.environment import ROOT
11
12
from exabgp.reactor.loop import Reactor
13
from exabgp.logger import log
14
15
16
def is_bgp(message):
17
    return all(c in string.hexdigits or c == ':' for c in message)
18
19
20
def setargs(sub):
21
    # fmt:off
22
    sub.add_argument('-d', '--debug', help='start the python debugger errors', action='store_true')
23
    sub.add_argument('-p', '--pdb', help='fire the debugger on fault', action='store_true')
24
    sub.add_argument('configuration', help='configuration file(s)', type=str)
25
    sub.add_argument('payload', help='the BGP payload in hexadecimal', nargs='+', type=str)
26
    # fmt:on
27
28
29
def main():
30
    parser = argparse.ArgumentParser(description=sys.modules[__name__].__doc__)
31
    setargs(parser)
32
    cmdline(parser.parse_args())
33
34
35
def cmdline(cmdarg):
36
    route = ''.join(cmdarg.payload).replace(' ', '')
37
38
    if not is_bgp(route):
39
        # parser.print_usage()
40
        sys.stdout.write('Environment values are:\n%s\n\n' % '\n'.join(' - %s' % _ for _ in Env.default()))
41
        sys.stdout.write('The BGP message must be an hexadecimal string.\n\n')
42
        sys.stdout.write('All colons or spaces are ignored, for example:\n\n')
43
        sys.stdout.write('  001E0200000007900F0003000101\n')
44
        sys.stdout.write('  001E:02:0000:0007:900F:0003:0001:01\n')
45
        sys.stdout.write('  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF001E0200000007900F0003000101\n')
46
        sys.stdout.write('  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF:001E:02:0000:0007:900F:0003:0001:01\n')
47
        sys.stdout.write("  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 001E02 00000007900F0003000101\n")
48
        sys.stdout.flush()
49
        sys.exit(1)
50
51
    env = getenv()
52
    env.log.parser = True
53
    env.debug.route = route
54
    env.tcp.bind = ''
55
56
    if cmdarg.debug:
57
        env.log.all = True
58
        env.log.level = 'DEBUG'
59
60
    if cmdarg.pdb:
61
        env.debug.pdb = True
62
63
    log.init(env)
64
    Reactor([cmdarg.configuration]).run()
65
66
67
if __name__ == '__main__':
68
    main()
69