check_help.py 904 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/env python3
  2. import glob
  3. import os.path
  4. import re
  5. import subprocess
  6. USAGE_RE = re.compile(r"```.*?\nUsage:.*?```", re.MULTILINE | re.DOTALL)
  7. USAGE_IN_CMD_RE = re.compile(r"^Usage:.*", re.MULTILINE | re.DOTALL)
  8. HELP_CMD = "docker run --rm docker/compose:latest %s --help"
  9. for file in glob.glob("compose/reference/*.md"):
  10. with open(file) as f:
  11. data = f.read()
  12. if not USAGE_RE.search(data):
  13. print("Not a command:", file)
  14. continue
  15. subcmd = os.path.basename(file).replace(".md", "")
  16. if subcmd == "overview":
  17. continue
  18. print(f"Found {subcmd}: {file}")
  19. help_cmd = HELP_CMD % subcmd
  20. help = subprocess.check_output(help_cmd.split())
  21. help = help.decode("utf-8")
  22. help = USAGE_IN_CMD_RE.findall(help)[0]
  23. help = help.strip()
  24. data = USAGE_RE.sub(f"```none\n{help}\n```", data)
  25. with open(file, "w") as f:
  26. f.write(data)