check-glossaries 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. import argparse
  3. import difflib
  4. import os
  5. import re
  6. from pathlib import Path
  7. def parse_arguments() -> argparse.Namespace:
  8. parser = argparse.ArgumentParser(description="Print supported distributions")
  9. parser.add_argument("-d", "--docs-dir",
  10. type=Path,
  11. default=Path(os.path.dirname(os.path.realpath(__file__))) / "documentation",
  12. help="Path to documentation/ directory in yocto-docs")
  13. return parser.parse_args()
  14. glossaries = (
  15. 'ref-manual/variables.rst',
  16. 'ref-manual/terms.rst',
  17. )
  18. def main():
  19. args = parse_arguments()
  20. in_glossary = False
  21. # Pattern to match:
  22. # :term:`A <ABIEXTENSION>` :term:`B` :term:`C <CACHE>`
  23. glossary_re = re.compile(r":term:`(?P<letter>[A-Z]{1})( <(?P<varname>[A-Z_]+)>)?`")
  24. entry_re = re.compile(r"^ :term:`(?P<entry>.+)`\s*$")
  25. for rst in glossaries:
  26. glossary = {}
  27. rst_path = Path(args.docs_dir) / rst
  28. with open(rst_path, "r") as f:
  29. for line in f.readlines():
  30. if "check_glossary_begin" in line:
  31. in_glossary = True
  32. continue
  33. if in_glossary:
  34. for m in re.finditer(glossary_re, line.strip()):
  35. letter = m.group("letter")
  36. varname = m.group("varname")
  37. if varname is None:
  38. varname = letter
  39. glossary[letter] = varname
  40. if "check_glossary_end" in line:
  41. in_glossary = False
  42. break
  43. entries = []
  44. with open(rst_path, "r") as f:
  45. for line in f.readlines():
  46. m = re.match(entry_re, line)
  47. if m:
  48. entries.append(m.group("entry"))
  49. # We lower here because underscore (_) come before lowercase letters
  50. # (the natural way) but after uppercase letters (which is not natural)
  51. sorted_entries = sorted(entries, key=lambda t: t.lower())
  52. diffs = list(difflib.unified_diff(entries,
  53. sorted_entries,
  54. fromfile="original_list",
  55. tofile="sorted_list"))
  56. if diffs:
  57. print(f"WARNING: {rst}: entries are not properly sorted:")
  58. print('\n'.join(diffs))
  59. for letter in glossary:
  60. try:
  61. index = entries.index(glossary[letter])
  62. except ValueError:
  63. print(f"WARNING: {rst}: variable "
  64. f"{glossary[letter]} in glossary does not exist")
  65. if index > 0 and entries[index - 1].startswith(letter[0]):
  66. print(f"WARNING: {rst}: The variable {glossary[letter]} shouldn't be in "
  67. "the glossary.")
  68. if __name__ == "__main__":
  69. main()