|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Print a .md table of stage variables that are set in the environment. |
| 4 | +# Sorted alphabetically, description truncated to fit 80 char lines. |
| 5 | +# No external dependencies: parses variables.yaml line by line. |
| 6 | +# |
| 7 | +import os |
| 8 | +import re |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def parse_variables(yaml_path): |
| 13 | + """Parse variables.yaml into {name: {description, stages}}.""" |
| 14 | + variables = {} |
| 15 | + current_var = None |
| 16 | + current_field = None |
| 17 | + |
| 18 | + with open(yaml_path) as f: |
| 19 | + for line in f: |
| 20 | + # Top-level variable: "VAR_NAME:" |
| 21 | + m = re.match(r"^(\w+):\s*$", line) |
| 22 | + if m: |
| 23 | + current_var = m.group(1) |
| 24 | + variables[current_var] = {"description": "", "stages": []} |
| 25 | + current_field = None |
| 26 | + continue |
| 27 | + |
| 28 | + if current_var is None: |
| 29 | + continue |
| 30 | + |
| 31 | + # Field: " description: >" or " stages:" |
| 32 | + m = re.match(r"^ (\w+):", line) |
| 33 | + if m: |
| 34 | + current_field = m.group(1) |
| 35 | + continue |
| 36 | + |
| 37 | + # List item under stages: " - name" |
| 38 | + m = re.match(r"^ - (\S+)", line) |
| 39 | + if m and current_field == "stages": |
| 40 | + variables[current_var]["stages"].append(m.group(1)) |
| 41 | + continue |
| 42 | + |
| 43 | + # Continuation of description |
| 44 | + if current_field == "description": |
| 45 | + variables[current_var]["description"] += " " + line.strip() |
| 46 | + |
| 47 | + return variables |
| 48 | + |
| 49 | + |
| 50 | +def main(): |
| 51 | + stage = sys.argv[1] |
| 52 | + dir_path = os.path.dirname(os.path.realpath(__file__)) |
| 53 | + yaml_path = os.path.join(dir_path, "variables.yaml") |
| 54 | + variables = parse_variables(yaml_path) |
| 55 | + |
| 56 | + rows = [] |
| 57 | + for key in sorted(variables): |
| 58 | + v = variables[key] |
| 59 | + stages = v["stages"] |
| 60 | + if not stages: |
| 61 | + continue |
| 62 | + if "All stages" not in stages and stage not in stages: |
| 63 | + continue |
| 64 | + if key not in os.environ: |
| 65 | + continue |
| 66 | + desc = v["description"].strip() |
| 67 | + rows.append((key, os.environ[key], desc)) |
| 68 | + |
| 69 | + if rows: |
| 70 | + w0 = max(len(r[0]) for r in rows) |
| 71 | + w1 = max(len(r[1]) for r in rows) |
| 72 | + w0 = max(w0, len("Variable")) |
| 73 | + w1 = max(w1, len("Value")) |
| 74 | + |
| 75 | + def fmt(c0, c1, c2): |
| 76 | + prefix = f"| {c0:<{w0}} | {c1:<{w1}} |" |
| 77 | + remaining = 80 - len(prefix) - 1 |
| 78 | + if remaining >= 4: |
| 79 | + max_desc = remaining - 3 |
| 80 | + if len(c2) > max_desc: |
| 81 | + c2 = c2[: max_desc - 3] + "..." |
| 82 | + return f"{prefix} {c2:<{max_desc}} |" |
| 83 | + return f"{prefix} |" |
| 84 | + |
| 85 | + header = fmt("Variable", "Value", "Description") |
| 86 | + print(header) |
| 87 | + print(re.sub(r"[^|]", "-", header)) |
| 88 | + for var, val, desc in rows: |
| 89 | + print(fmt(var, val, desc)) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments