update_proposal_list.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. """Updates the list of proposals in proposals/README.md."""
  3. __copyright__ = """
  4. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  5. Exceptions. See /LICENSE for license information.
  6. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. """
  8. import io
  9. import os
  10. import re
  11. import sys
  12. # To support direct runs, ensure the pythonpath has the repo root.
  13. _PYTHONPATH = os.path.realpath(os.path.join(os.path.dirname(__file__), "../.."))
  14. if _PYTHONPATH not in sys.path:
  15. sys.path.insert(0, _PYTHONPATH)
  16. from proposals.scripts import proposals
  17. if __name__ == "__main__":
  18. proposals_path = proposals.get_path()
  19. with io.StringIO() as out:
  20. out.write("<!-- Generated by ./scripts/update_proposal_list.py -->\n\n")
  21. results = out.getvalue()
  22. for title, filename in proposals.get_list(proposals_path):
  23. indent = ""
  24. if filename.endswith("decision.md"):
  25. indent = " "
  26. out.write("%s- [%s](%s)\n" % (indent, title, filename))
  27. toc = out.getvalue()
  28. # Replace the README content if needed.
  29. readme_path = os.path.join(proposals_path, "README.md")
  30. with open(readme_path) as f:
  31. old_content = f.read()
  32. proposals_re = re.compile(
  33. r"(.*<!-- proposals -->)(?:.*)(<!-- endproposals -->)",
  34. re.DOTALL | re.MULTILINE,
  35. )
  36. if not proposals_re.match(old_content):
  37. print(
  38. "ERROR: proposals/README.md is missing the <!-- proposals --> ... "
  39. "<!-- endproposals --> marker."
  40. )
  41. sys.exit(1)
  42. new_content = proposals_re.sub(r"\1\n%s\n\2" % toc, old_content)
  43. if old_content != new_content:
  44. print("Updating proposals/README.md")
  45. with open(readme_path, "w") as f:
  46. f.write(new_content)