update_proposal_list.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 importlib.util
  11. import re
  12. import sys
  13. # Do some extra work to support direct runs.
  14. try:
  15. from carbon.proposals.scripts import proposals
  16. except ImportError:
  17. proposals_spec = importlib.util.spec_from_file_location(
  18. "proposals", os.path.join(os.path.dirname(__file__), "proposals.py")
  19. )
  20. proposals = importlib.util.module_from_spec(proposals_spec)
  21. proposals_spec.loader.exec_module(proposals)
  22. if __name__ == "__main__":
  23. proposals_path = proposals.get_path()
  24. with io.StringIO() as out:
  25. out.write("<!-- Generated by ./scripts/update_proposal_list.py -->\n\n")
  26. results = out.getvalue()
  27. for title, filename in proposals.get_list(proposals_path):
  28. indent = ""
  29. if filename.endswith("decision.md"):
  30. indent = " "
  31. out.write("%s- [%s](%s)\n" % (indent, title, filename))
  32. toc = out.getvalue()
  33. # Replace the README content if needed.
  34. readme_path = os.path.join(proposals_path, "README.md")
  35. with open(readme_path) as f:
  36. old_content = f.read()
  37. proposals_re = re.compile(
  38. r"(.*<!-- proposals -->)(?:.*)(<!-- endproposals -->)",
  39. re.DOTALL | re.MULTILINE,
  40. )
  41. if not proposals_re.match(old_content):
  42. print(
  43. "ERROR: proposals/README.md is missing the <!-- proposals --> ... "
  44. "<!-- endproposals --> marker."
  45. )
  46. sys.exit(1)
  47. new_content = proposals_re.sub(r"\1\n%s\n\2" % toc, old_content)
  48. if old_content != new_content:
  49. print("Updating proposals/README.md")
  50. with open(readme_path, "w") as f:
  51. f.write(new_content)