1
0

check-changelog-entries.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. # pylint: skip-file
  3. import pathlib
  4. import re
  5. import sys
  6. CODE_ROOT = pathlib.Path(__file__).resolve().parent.parent
  7. DOCS_PATH = CODE_ROOT / "doc"
  8. TESTS_INTEGRATION_FILES_PATH = CODE_ROOT / "tests" / "integration" / "files"
  9. CHANGELOG_ENTRIES_PATH = CODE_ROOT / "changelog"
  10. CHANGELOG_LIKE_RE = re.compile(r"([\d]+)\.([a-z]+)$")
  11. CHANGELOG_EXTENSIONS = ("removed", "deprecated", "changed", "fixed", "added")
  12. CHANGELOG_ENTRY_RE = re.compile(r"[\d]+\.({})$".format("|".join(CHANGELOG_EXTENSIONS)))
  13. def check_changelog_entries(files):
  14. exitcode = 0
  15. for entry in files:
  16. path = pathlib.Path(entry).resolve()
  17. # Does it look like a changelog entry
  18. if CHANGELOG_LIKE_RE.match(path.name) and not CHANGELOG_ENTRY_RE.match(
  19. path.name
  20. ):
  21. try:
  22. # Is this under doc/
  23. path.relative_to(DOCS_PATH)
  24. # Yes, carry on
  25. continue
  26. except ValueError:
  27. # No, resume the check
  28. pass
  29. try:
  30. # Is this under tests/integration/files
  31. path.relative_to(TESTS_INTEGRATION_FILES_PATH)
  32. # Yes, carry on
  33. continue
  34. except ValueError:
  35. # No, resume the check
  36. pass
  37. print(
  38. "The changelog entry '{}' should have one of the following extensions: {}.".format(
  39. path.relative_to(CODE_ROOT),
  40. ", ".join(repr(ext) for ext in CHANGELOG_EXTENSIONS),
  41. ),
  42. file=sys.stderr,
  43. flush=True,
  44. )
  45. exitcode = 1
  46. continue
  47. # Is it a changelog entry
  48. if not CHANGELOG_ENTRY_RE.match(path.name):
  49. # No? Carry on
  50. continue
  51. # Is the changelog entry in the right path?
  52. try:
  53. path.relative_to(CHANGELOG_ENTRIES_PATH)
  54. except ValueError:
  55. exitcode = 1
  56. print(
  57. "The changelog entry '{}' should be placed under '{}/', not '{}'".format(
  58. path.name,
  59. CHANGELOG_ENTRIES_PATH.relative_to(CODE_ROOT),
  60. path.relative_to(CODE_ROOT).parent,
  61. ),
  62. file=sys.stderr,
  63. flush=True,
  64. )
  65. sys.exit(exitcode)
  66. if __name__ == "__main__":
  67. check_changelog_entries(sys.argv[1:])