| 1 |
|
| 2 |
|
| 3 |
import subprocess |
| 4 |
import tempfile |
| 5 |
|
| 6 |
|
| 7 |
def parse_autoflake_py() -> tuple[str, list[str]]: |
| 8 |
lines: list[str] = [] |
| 9 |
for line in open("autoflake.py"): |
| 10 |
if lines: |
| 11 |
if lines and "parser.parse_args" in line: |
| 12 |
break |
| 13 |
lines.append(line) |
| 14 |
continue |
| 15 |
|
| 16 |
if line.startswith("__version__"): |
| 17 |
version_line = line |
| 18 |
continue |
| 19 |
|
| 20 |
if "argparse.ArgumentParser" in line: |
| 21 |
lines.append(line) |
| 22 |
continue |
| 23 |
|
| 24 |
return version_line, lines |
| 25 |
|
| 26 |
|
| 27 |
def argparse_pyfile(): |
| 28 |
version_line, lines = parse_autoflake_py() |
| 29 |
return f""" |
| 30 |
import argparse |
| 31 |
|
| 32 |
{version_line} |
| 33 |
def get_argparse(): |
| 34 |
{"".join(lines)} |
| 35 |
return parser |
| 36 |
""" |
| 37 |
|
| 38 |
|
| 39 |
def get_argparse_manpage_cmd(filename): |
| 40 |
return [ |
| 41 |
"argparse-manpage", |
| 42 |
"--pyfile", |
| 43 |
filename, |
| 44 |
"--function", |
| 45 |
"get_argparse", |
| 46 |
"--author", |
| 47 |
"Steven Myint", |
| 48 |
"--author-email", |
| 49 |
"[email protected]", |
| 50 |
"--project-name", |
| 51 |
"autoflake", |
| 52 |
"--url", |
| 53 |
"https://github.com/PyCQA/autoflake", |
| 54 |
] |
| 55 |
|
| 56 |
|
| 57 |
def run_argparse_manpage(): |
| 58 |
with tempfile.NamedTemporaryFile(mode="w") as tmp: |
| 59 |
tmp.write(argparse_pyfile()) |
| 60 |
tmp.flush() |
| 61 |
cmd = get_argparse_manpage_cmd(tmp.name) |
| 62 |
p = subprocess.run(cmd, text=True, capture_output=True) |
| 63 |
return p.stdout.splitlines() |
| 64 |
|
| 65 |
|
| 66 |
def build_manpage(): |
| 67 |
manpage = run_argparse_manpage() |
| 68 |
for line in manpage: |
| 69 |
if line == "autoflake": |
| 70 |
print( |
| 71 |
"autoflake \- removes unused imports and unused variables in Python code" |
| 72 |
) |
| 73 |
continue |
| 74 |
if line == ".SH OPTIONS": |
| 75 |
print(".SH DESCRIPTION") |
| 76 |
print( |
| 77 |
"Removes unused imports and unused variables as reported by pyflakes." |
| 78 |
) |
| 79 |
print(line) |
| 80 |
|
| 81 |
if __name__ == "__main__": |
| 82 |
build_manpage() |