update_proposal_list.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 os
  9. import re
  10. import sys
  11. if __name__ == "__main__":
  12. # Read the proposal dir relative to this script.
  13. proposal_dir = os.path.realpath(
  14. os.path.join(os.path.dirname(sys.argv[0]), "../../proposals/")
  15. )
  16. print(proposal_dir)
  17. # Identify proposal titles in the file list.
  18. proposals = [
  19. "<!-- This list is updated by src/scripts/update_proposal_list.py. "
  20. "-->",
  21. "",
  22. ]
  23. error = False
  24. for file in sorted(os.listdir(proposal_dir)):
  25. file_match = re.match(r"^p([0-9]{4})\.md$", file)
  26. if not file_match:
  27. continue
  28. with open(os.path.join(proposal_dir, file)) as f:
  29. content = f.read()
  30. title = content.split("\n")[0]
  31. title_match = re.match(r"^# (.*)$", title)
  32. if not title_match:
  33. print("ERROR: %s doesn't have a title on the first line." % file)
  34. error = True
  35. proposals.append(
  36. "- [%s - %s](%s)" % (file_match[1], title_match[1], file)
  37. )
  38. decision_file = "p%s_decision.md" % file_match[1]
  39. if os.path.exists(os.path.join(proposal_dir, decision_file)):
  40. proposals.append(" - [Decision](%s)" % decision_file)
  41. # We print batched errors for usability, but still need to exit with
  42. # failure.
  43. if error:
  44. sys.exit(1)
  45. # Replace the README content if needed.
  46. readme_path = os.path.join(proposal_dir, "README.md")
  47. with open(readme_path) as f:
  48. old_content = f.read()
  49. proposals_re = re.compile(
  50. r"(.*<!-- proposals -->)(?:.*)(<!-- endproposals -->)",
  51. re.DOTALL | re.MULTILINE,
  52. )
  53. if not proposals_re.match(old_content):
  54. print(
  55. "ERROR: proposals/README.md is missing the <!-- proposals --> ... "
  56. "<!-- endproposals --> marker."
  57. )
  58. sys.exit(1)
  59. new_content = proposals_re.sub(
  60. r"\1\n%s\n\n\2" % "\n".join(proposals), old_content
  61. )
  62. if old_content != new_content:
  63. print("Updating proposals/README.md")
  64. with open(readme_path, "w") as f:
  65. f.write(new_content)