update_proposal_list.py 1.9 KB

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