From 47a7dabb1785674c113c74589af75d175a5cfb49 Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Fri, 25 Oct 2024 17:02:35 +0100 Subject: [PATCH] more code ruff'ing --- etc/exabgp/example-api-program.run | 2 +- etc/exabgp/run/api-blocklist.run | 18 ++++---- etc/exabgp/run/api-no-respawn-1.run | 1 - qa/bin/functional | 62 ++++++++++++++++++++++++---- qa/sbin/bgp | 32 +++++++++++---- qa/sbin/bgp-3.6 | 64 +++++++++++++++++++++++------ release | 4 +- tests/bgpls_test.py | 32 ++++++--------- 8 files changed, 155 insertions(+), 60 deletions(-) diff --git a/etc/exabgp/example-api-program.run b/etc/exabgp/example-api-program.run index 7878e34f8..ccd4fd1eb 100755 --- a/etc/exabgp/example-api-program.run +++ b/etc/exabgp/example-api-program.run @@ -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 diff --git a/etc/exabgp/run/api-blocklist.run b/etc/exabgp/run/api-blocklist.run index e8c4632d0..09e815d5f 100755 --- a/etc/exabgp/run/api-blocklist.run +++ b/etc/exabgp/run/api-blocklist.run @@ -8,7 +8,6 @@ import sys import errno import threading import time -import json import ipaddress import traceback import requests @@ -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}, @@ -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): @@ -200,7 +202,7 @@ 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 @@ -208,7 +210,7 @@ if __name__ == '__main__': 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 diff --git a/etc/exabgp/run/api-no-respawn-1.run b/etc/exabgp/run/api-no-respawn-1.run index 097d07ec7..0a10b4770 100755 --- a/etc/exabgp/run/api-no-respawn-1.run +++ b/etc/exabgp/run/api-no-respawn-1.run @@ -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', diff --git a/qa/bin/functional b/qa/bin/functional index 528b3f2d1..81a095a4d 100755 --- a/qa/bin/functional +++ b/qa/bin/functional @@ -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 @@ -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) @@ -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) @@ -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) @@ -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, @@ -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() diff --git a/qa/sbin/bgp b/qa/sbin/bgp index 6b0db07d5..c1e0bc248 100755 --- a/qa/sbin/bgp +++ b/qa/sbin/bgp @@ -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 @@ -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() @@ -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 .. @@ -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() diff --git a/qa/sbin/bgp-3.6 b/qa/sbin/bgp-3.6 index a5bb6347d..e6d4dc63a 100755 --- a/qa/sbin/bgp-3.6 +++ b/qa/sbin/bgp-3.6 @@ -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:'): @@ -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) @@ -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) @@ -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 @@ -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: @@ -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) @@ -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) @@ -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(): @@ -556,7 +592,11 @@ def main(): flushed( 'a list of expected route announcement/withdrawl in the format :announce: :withdraw: :raw:' ) - 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 can arrive in any order') sys.exit(1) @@ -564,12 +604,10 @@ def main(): for arg in sys.argv[1:]: if arg == '--sink': - messages = [] options['sink'] = True continue if arg == '--echo': - messages = [] options['echo'] = True continue @@ -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() diff --git a/release b/release index d71fae4be..b5ec40408 100755 --- a/release +++ b/release @@ -331,8 +331,8 @@ def generate_binary(args): if sys.version_info.minor >= 7: params['compressed'] = not debug - here = os.path.dirname(os.path.realpath(__file__)) - data = os.path.abspath(os.path.join(here, 'data')) + # here = os.path.dirname(os.path.realpath(__file__)) + # data = os.path.abspath(os.path.join(here, 'data')) zipapp.create_archive(**params) diff --git a/tests/bgpls_test.py b/tests/bgpls_test.py index 6ce06bc46..664d79bab 100644 --- a/tests/bgpls_test.py +++ b/tests/bgpls_test.py @@ -8,49 +8,41 @@ class TestTlvs(unittest.TestCase): - def test_ip_reach_ipv4( - self, - ): + def test_ip_reach_ipv4(self): data = b'\n\n\x00' tlv = IpReach.unpack(data, 3) - self.assertEqual(tlv.json(), '"ip-reachability-tlv": "10.0.0.0", "ip-reach-prefix": "10.0.0.0/10"') + self.assertEqual( + tlv.json(), + '"ip-reachability-tlv": "10.0.0.0", "ip-reach-prefix": "10.0.0.0/10"', + ) - def test_ip_reach_ipv6( - self, - ): + def test_ip_reach_ipv6(self): data = b'\x7f \x01\x07\x00\x00\x00\x80' tlv = IpReach.unpack(data, 4) self.assertEqual( - tlv.json(), '"ip-reachability-tlv": "2001:700:0:8000::", "ip-reach-prefix": "2001:700:0:8000::/127"' + tlv.json(), + '"ip-reachability-tlv": "2001:700:0:8000::", "ip-reach-prefix": "2001:700:0:8000::/127"', ) - def test_igp_tags( - self, - ): + def test_igp_tags(self): data = b'\x00\x00\xff\xfe' tlv = IgpTags.unpack(data) self.assertEqual(tlv.json(), '"igp-route-tags": [65534]') - def test_prefix_metric( - self, - ): + def test_prefix_metric(self): data = b'\x00\x00\x00\x14' tlv = PrefixMetric.unpack(data) self.assertEqual(tlv.json(), '"prefix-metric": 20') - def test_ospf_route_type( - self, - ): + def test_ospf_route_type(self): data = b'\x04' tlv = OspfRoute.unpack(data) self.assertEqual(tlv.json(), '"ospf-route-type": 4') class TestDescriptors(unittest.TestCase): - def test_node_descriptor( - self, - ): + def test_node_descriptor(self): data = b'\x02\x00\x00\x04\x00\x00\xff\xfd\x02\x01\x00\x04\x00\x00\x00\x00\x02\x03\x00\x04\nq?\xf0' igp_type = 3 descriptor, remain = NodeDescriptor.unpack(data, igp_type)