-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathupdate_readme.py
More file actions
58 lines (49 loc) · 1.41 KB
/
update_readme.py
File metadata and controls
58 lines (49 loc) · 1.41 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
"""
Updates the README.md file with the latest help output from flynt.
"""
import contextlib
import io
import os
import re
import sys
from pathlib import Path
from unittest.mock import patch
from flynt.cli import run_flynt_cli
options_marker = "<!-- begin-options -->"
def patch_terminal_size():
"""
Patch the terminal size to 70 characters
for better wrapping of help output.
"""
return patch(
"shutil.get_terminal_size",
return_value=os.terminal_size((70, 24)),
)
def main():
readme_path = Path(__file__).parent / "README.md"
sio = io.StringIO()
# Redirect the output,
# disable argparse exiting the entire program when it prints help,
# and patch the terminal size so we get the same output all the time
with (
contextlib.redirect_stdout(sio),
contextlib.suppress(
SystemExit,
),
patch_terminal_size(),
):
sys.argv = ["flynt", "--help"]
run_flynt_cli()
flynt_help = sio.getvalue()
original_readme_content = readme_path.read_text()
readme_content = re.sub(
rf"{options_marker}\n```.+?```\n",
f"{options_marker}\n```\n{flynt_help}\n```\n",
original_readme_content,
flags=re.DOTALL,
)
if readme_content != original_readme_content:
readme_path.write_text(readme_content)
print("Updated README.md")
if __name__ == "__main__":
main()