Skip to content

Commit

Permalink
more code ruff'ing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mangin committed Oct 25, 2024
1 parent 48b15e5 commit 47a7dab
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 60 deletions.
2 changes: 1 addition & 1 deletion etc/exabgp/example-api-program.run
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _reader():

if not data:
# we lost the pipe
sys.stderr.write('the read pipe was closed by the other side : %s' % str(exc))
sys.stderr.write('the read pipe was closed by the other side')
sys.exit(1)

received += data
Expand Down
18 changes: 10 additions & 8 deletions etc/exabgp/run/api-blocklist.run
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import sys
import errno
import threading
import time
import json
import ipaddress
import traceback
import requests
Expand Down Expand Up @@ -44,7 +43,10 @@ blocklists = [
{'url': 'https://www.spamhaus.org/drop/drop.txt', 'refresh': 7200},
{'url': 'https://www.spamhaus.org/drop/edrop.txt', 'refresh': 7200},
{'url': 'https://www.spamhaus.org/drop/dropv6.txt', 'refresh': 7200},
{'url': 'https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt', 'refresh': 7200},
{
'url': 'https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt',
'refresh': 7200,
},
{'url': 'https://blocklist.greensnow.co/greensnow.txt', 'refresh': 7200},
{'url': 'https://www.darklist.de/raw.php', 'refresh': 7200},
{'url': 'https://sigs.interserver.net/ipslim.txt', 'refresh': 7200},
Expand All @@ -63,12 +65,12 @@ def requestsGet(url):
def lineFilter(line):
if not line:
return None
l = line.strip()
if l.startswith(';'):
line = line.strip()
if line.startswith(';'):
return None
if l.startswith('#'):
if line.startswith('#'):
return None
return l.split(' ')[0].split(';')[0].split('#')[0].strip()
return line.split(' ')[0].split(';')[0].split('#')[0].strip()


class blocklistThread(object):
Expand Down Expand Up @@ -200,15 +202,15 @@ if __name__ == '__main__':
newBlocklist[prefix] = None

for prefix in currentBlocklist:
if not prefix in newBlocklist:
if prefix not in newBlocklist:
specs = specs4
if ipaddress.ip_network(prefix).version == 6:
specs = specs6
sys.stdout.write('withdraw route ' + str(prefix) + specs + '\n')
sys.stdout.flush()

for prefix in newBlocklist:
if not prefix in currentBlocklist:
if prefix not in currentBlocklist:
specs = specs4
if ipaddress.ip_network(prefix).version == 6:
specs = specs6
Expand Down
1 change: 0 additions & 1 deletion etc/exabgp/run/api-no-respawn-1.run
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

import sys
import time

messages = [
'announce route 1.1.1.1/32 next-hop 11.11.11.11',
Expand Down
62 changes: 54 additions & 8 deletions qa/bin/functional
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ class EncodingTests(Tests):
self.collect()
if self.code == 0:
if self._check in self.stdout:
if os.getenv('DEBUG', None) != None:
if os.getenv('DEBUG', None) is not None:
self.report('completed successfully')
return True
if self._check in self.stderr:
if os.getenv('DEBUG', None) != None:
if os.getenv('DEBUG', None) is not None:
self.report('completed successfully')
return True

Expand Down Expand Up @@ -435,10 +435,30 @@ class EncodingTests(Tests):
result = []
for test in self.selected():
result.append(
' '.join(['>', sys.argv[0], 'encoding', '--server', test.nick, '--port', f'{test.conf["port"]}'])
' '.join(
[
'>',
sys.argv[0],
'encoding',
'--server',
test.nick,
'--port',
f'{test.conf["port"]}',
]
)
)
result.append(
' '.join(['>', sys.argv[0], 'encoding', '--client', test.nick, '--port', f'{test.conf["port"]}'])
' '.join(
[
'>',
sys.argv[0],
'encoding',
'--client',
test.nick,
'--port',
f'{test.conf["port"]}',
]
)
)
return '\n'.join(result)

Expand All @@ -449,7 +469,16 @@ class EncodingTests(Tests):
for test in self.selected():
test.setup()
self.display()
test.run([sys.argv[0], 'encoding', '--server', test.nick, '--port', f'{test.conf["port"]}'])
test.run(
[
sys.argv[0],
'encoding',
'--server',
test.nick,
'--port',
f'{test.conf["port"]}',
]
)
time.sleep(0.005)

time.sleep(0.02)
Expand All @@ -458,7 +487,14 @@ class EncodingTests(Tests):
test.setup()
self.display()
client[test.nick] = Exec().run(
[sys.argv[0], 'encoding', '--client', test.nick, '--port', f'{test.conf["port"]}']
[
sys.argv[0],
'encoding',
'--client',
test.nick,
'--port',
f'{test.conf["port"]}',
]
)
time.sleep(0.005)

Expand Down Expand Up @@ -578,7 +614,12 @@ class DecodingTests(Tests):
self.display()
message = test.conf['type']
if message == 'open':
cmd = [Path.EXABGP, 'decode', '--%s' % test.conf['type'], test.conf['packet']]
cmd = [
Path.EXABGP,
'decode',
'--%s' % test.conf['type'],
test.conf['packet'],
]
elif message in ['update', 'nlri']:
cmd = [
Path.EXABGP,
Expand Down Expand Up @@ -774,7 +815,12 @@ if __name__ == '__main__':
subparser = parser.add_subparsers()

add_test(subparser, 'decoding', decoding, ['list', 'edit', 'dry', 'timeout', 'port'])
add_test(subparser, 'encoding', encoding, ['list', 'edit', 'dry', 'timeout', 'port', 'server', 'client'])
add_test(
subparser,
'encoding',
encoding,
['list', 'edit', 'dry', 'timeout', 'port', 'server', 'client'],
)
add_test(subparser, 'parsing', parsing, ['list', 'dry', 'edit'])

parsed = parser.parse_args()
Expand Down
32 changes: 25 additions & 7 deletions qa/sbin/bgp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ class Checker(object):
ordered = []
for kg in sorted(list(group.keys())):
source = group[kg]
appending = []
for ks in sorted(list(source.keys())):
ordered.append(source[ks])
return ordered
Expand Down Expand Up @@ -692,13 +691,31 @@ def parse_cmdline():

parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--help', help='this help :-)', action='store_true')
parser.add_argument('--echo', help='accept any BGP messages send it back to the emiter', action='store_true')
parser.add_argument('--sink', help='accept any BGP messages and reply with a keepalive', action='store_true')
parser.add_argument('--asn', help='ASN to use (otherwise extracted from the OPEN)', type=int, default=None)
parser.add_argument(
'--echo',
help='accept any BGP messages send it back to the emiter',
action='store_true',
)
parser.add_argument(
'--sink',
help='accept any BGP messages and reply with a keepalive',
action='store_true',
)
parser.add_argument(
'--asn',
help='ASN to use (otherwise extracted from the OPEN)',
type=int,
default=None,
)
parser.add_argument('--port', help='port to bind to', type=int, default=port)
parser.add_argument('--view', help='look at the expected packets', action='store_true')
parser.add_argument('--ipv6', help='bind using ipv6', action='store_true')
parser.add_argument('checks', help='a list of expected route announcement/withdrawl', nargs='?', type=open)
parser.add_argument(
'checks',
help='a list of expected route announcement/withdrawl',
nargs='?',
type=open,
)

cmdarg = parser.parse_args()

Expand Down Expand Up @@ -801,7 +818,8 @@ async def main(options, checker, queue):
service = BGPService(loop, queue, options, checker)

server = await asyncio.start_server(
lambda reader, writer: BGPProtocol(service, reader, writer).handle_bgp(), sock=sock
lambda reader, writer: BGPProtocol(service, reader, writer).handle_bgp(),
sock=sock,
)
# perhaps set backlog to 1 ..

Expand All @@ -826,7 +844,7 @@ if __name__ == '__main__':
flushed('\n')

if queue.empty():
flushed(f'failed with no code')
flushed('failed with no code')
sys.exit(1)

code = queue.get()
Expand Down
64 changes: 51 additions & 13 deletions qa/sbin/bgp-3.6
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ class BGPHandler(asyncore.dispatcher_with_send):
yield 'notification:%d,%d' % (body[0], body[1]), bytestream(body)

def announce(self, *args):
flushed(' ', self.ip, self.port, ' '.join(str(_) for _ in args) if len(args) > 1 else args[0])
flushed(
' ',
self.ip,
self.port,
' '.join(str(_) for _ in args) if len(args) > 1 else args[0],
)

def check_signal(self):
if self.messages and self.messages[0].startswith('signal:'):
Expand Down Expand Up @@ -307,7 +312,12 @@ class BGPHandler(asyncore.dispatcher_with_send):
% (
self.counter,
'%s:%s:%s:%s'
% (bytestream(header[:16]), bytestream(header[16:18]), bytestream(header[18:]), bytestream(body)),
% (
bytestream(header[:16]),
bytestream(header[16:18]),
bytestream(header[18:]),
bytestream(body),
),
)
)
self.send(self.keepalive)
Expand All @@ -319,7 +329,12 @@ class BGPHandler(asyncore.dispatcher_with_send):
% (
self.counter,
'%s:%s:%s:%s'
% (bytestream(header[:16]), bytestream(header[16:18]), bytestream(header[18:]), bytestream(body)),
% (
bytestream(header[:16]),
bytestream(header[16:18]),
bytestream(header[18:]),
bytestream(body),
),
)
)
self.send(header + body)
Expand All @@ -328,7 +343,12 @@ class BGPHandler(asyncore.dispatcher_with_send):
% (
self.counter,
'%s:%s:%s:%s'
% (bytestream(header[:16]), bytestream(header[16:18]), bytestream(header[18:]), bytestream(body)),
% (
bytestream(header[:16]),
bytestream(header[16:18]),
bytestream(header[18:]),
bytestream(body),
),
)
)
return
Expand All @@ -352,11 +372,17 @@ class BGPHandler(asyncore.dispatcher_with_send):
self.announce(
'received %d (%1s%s):' % (self.counter, self.options['letter'], self.step),
'%s:%s:%s:%s'
% (announcement[:32], announcement[32:36], announcement[36:38], announcement[38:]),
% (
announcement[:32],
announcement[32:36],
announcement[36:38],
announcement[38:],
),
)
else:
self.announce(
'received %d (%1s%s):' % (self.counter, self.options['letter'], self.step), announcement
'received %d (%1s%s):' % (self.counter, self.options['letter'], self.step),
announcement,
)
self.check_signal()
else:
Expand All @@ -380,7 +406,12 @@ class BGPHandler(asyncore.dispatcher_with_send):
if message.startswith('F' * 32):
self.announce(
' %s:%s:%s:%s'
% (message[:32], message[32:36], message[36:38], message[38:])
% (
message[:32],
message[32:36],
message[36:38],
message[38:],
)
)
else:
self.announce(' %s' % message)
Expand All @@ -389,7 +420,12 @@ class BGPHandler(asyncore.dispatcher_with_send):
if message.startswith('F' * 32):
self.announce(
'expected : %s:%s:%s:%s'
% (message[:32], message[32:36], message[36:38], message[38:])
% (
message[:32],
message[32:36],
message[36:38],
message[38:],
)
)
else:
self.announce('expected : %s' % message)
Expand Down Expand Up @@ -520,7 +556,7 @@ class BGPServer(asyncore.dispatcher):
pair = self.accept()
if pair is not None:
sock, addr = pair
handler = BGPHandler(sock).setup(*addr[:2], messages=messages, options=self.options)
BGPHandler(sock).setup(*addr[:2], messages=messages, options=self.options)


def drop():
Expand Down Expand Up @@ -556,20 +592,22 @@ def main():
flushed(
'a list of expected route announcement/withdrawl in the format <number>:announce:<ipv4-route> <number>:withdraw:<ipv4-route> <number>:raw:<exabgp hex dump : separated>'
)
flushed('for example:', sys.argv[0], '1:announce:10.0.0.0/8 1:announce:192.0.2.0/24 2:withdraw:10.0.0.0/8 ')
flushed(
'for example:',
sys.argv[0],
'1:announce:10.0.0.0/8 1:announce:192.0.2.0/24 2:withdraw:10.0.0.0/8 ',
)
flushed('routes with the same <number> can arrive in any order')
sys.exit(1)

options = {'sink': False, 'echo': False, 'port': int(port), 'messages': []}

for arg in sys.argv[1:]:
if arg == '--sink':
messages = []
options['sink'] = True
continue

if arg == '--echo':
messages = []
options['echo'] = True
continue

Expand Down Expand Up @@ -598,7 +636,7 @@ def main():
BGPServer('127.0.0.1', options)
try:
BGPServer('::1', options)
except:
except Exception:
# does not work on travis-ci
pass
drop()
Expand Down
Loading

0 comments on commit 47a7dab

Please sign in to comment.