Debian Codegraph @

scripts/check_version_in_basics_example.py

raw
1 """
2 Check that the rev value in the example from ``the_basics.md`` matches
3 the latest version of Black. This saves us from forgetting to update that
4 during the release process.
5 """
6
7 import os
8 import sys
9
10 import commonmark
11 from bs4 import BeautifulSoup
12
13
14 def main(changes: str, the_basics: str) -> None:
15 changes_html = commonmark.commonmark(changes)
16 changes_soup = BeautifulSoup(changes_html, "html.parser")
17 headers = changes_soup.find_all("h2")
18 tags = []
19 for header in headers:
20 text = header.string or ""
21 if text == "Unreleased":
22 continue
23 # Strip "Version " prefix if present
24 tags.append(text.removeprefix("Version "))
25 latest_tag = tags[0]
26
27 the_basics_html = commonmark.commonmark(the_basics)
28 the_basics_soup = BeautifulSoup(the_basics_html, "html.parser")
29 version_examples = [
30 code_block.string
31 for code_block in the_basics_soup.find_all(class_="language-console")
32 if "$ black --version" in code_block.string # type: ignore[operator]
33 ]
34
35 for tag in tags:
36 for version_example in version_examples:
37 if tag in version_example and tag != latest_tag: # type: ignore[operator]
38 print(
39 "Please set the version in the ``black --version`` "
40 "examples from ``the_basics.md`` to be the latest one.\n"
41 f"Expected {latest_tag}, got {tag}.\n"
42 )
43 sys.exit(1)
44
45
46 if __name__ == "__main__":
47 with open("CHANGES.md", encoding="utf-8") as fd:
48 changes = fd.read()
49 with open(
50 os.path.join("docs", "usage_and_configuration", "the_basics.md"),
51 encoding="utf-8",
52 ) as fd:
53 the_basics = fd.read()
54 main(changes, the_basics)