update_proposal_list.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. out.write("- [%s](%s)\n" % (title, filename))
  29. toc = out.getvalue()
  30. # Replace the README content if needed.
  31. readme_path = os.path.join(proposals_path, "README.md")
  32. with open(readme_path) as f:
  33. old_content = f.read()
  34. proposals_re = re.compile(
  35. r"(.*<!-- proposals -->)(?:.*)(<!-- endproposals -->)",
  36. re.DOTALL | re.MULTILINE,
  37. )
  38. if not proposals_re.match(old_content):
  39. print(
  40. "ERROR: proposals/README.md is missing the <!-- proposals --> ... "
  41. "<!-- endproposals --> marker."
  42. )
  43. sys.exit(1)
  44. new_content = proposals_re.sub(r"\1\n%s\n\2" % toc, old_content)
  45. if old_content != new_content:
  46. print("Updating proposals/README.md")
  47. with open(readme_path, "w") as f:
  48. f.write(new_content)