#!/usr/bin/env python3 # Rewrite the revhistory part of the document for vty reference (OS#4063) import os import shutil import sys xml_in = sys.argv[1] xml_out = os.environ["TEMPFILE"] git_date = os.environ["GIT_DATE"] revnumber = os.environ["REVNUMBER"] def get_author_initials(line): return line.split("")[1].split("")[0] def write_revhistory_block(h_out, author_initials): h_out.write("\n") h_out.write("\n") h_out.write(f"{revnumber}\n") h_out.write(f"{git_date}\n") h_out.write(f"{author_initials}\n") h_out.write("Automatically Generated VTY Reference\n") h_out.write("\n") h_out.write("\n") def set_revhistory(): """ Replace the part of the docbook that looks like the following, and copy all other lines. Just using python's xml library would be better, but it fails on docbook's custom DTDs. v1 18th September 2017 nh Initial """ before = 0 inside = 1 after = 2 pos = before author_initials = "" with open(xml_out, "w") as h_out: with open(xml_in, "r") as h_in: for line in h_in.readlines(): if pos == before: if "" in line: h_out.write(line.split("")[0] + "\n") pos = inside if pos in [before, after]: h_out.write(line) if pos == inside: if "" in line: author_initials = get_author_initials(line) if "" in line: write_revhistory_block(h_out, author_initials) h_out.write(line.split("")[1]) pos = after def main(): if xml_in.endswith("-vty-reference.xml"): print(f"Changing revhistory to {revnumber}, {git_date}...") set_revhistory() else: print("Copying without change of revhistory...") shutil.copyfile(xml_in, xml_out) main()