forked from encode/uvicorn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tool to keep CLI usage in
index.md
in sync (encode#876)
* Add tool to keep CLI usage in `index.md` in sync * Ensure final newline * Keep deployment.md in sync as well * Rename and reorganize tool
- Loading branch information
1 parent
1b36d69
commit 5311e11
Showing
6 changed files
with
143 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
Look for a marker comment in docs pages, and place the output of | ||
`$ uvicorn --help` there. Pass `--check` to ensure the content is in sync. | ||
""" | ||
import argparse | ||
import subprocess | ||
import sys | ||
import typing | ||
from pathlib import Path | ||
|
||
|
||
def _get_usage_lines() -> typing.List[str]: | ||
res = subprocess.run(["uvicorn", "--help"], stdout=subprocess.PIPE) | ||
help_text = res.stdout.decode("utf-8") | ||
return ["```", "$ uvicorn --help", *help_text.splitlines(), "```"] | ||
|
||
|
||
def _find_next_codefence_lineno(lines: typing.List[str], after: int) -> int: | ||
return next( | ||
lineno for lineno, line in enumerate(lines[after:], after) if line == "```" | ||
) | ||
|
||
|
||
def _get_insert_location(lines: typing.List[str]) -> typing.Tuple[int, int]: | ||
marker = lines.index("<!-- :cli_usage: -->") | ||
start = marker + 1 | ||
|
||
if lines[start] == "```": | ||
# Already generated. | ||
# <!-- :cli_usage: --> | ||
# ``` <- start | ||
# [...] | ||
# ``` <- end | ||
next_codefence = _find_next_codefence_lineno(lines, after=start + 1) | ||
end = next_codefence + 1 | ||
else: | ||
# Not generated yet. | ||
end = start | ||
|
||
return start, end | ||
|
||
|
||
def _generate_cli_usage(path: Path, check: bool = False) -> int: | ||
content = path.read_text() | ||
|
||
lines = content.splitlines() | ||
usage_lines = _get_usage_lines() | ||
start, end = _get_insert_location(lines) | ||
lines = lines[:start] + usage_lines + lines[end:] | ||
output = "\n".join(lines) + "\n" | ||
|
||
if check: | ||
if content == output: | ||
return 0 | ||
print(f"ERROR: CLI usage in {path} is out of sync. Run scripts/lint to fix.") | ||
return 1 | ||
|
||
path.write_text(output) | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--check", action="store_true") | ||
args = parser.parse_args() | ||
paths = [Path("docs", "index.md"), Path("docs", "deployment.md")] | ||
rv = 0 | ||
for path in paths: | ||
rv |= _generate_cli_usage(path, check=args.check) | ||
sys.exit(rv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters