- Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathreadme.py
More file actions
Latest commit
76 lines (57 loc) · 2.63 KB
/
Copy pathreadme.py
File metadata and controls
76 lines (57 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
importlogging
importre
frompathlibimportPath
fromtypingimportTYPE_CHECKING
ifTYPE_CHECKING:
from .versionsimportBuildVersion, SupportedVersion
logger=logging.getLogger("dpn")
def_format_md_table(columns: list[str], rows: list[list[str]]) ->str:
head=f"{' | '.join(columns)}\n{' | '.join(['---'] *len(columns))}"
body="\n".join([" | ".join(row) forrowinrows])
returnf"{head}\n{body}\n"
def_replace(name: str, replacement: str, document: str) ->str:
start=f"<!-- {name}_START -->\n"
end=f"<!-- {name}_END -->"
repl=f"{start}\n{replacement}\n{end}"
returnre.sub(f"{start}(.*?){end}", repl, document, flags=re.MULTILINE|re.DOTALL)
defupdate_dynamic_readme(
versions: list[BuildVersion],
python_versions: list[SupportedVersion],
node_versions: list[SupportedVersion],
dry_run: bool=False,
) ->None:
"""Read out current README, format fresh README, write back possible changes"""
readme_path=Path("README.md")
readme=readme_path.read_text()
readme_new=format_readme(versions, python_versions, node_versions, readme)
ifreadme==readme_new:
logger.debug("Regenerated readme matches existing")
return
ifnotdry_run:
readme_path.write_text(readme_new)
else:
print(readme_new)
defformat_readme(
versions: list[BuildVersion],
python_versions: list[SupportedVersion],
node_versions: list[SupportedVersion],
readme: str,
) ->str:
"""Format fresh README based on passed in version. Replaces the whole table with new versions."""
tags_table=format_tags(versions)
readme_fresh=_replace("TAGS", tags_table, readme)
supported_versions_table=format_supported_versions(python_versions, node_versions)
return_replace("SUPPORTED_VERSIONS", supported_versions_table, readme_fresh)
defformat_tags(versions: list[BuildVersion]) ->str:
headings= ["Tag", "Python version", "Node.js version", "Distro"]
rows= [[f"`{v.key}`", v.python_canonical, v.nodejs_canonical, v.distro] forvinversions]
return_format_md_table(headings, rows)
defformat_supported_versions(
python_versions: list[SupportedVersion],
node_versions: list[SupportedVersion],
) ->str:
def_as_rows(versions: list[SupportedVersion]) ->list[list[str]]:
return [[ver.version, ver.start, ver.end] forverinsorted(versions, key=lambdax: x.start, reverse=True)]
python_table=_format_md_table(["Python version", "Start", "End"], _as_rows(python_versions))
node_table=_format_md_table(["Node.js version", "Start", "End"], _as_rows(node_versions))
returnf"{python_table}\n{node_table}"