| 1 |
#!/usr/bin/python3 |
| 2 |
import argparse |
| 3 |
import logging |
| 4 |
import subprocess |
| 5 |
import os |
| 6 |
import re |
| 7 |
import sys |
| 8 |
|
| 9 |
|
| 10 |
log = logging.getLogger("document-a38") |
| 11 |
|
| 12 |
|
| 13 |
class Fail(Exception): |
| 14 |
pass |
| 15 |
|
| 16 |
|
| 17 |
def sample_output(cmd, max_lines=None, max_line_length=None, returncode=0): |
| 18 |
res = subprocess.run("./" + cmd, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) |
| 19 |
if res.returncode != returncode: |
| 20 |
raise RuntimeError(f"{cmd} return code is {res.returncode} instead of {returncode}") |
| 21 |
lines = [] |
| 22 |
for idx, line in enumerate(res.stdout.splitlines()): |
| 23 |
if max_lines is not None and idx >= max_lines: |
| 24 |
lines.append("…") |
| 25 |
break |
| 26 |
if max_line_length is not None and len(line) > max_line_length: |
| 27 |
lines.append(line[:max_line_length] + "…") |
| 28 |
else: |
| 29 |
lines.append(line) |
| 30 |
return lines |
| 31 |
|
| 32 |
|
| 33 |
def generate_output(mo): |
| 34 |
cmd = mo.group("cmd") |
| 35 |
output = mo.group("output") |
| 36 |
returncode = mo.group("result") |
| 37 |
if returncode is not None: |
| 38 |
returncode = int(returncode) |
| 39 |
else: |
| 40 |
returncode = 0 |
| 41 |
|
| 42 |
# Autodetect max_lines and max_line_length |
| 43 |
lines = output.splitlines() |
| 44 |
|
| 45 |
max_lines = None |
| 46 |
if lines[-1] == "…": |
| 47 |
max_lines = len(lines) - 1 |
| 48 |
|
| 49 |
max_line_length = None |
| 50 |
for l in lines: |
| 51 |
if len(l) > 1 and l[-1] == "…": |
| 52 |
max_line_length = len(l) - 1 |
| 53 |
break |
| 54 |
|
| 55 |
lines = [ |
| 56 |
"```text", |
| 57 |
f"$ {cmd}", |
| 58 |
] |
| 59 |
lines.extend(sample_output(cmd, max_lines, max_line_length, returncode)) |
| 60 |
if returncode != 0: |
| 61 |
lines.append("$ echo $?") |
| 62 |
lines.append(str(returncode)) |
| 63 |
lines.append("```") |
| 64 |
|
| 65 |
return "\n".join(lines) + "\n" |
| 66 |
|
| 67 |
|
| 68 |
def process_md(fname): |
| 69 |
with open(fname, "rt") as fd: |
| 70 |
content = fd.read() |
| 71 |
|
| 72 |
# print(re.search( |
| 73 |
# r"```text\s*\n" |
| 74 |
# r"(?P<output>.+?)" |
| 75 |
# r"```" |
| 76 |
# , content, re.S)) |
| 77 |
|
| 78 |
new_content = re.sub( |
| 79 |
r"```text[ \t]*\n" |
| 80 |
r"\$ (?P<cmd>a38tool [^\n]+)\n" |
| 81 |
r"(?P<output>.+?)" |
| 82 |
r"(?:\$ echo \$\?\s*\n(?P<result>\d+)\s*\n)?" |
| 83 |
r"```[ \t]*\n", generate_output, content, flags=re.S) |
| 84 |
|
| 85 |
if new_content == content: |
| 86 |
return False |
| 87 |
|
| 88 |
with open(fname, "wt") as fd: |
| 89 |
fd.write(new_content) |
| 90 |
return True |
| 91 |
|
| 92 |
|
| 93 |
def main(): |
| 94 |
parser = argparse.ArgumentParser(description="Update a38tool examples in markdown documentation") |
| 95 |
parser.add_argument("--verbose", "-v", action="store_true", help="verbose output") |
| 96 |
parser.add_argument("--debug", action="store_true", help="debug output") |
| 97 |
|
| 98 |
args = parser.parse_args() |
| 99 |
|
| 100 |
log_format = "%(asctime)-15s %(levelname)s %(message)s" |
| 101 |
level = logging.WARN |
| 102 |
if args.debug: |
| 103 |
level = logging.DEBUG |
| 104 |
elif args.verbose: |
| 105 |
level = logging.INFO |
| 106 |
logging.basicConfig(level=level, stream=sys.stderr, format=log_format) |
| 107 |
|
| 108 |
for fn in os.listdir("."): |
| 109 |
if not fn.endswith(".md"): |
| 110 |
continue |
| 111 |
if process_md(fn): |
| 112 |
log.warning("%s: updated", fn) |
| 113 |
else: |
| 114 |
log.info("%s: unchanged", fn) |
| 115 |
|
| 116 |
|
| 117 |
if __name__ == "__main__": |
| 118 |
try: |
| 119 |
main() |
| 120 |
except Fail as e: |
| 121 |
print(e, file=sys.stderr) |
| 122 |
sys.exit(1) |
| 123 |
except Exception: |
| 124 |
log.exception("uncaught exception") |