summaryrefslogtreecommitdiffstats
path: root/documentation/tools/check-glossaries
diff options
context:
space:
mode:
authorAntonin Godard <antonin.godard@bootlin.com>2025-07-29 13:30:02 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2025-08-08 23:41:11 +0100
commitdc768a3d9d6df0cd048bf7ded4773372929f1675 (patch)
tree307e48ca1d172946f4962b0e2d0e35d4ae8efbea /documentation/tools/check-glossaries
parent6217bbbe7e030fed93c302fd9826e7bb73a217ce (diff)
downloadpoky-dc768a3d9d6df0cd048bf7ded4773372929f1675.tar.gz
Add a script to validate documentation glossaries
Instead of tracking the glossary manually, add a small script that checks if it is properly sorted. Add two comments between the start and end of the glossary for the script to know where it's located. The script also checks if the variables are properly sorted. It uses difflib and returns the diff if there's a difference between the unsorted and sorted list. Messages beginning with "WARNING:" are reported by the Autobuilder, which is the reason for this format. (From yocto-docs rev: 416d50c0c322eb88bf13353a198db7211e4d665a) Signed-off-by: Antonin Godard <antonin.godard@bootlin.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'documentation/tools/check-glossaries')
-rwxr-xr-xdocumentation/tools/check-glossaries90
1 files changed, 90 insertions, 0 deletions
diff --git a/documentation/tools/check-glossaries b/documentation/tools/check-glossaries
new file mode 100755
index 0000000000..b5dfe834e5
--- /dev/null
+++ b/documentation/tools/check-glossaries
@@ -0,0 +1,90 @@
1#!/usr/bin/env python3
2
3import argparse
4import difflib
5import os
6import re
7
8from pathlib import Path
9
10
11def parse_arguments() -> argparse.Namespace:
12 parser = argparse.ArgumentParser(description="Print supported distributions")
13
14 parser.add_argument("-d", "--docs-dir",
15 type=Path,
16 default=Path(os.path.dirname(os.path.realpath(__file__))) / "documentation",
17 help="Path to documentation/ directory in yocto-docs")
18
19 return parser.parse_args()
20
21
22glossaries = (
23 'ref-manual/variables.rst',
24 'ref-manual/terms.rst',
25)
26
27
28def main():
29
30 args = parse_arguments()
31 in_glossary = False
32 # Pattern to match:
33 # :term:`A <ABIEXTENSION>` :term:`B` :term:`C <CACHE>`
34 glossary_re = re.compile(r":term:`(?P<letter>[A-Z]{1})( <(?P<varname>[A-Z_]+)>)?`")
35 entry_re = re.compile(r"^ :term:`(?P<entry>.+)`\s*$")
36
37 for rst in glossaries:
38
39 glossary = {}
40 rst_path = Path(args.docs_dir) / rst
41
42 with open(rst_path, "r") as f:
43 for line in f.readlines():
44 if "check_glossary_begin" in line:
45 in_glossary = True
46 continue
47 if in_glossary:
48 for m in re.finditer(glossary_re, line.strip()):
49 letter = m.group("letter")
50 varname = m.group("varname")
51 if varname is None:
52 varname = letter
53 glossary[letter] = varname
54 if "check_glossary_end" in line:
55 in_glossary = False
56 break
57
58 entries = []
59
60 with open(rst_path, "r") as f:
61 for line in f.readlines():
62 m = re.match(entry_re, line)
63 if m:
64 entries.append(m.group("entry"))
65
66 # We lower here because underscore (_) come before lowercase letters
67 # (the natural way) but after uppercase letters (which is not natural)
68 sorted_entries = sorted(entries, key=lambda t: t.lower())
69 diffs = list(difflib.unified_diff(entries,
70 sorted_entries,
71 fromfile="original_list",
72 tofile="sorted_list"))
73
74 if diffs:
75 print(f"WARNING: {rst}: entries are not properly sorted:")
76 print('\n'.join(diffs))
77
78 for letter in glossary:
79 try:
80 index = entries.index(glossary[letter])
81 except ValueError:
82 print(f"WARNING: {rst}: variable "
83 f"{glossary[letter]} in glossary does not exist")
84 if index > 0 and entries[index - 1].startswith(letter[0]):
85 print(f"WARNING: {rst}: The variable {glossary[letter]} shouldn't be in "
86 "the glossary.")
87
88
89if __name__ == "__main__":
90 main()