tools: use f-string in frr-reload.py

Adopt f-string for printing log message, so that appropriate formatting can be
applied.

Signed-off-by: Giovanni Tataranni <g.tataranni@gmail.com>
This commit is contained in:
Giovanni Tataranni 2024-09-19 15:32:25 +02:00
parent a962ff7833
commit a09e949e48

View file

@ -95,7 +95,7 @@ class Vtysh(object):
output = self("configure") output = self("configure")
if "configuration is locked" in output.lower(): if "configuration is locked" in output.lower():
log.error("vtysh 'configure' returned\n%s\n" % (output)) log.error(f"vtysh 'configure' returned\n{output}\n")
return False return False
return True return True
@ -104,7 +104,7 @@ class Vtysh(object):
child = self._call(["-f", filename]) child = self._call(["-f", filename])
if child.wait() != 0: if child.wait() != 0:
raise VtyshException( raise VtyshException(
"vtysh (exec file) exited with status %d" % (child.returncode) f"vtysh (exec file) exited with status {child.returncode}"
) )
def mark_file(self, filename, stdin=None): def mark_file(self, filename, stdin=None):
@ -312,7 +312,7 @@ class Config(object):
The internal representation has been marked appropriately by passing it The internal representation has been marked appropriately by passing it
through vtysh with the -m parameter through vtysh with the -m parameter
""" """
log.info("Loading Config object from file %s", filename) log.info(f"Loading Config object from file {filename}")
file_output = self.vtysh.mark_file(filename) file_output = self.vtysh.mark_file(filename)
@ -649,7 +649,7 @@ class Config(object):
self.save_contexts(ctx_keys, cur_ctx_lines) self.save_contexts(ctx_keys, cur_ctx_lines)
# exit current context # exit current context
log.debug("LINE %-50s: exit context %-50s", line, ctx_keys) log.debug(f"LINE {line:<50}: exit context {' '.join(ctx_keys):<50}")
ctx_keys.pop() ctx_keys.pop()
cur_ctx_keywords.pop() cur_ctx_keywords.pop()
@ -664,7 +664,7 @@ class Config(object):
self.save_contexts(ctx_keys, cur_ctx_lines) self.save_contexts(ctx_keys, cur_ctx_lines)
# exit current context # exit current context
log.debug("LINE %-50s: exit context %-50s", line, ctx_keys) log.debug(f"LINE {line:<50}: exit context {' '.join(ctx_keys):<50}")
ctx_keys.pop() ctx_keys.pop()
cur_ctx_keywords.pop() cur_ctx_keywords.pop()
@ -695,17 +695,21 @@ class Config(object):
cur_ctx_keywords.append(v) cur_ctx_keywords.append(v)
cur_ctx_lines = [] cur_ctx_lines = []
log.debug("LINE %-50s: enter context %-50s", line, ctx_keys) log.debug(
f"LINE {line:<50}: enter context {' '.join(ctx_keys):<50}"
)
break break
if new_ctx: if new_ctx:
continue continue
if len(ctx_keys) == 0: if len(ctx_keys) == 0:
log.debug("LINE %-50s: single-line context", line) log.debug(f"LINE {line:<50}: single-line context")
self.save_contexts([line], []) self.save_contexts([line], [])
else: else:
log.debug("LINE %-50s: add to current context %-50s", line, ctx_keys) log.debug(
f"LINE {line:<50}: add to current context {' '.join(ctx_keys):<50}"
)
cur_ctx_lines.append(line) cur_ctx_lines.append(line)
# Save the context of the last one # Save the context of the last one
@ -1696,7 +1700,7 @@ def ignore_unconfigurable_lines(lines_to_add, lines_to_del):
] ]
] ]
): ):
log.info('"%s" cannot be removed' % (ctx_keys[-1],)) log.info(f'"{ctx_keys[-1]}" cannot be removed')
lines_to_del_to_del.append((ctx_keys, line)) lines_to_del_to_del.append((ctx_keys, line))
for ctx_keys, line in lines_to_del_to_del: for ctx_keys, line in lines_to_del_to_del:
@ -2090,26 +2094,26 @@ if __name__ == "__main__":
# Verify the new config file is valid # Verify the new config file is valid
if not os.path.isfile(args.filename): if not os.path.isfile(args.filename):
log.error("Filename %s does not exist" % args.filename) log.error(f"Filename {args.filename} does not exist")
sys.exit(1) sys.exit(1)
if not os.path.getsize(args.filename): if not os.path.getsize(args.filename):
log.error("Filename %s is an empty file" % args.filename) log.error(f"Filename {args.filename} is an empty file")
sys.exit(1) sys.exit(1)
# Verify that confdir is correct # Verify that confdir is correct
if not os.path.isdir(args.confdir): if not os.path.isdir(args.confdir):
log.error("Confdir %s is not a valid path" % args.confdir) log.error(f"Confdir {args.confdir} is not a valid path")
sys.exit(1) sys.exit(1)
# Verify that bindir is correct # Verify that bindir is correct
if not os.path.isdir(args.bindir) or not os.path.isfile(args.bindir + "/vtysh"): if not os.path.isdir(args.bindir) or not os.path.isfile(args.bindir + "/vtysh"):
log.error("Bindir %s is not a valid path to vtysh" % args.bindir) log.error(f"Bindir {args.bindir} is not a valid path to vtysh")
sys.exit(1) sys.exit(1)
# verify that the vty_socket, if specified, is valid # verify that the vty_socket, if specified, is valid
if args.vty_socket and not os.path.isdir(args.vty_socket): if args.vty_socket and not os.path.isdir(args.vty_socket):
log.error("vty_socket %s is not a valid path" % args.vty_socket) log.error(f"vty_socket {args.vty_socket} is not a valid path")
sys.exit(1) sys.exit(1)
# verify that the daemon, if specified, is valid # verify that the daemon, if specified, is valid
@ -2164,7 +2168,7 @@ if __name__ == "__main__":
) )
sys.exit(1) sys.exit(1)
log.info('Called via "%s"', str(args)) log.info(f'Called via "{args}"')
# Create a Config object from the config generated by newconf # Create a Config object from the config generated by newconf
newconf = Config(vtysh) newconf = Config(vtysh)
@ -2240,7 +2244,7 @@ if __name__ == "__main__":
if not vtysh.is_config_available() or not reload_ok: if not vtysh.is_config_available() or not reload_ok:
sys.exit(1) sys.exit(1)
log.debug("New Frr Config\n%s", newconf.get_lines()) log.debug(f"New Frr Config\n{newconf.get_lines()}")
# This looks a little odd but we have to do this twice...here is why # This looks a little odd but we have to do this twice...here is why
# If the user had this running bgp config: # If the user had this running bgp config:
@ -2282,7 +2286,7 @@ if __name__ == "__main__":
for x in range(2): for x in range(2):
running = Config(vtysh) running = Config(vtysh)
running.load_from_show_running(args.daemon) running.load_from_show_running(args.daemon)
log.debug("Running Frr Config (Pass #%d)\n%s", x, running.get_lines()) log.debug(f"Running Frr Config (Pass #{x})\n{running.get_lines()}")
(lines_to_add, lines_to_del) = compare_context_objects(newconf, running) (lines_to_add, lines_to_del) = compare_context_objects(newconf, running)
@ -2339,7 +2343,7 @@ if __name__ == "__main__":
# 'no ip ospf authentication message-digest 1.1.1.1' in # 'no ip ospf authentication message-digest 1.1.1.1' in
# our example above # our example above
# - Split that last entry by whitespace and drop the last word # - Split that last entry by whitespace and drop the last word
log.error("Failed to execute %s", " ".join(cmd)) log.error(f"Failed to execute {' '.join(cmd)}")
last_arg = cmd[-1].split(" ") last_arg = cmd[-1].split(" ")
if len(last_arg) <= 2: if len(last_arg) <= 2:
@ -2356,7 +2360,7 @@ if __name__ == "__main__":
new_last_arg = last_arg[0:-1] new_last_arg = last_arg[0:-1]
cmd[-1] = " ".join(new_last_arg) cmd[-1] = " ".join(new_last_arg)
else: else:
log.info('Executed "%s"', " ".join(cmd)) log.info(f'Executed "{" ".join(cmd)}"')
break break
if lines_to_add: if lines_to_add:
@ -2383,7 +2387,7 @@ if __name__ == "__main__":
) )
filename = args.rundir + "/reload-%s.txt" % random_string filename = args.rundir + "/reload-%s.txt" % random_string
log.info("%s content\n%s" % (filename, pformat(lines_to_configure))) log.info(f"{filename} content\n{pformat(lines_to_configure)}")
with open(filename, "w") as fh: with open(filename, "w") as fh:
for line in lines_to_configure: for line in lines_to_configure:
@ -2392,7 +2396,7 @@ if __name__ == "__main__":
try: try:
vtysh.exec_file(filename) vtysh.exec_file(filename)
except VtyshException as e: except VtyshException as e:
log.warning("frr-reload.py failed due to\n%s" % e.args) log.warning(f"frr-reload.py failed due to\n{e.args}")
reload_ok = False reload_ok = False
os.unlink(filename) os.unlink(filename)