From 9dadff85445b92a68aafe27079e3ea19e389732f Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Fri, 25 Oct 2024 16:01:01 +0100 Subject: [PATCH] ruff'ing the qa folder --- qa/bin/functional | 17 +++++++---- qa/bin/functional-3.6 | 26 +++++++++++++---- qa/sbin/bgp | 22 +++++++-------- qa/sbin/bgp-3.6 | 36 ++++++++++++++++++++---- qa/self/api/receive.py | 15 ++++++++-- qa/self/big/api-internet.py | 3 +- qa/self/flow/flow-change.py | 3 +- qa/self/flow/log-syslog.py | 6 ++-- qa/self/load/api-internet.py | 2 +- qa/self/operational/operational-print.py | 4 +-- qa/self/operational/operational-send.py | 1 - qa/self/simple/log-syslog.py | 6 ++-- qa/self/update/update-receive.py | 4 +-- qa/self/update/update-send.py | 1 - 14 files changed, 97 insertions(+), 49 deletions(-) diff --git a/qa/bin/functional b/qa/bin/functional index 957d7c3af..528b3f2d1 100755 --- a/qa/bin/functional +++ b/qa/bin/functional @@ -20,7 +20,7 @@ import subprocess from enum import Enum -INTERPRETER = os.environ.get('INTERPRETER', "") +INTERPRETER = os.environ.get('INTERPRETER', '') if not INTERPRETER: INTERPRETER = os.environ.get('__PYVENV_LAUNCHER__', sys.executable) @@ -392,7 +392,7 @@ class EncodingTests(Tests): def client(self, nick): if not self.enable_by_nick(nick): - sys.exit("no such test") + sys.exit('no such test') test = self.get_by_nick(nick) config = { @@ -403,7 +403,7 @@ class EncodingTests(Tests): 'exabgp_api_cli=false', 'exabgp_debug_rotate=true', 'exabgp_debug_configuration=true', - 'exabgp_tcp_bind=\'\'', + "exabgp_tcp_bind=''", 'exabgp_tcp_port=%d' % test.conf['port'], 'INTERPRETER=%s' % INTERPRETER, ] @@ -415,7 +415,7 @@ class EncodingTests(Tests): def server(self, nick): if not self.enable_by_nick(nick): - sys.exit("no such test") + sys.exit('no such test') test = self.get_by_nick(nick) config = { @@ -580,7 +580,14 @@ class DecodingTests(Tests): if message == 'open': cmd = [Path.EXABGP, 'decode', '--%s' % test.conf['type'], test.conf['packet']] elif message in ['update', 'nlri']: - cmd = [Path.EXABGP, 'decode', '-f', test.conf['family'], '--%s' % test.conf['type'], test.conf['packet']] + cmd = [ + Path.EXABGP, + 'decode', + '-f', + test.conf['family'], + '--%s' % test.conf['type'], + test.conf['packet'], + ] else: raise ValueError(f'invalid message type: {message}') test.run(cmd) diff --git a/qa/bin/functional-3.6 b/qa/bin/functional-3.6 index ebc2a72f5..31fc3f47a 100755 --- a/qa/bin/functional-3.6 +++ b/qa/bin/functional-3.6 @@ -123,7 +123,15 @@ class CI(dict): def make(cls): for filename in Path.ALL_CI: name, extension = os.path.splitext(filename.split('/')[-1]) - if name in ['api-reload', 'api-notification', 'conf-ebgp', 'conf-ipself6', 'conf-cap-software-version', 'conf-hostname', 'api-rr']: + if name in [ + 'api-reload', + 'api-notification', + 'conf-ebgp', + 'conf-ipself6', + 'conf-cap-software-version', + 'conf-hostname', + 'api-rr', + ]: continue nick = Identifier.get(name) with open(filename, 'r') as reader: @@ -308,7 +316,11 @@ class Command(dict): @classmethod def explain(cls, index): print( - EXPLAIN % {'client': cls.client(index), 'server': cls.server(index),} + EXPLAIN + % { + 'client': cls.client(index), + 'server': cls.server(index), + } ) sys.exit(1) @@ -328,7 +340,7 @@ class Command(dict): 'exabgp_api_cli=false', 'exabgp_debug_rotate=true', 'exabgp_debug_configuration=true', - 'exabgp_tcp_bind=\'\'', + "exabgp_tcp_bind=''", 'exabgp_tcp_port=%d' % test['port'], 'INTERPRETER=%s ' % os.environ.get('__PYVENV_LAUNCHER__', sys.executable), ] @@ -349,7 +361,11 @@ class Command(dict): sys.exit('you need to have root privileges to bind to port 79') config = { - 'env': ' \\\n '.join(['exabgp_tcp_port=%d' % test['port'],]), + 'env': ' \\\n '.join( + [ + 'exabgp_tcp_port=%d' % test['port'], + ] + ), 'interpreter': os.environ.get('__PYVENV_LAUNCHER__', sys.executable), 'bgp': Path.BGP, 'msg': test['msg'], @@ -523,4 +539,4 @@ if __name__ == '__main__': if vars(parsed): parsed.func(parsed) else: - parser.print_help() \ No newline at end of file + parser.print_help() diff --git a/qa/sbin/bgp b/qa/sbin/bgp index 7cc220eaf..6b0db07d5 100755 --- a/qa/sbin/bgp +++ b/qa/sbin/bgp @@ -68,7 +68,7 @@ def print_payload(prefix, header, body): def print_rule(prefix, rule): - print_tab(prefix,f'{rule[:32]}:{rule[32:36]}:{rule[36:38]}:{rule[38:]}') + print_tab(prefix, f'{rule[:32]}:{rule[32:36]}:{rule[36:38]}:{rule[38:]}') def print_tab(prefix, rule): @@ -136,7 +136,7 @@ def find_exabgp_pid(): processes = [] cmdlines = [] - for line in os.popen("/bin/ps x"): + for line in os.popen('/bin/ps x'): low = line.strip().lower() if not low: continue @@ -374,19 +374,19 @@ class Message: open = ( self.header + self.body[0:1] - + pack("!H", asn) + + pack('!H', asn) + self.body[3:8] - + pack("!H", byte_id)[1:] + + pack('!H', byte_id)[1:] + self.body[9:10] + opt_params[: offset + 2 + 2] - + pack("!I", asn) + + pack('!I', asn) + opt_params[offset + 2 + 2 + 4 :] ) return self._add_capa66(add_capa66, open) offset += param_len + 2 # No "Support for 4 octet AS number capability" found simply replace the 16-bit ASN number field. - open = self.header + self.body[0:1] + pack("!H", asn) + self.body[3:8] + pack("!H", byte_id)[1:] + self.body[9:] + open = self.header + self.body[0:1] + pack('!H', asn) + self.body[3:8] + pack('!H', byte_id)[1:] + self.body[9:] return self._add_capa66(add_capa66, open) @@ -506,7 +506,7 @@ class Checker(object): if msg.is_route_refresh(): for header, body in self.updates_seen: print_payload('rr sent', header, body) - writer.write(Message(header,body).packet()) + writer.write(Message(header, body).packet()) writer.write(Message.keepalive()) def perform_actions_if_required(self, writer, msg): @@ -542,7 +542,7 @@ class Checker(object): if check == received: self.messages.remove(check) if msg.is_update() and not msg.is_eor(): - self.updates_seen.append((msg.header,msg.body)) + self.updates_seen.append((msg.header, msg.body)) self.perform_actions_if_required(writer, msg) return True @@ -628,7 +628,6 @@ class BGPProtocol(asyncio.Protocol): sys.exit(1) self.writer.close() - def handle_open(self, msg): option_asn = self.service.options['asn'] option_capa66 = self.service.options['send-unknown-capability'] @@ -669,7 +668,7 @@ class BGPProtocol(asyncio.Protocol): return FSM.MSG if self.service.options['echo']: - print_payload(f'echo\'d #{self.counter}', header, body) + print_payload(f"echo'd #{self.counter}", header, body) self.writer.write(header + body) return FSM.MSG @@ -687,6 +686,7 @@ class BGPProtocol(asyncio.Protocol): return FSM.MSG + def parse_cmdline(): port = os.environ.get('exabgp.tcp.port', os.environ.get('exabgp_tcp_port', '179')) @@ -793,7 +793,7 @@ async def main(options, checker, queue): port = options['port'] sock.bind((host, port)) except PermissionError as exc: - flushed(f"could not bind to {host}:{port}") + flushed(f'could not bind to {host}:{port}') flushed(exc) sys.exit(1) diff --git a/qa/sbin/bgp-3.6 b/qa/sbin/bgp-3.6 index 0f155b168..a5bb6347d 100755 --- a/qa/sbin/bgp-3.6 +++ b/qa/sbin/bgp-3.6 @@ -59,7 +59,13 @@ def cdr_to_length(cidr): class BGPHandler(asyncore.dispatcher_with_send): counter = 0 - keepalive = bytearray([0xFF,] * 16 + [0x0, 0x13, 0x4]) + keepalive = bytearray( + [ + 0xFF, + ] + * 16 + + [0x0, 0x13, 0x4] + ) _name = { b'\x01': 'OPEN', @@ -79,7 +85,7 @@ class BGPHandler(asyncore.dispatcher_with_send): processes = [] - for line in os.popen("/bin/ps x"): + for line in os.popen('/bin/ps x'): low = line.strip().lower() if not low: continue @@ -250,9 +256,14 @@ class BGPHandler(asyncore.dispatcher_with_send): if self.options['send-default-route']: self.send( bytearray( - [0xFF,] * 16 + [ + 0xFF, + ] + * 16 + [0x00, 0x31] - + [0x02,] + + [ + 0x02, + ] + [0x00, 0x00] + [0x00, 0x15] + [] @@ -376,7 +387,10 @@ class BGPHandler(asyncore.dispatcher_with_send): elif self.messages: message = self.messages[0].upper() if message.startswith('F' * 32): - self.announce('expected : %s:%s:%s:%s' % (message[:32], message[32:36], message[36:38], message[38:])) + self.announce( + 'expected : %s:%s:%s:%s' + % (message[:32], message[32:36], message[36:38], message[38:]) + ) else: self.announce('expected : %s' % message) else: @@ -401,7 +415,17 @@ class BGPHandler(asyncore.dispatcher_with_send): if self.options['send-notification']: notification = b'closing session because we can' self.send( - bytearray([0xFF,] * 16 + [0x00, 19 + 2 + len(notification)] + [0x03] + [0x06] + [0x00]) + notification + bytearray( + [ + 0xFF, + ] + * 16 + + [0x00, 19 + 2 + len(notification)] + + [0x03] + + [0x06] + + [0x00] + ) + + notification ) _decoder = { diff --git a/qa/self/api/receive.py b/qa/self/api/receive.py index d816dd857..5f151ea07 100755 --- a/qa/self/api/receive.py +++ b/qa/self/api/receive.py @@ -3,7 +3,10 @@ import sys match = { - 0: ['neighbor 127.0.0.1 update start',] * 20, + 0: [ + 'neighbor 127.0.0.1 update start', + ] + * 20, 1: [ 'neighbor 127.0.0.1 announced route %d%d.0.0.0/8 next-hop 1.1.1.1 origin igp as-path [ 1 2 3 4 ] med 100' % (number, number) @@ -25,8 +28,14 @@ (25, 65, 'l2vpn vpls'), ) ], - 2: ['neighbor 127.0.0.1 update end',] * 20, - 3: ['',] * 20, + 2: [ + 'neighbor 127.0.0.1 update end', + ] + * 20, + 3: [ + '', + ] + * 20, } count = 0 diff --git a/qa/self/big/api-internet.py b/qa/self/big/api-internet.py index 87867cc77..933fb0d91 100755 --- a/qa/self/big/api-internet.py +++ b/qa/self/big/api-internet.py @@ -2,7 +2,6 @@ import sys import time -import random def write(data): @@ -14,7 +13,7 @@ def main(): count = 0 ip = {} - nexthop = "1.2.3.4" + nexthop = '1.2.3.4' for ip1 in range(0, 223): generated = '%d.0.0.0/8' % (ip1) diff --git a/qa/self/flow/flow-change.py b/qa/self/flow/flow-change.py index 58b1b12a2..070357f25 100755 --- a/qa/self/flow/flow-change.py +++ b/qa/self/flow/flow-change.py @@ -1,11 +1,10 @@ #!/usr/bin/env python3 from __future__ import print_function -import os import sys import time -flow = "flow route { match { source 10.0.0.1/32; destination 10.0.0.2/32; destination-port =3128; protocol tcp; } then { discard; } }" +flow = 'flow route { match { source 10.0.0.1/32; destination 10.0.0.2/32; destination-port =3128; protocol tcp; } then { discard; } }' # When the parent dies we are seeing continual newlines, so we only access so many before stopping counter = 1 diff --git a/qa/self/flow/log-syslog.py b/qa/self/flow/log-syslog.py index de7535d10..32bbb3a6f 100755 --- a/qa/self/flow/log-syslog.py +++ b/qa/self/flow/log-syslog.py @@ -8,10 +8,10 @@ def _prefixed(level, message): now = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()) - return "%s %-8s %-6d %s" % (now, level, os.getpid(), message) + return '%s %-8s %-6d %s' % (now, level, os.getpid(), message) -syslog.openlog("ExaBGP") +syslog.openlog('ExaBGP') # When the parent dies we are seeing continual newlines, so we only access so many before stopping counter = 0 @@ -19,7 +19,7 @@ def _prefixed(level, message): while True: try: line = sys.stdin.readline().strip() - if line == "": + if line == '': counter += 1 if counter > 100: break diff --git a/qa/self/load/api-internet.py b/qa/self/load/api-internet.py index 5ad65a989..d6c27d558 100755 --- a/qa/self/load/api-internet.py +++ b/qa/self/load/api-internet.py @@ -13,7 +13,7 @@ def write(data): def main(): if len(sys.argv) < 2: - print("%s ") + print('%s ') sys.exit(1) initial = sys.argv[1] diff --git a/qa/self/operational/operational-print.py b/qa/self/operational/operational-print.py index 67c292e96..3cc2d1305 100755 --- a/qa/self/operational/operational-print.py +++ b/qa/self/operational/operational-print.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -import os import sys -import time # When the parent dies we are seeing continual newlines, so we only access so many before stopping counter = 0 @@ -10,7 +8,7 @@ while True: try: line = sys.stdin.readline().strip() - if line == "": + if line == '': counter += 1 if counter > 100: break diff --git a/qa/self/operational/operational-send.py b/qa/self/operational/operational-send.py index c0ec5f418..0818bb17c 100755 --- a/qa/self/operational/operational-send.py +++ b/qa/self/operational/operational-send.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 from __future__ import print_function -import os import sys import time diff --git a/qa/self/simple/log-syslog.py b/qa/self/simple/log-syslog.py index 41646e431..7a2e5d697 100755 --- a/qa/self/simple/log-syslog.py +++ b/qa/self/simple/log-syslog.py @@ -8,10 +8,10 @@ def _prefixed(level, message): now = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()) - return "%s %-8s %-6d %s" % (now, level, os.getpid(), message) + return '%s %-8s %-6d %s' % (now, level, os.getpid(), message) -syslog.openlog("ExaBGP") +syslog.openlog('ExaBGP') # When the parent dies we are seeing continual newlines, so we only access so many before stopping counter = 0 @@ -19,7 +19,7 @@ def _prefixed(level, message): while True: try: line = sys.stdin.readline().strip() - if line == "": + if line == '': counter += 1 if counter > 100: break diff --git a/qa/self/update/update-receive.py b/qa/self/update/update-receive.py index 67c292e96..3cc2d1305 100755 --- a/qa/self/update/update-receive.py +++ b/qa/self/update/update-receive.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 -import os import sys -import time # When the parent dies we are seeing continual newlines, so we only access so many before stopping counter = 0 @@ -10,7 +8,7 @@ while True: try: line = sys.stdin.readline().strip() - if line == "": + if line == '': counter += 1 if counter > 100: break diff --git a/qa/self/update/update-send.py b/qa/self/update/update-send.py index 0d102027b..4c489d826 100755 --- a/qa/self/update/update-send.py +++ b/qa/self/update/update-send.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 from __future__ import print_function -import os import sys import time