chore: improve create_command_partials runtime by not calling subprocess several times

This commit is contained in:
Matthias
2025-12-26 20:02:24 +01:00
parent 89c4aa23f3
commit 4e809947c7

View File

@@ -1,5 +1,7 @@
import os
import subprocess # noqa: S404, RUF100
import sys
from io import StringIO
from pathlib import Path
@@ -8,7 +10,20 @@ def _write_partial_file(filename: str, content: str):
f.write(f"``` output\n{content}\n```\n")
def _get_help_output(parser) -> str:
"""Capture the help output from a parser."""
output = StringIO()
parser.print_help(file=output)
return output.getvalue()
def extract_command_partials():
# Set terminal width to 80 columns for consistent output formatting
os.environ["COLUMNS"] = "80"
# Import Arguments here to avoid circular imports and ensure COLUMNS is set
from freqtrade.commands.arguments import Arguments
subcommands = [
"trade",
"create-userdir",
@@ -46,16 +61,35 @@ def extract_command_partials():
"recursive-analysis",
]
result = subprocess.run(["freqtrade", "--help"], capture_output=True, text=True)
# Build the Arguments class to get the parser with all subcommands
args = Arguments(None)
args._build_subcommands()
_write_partial_file("docs/commands/main.md", result.stdout)
# Get main help output
main_help = _get_help_output(args.parser)
_write_partial_file("docs/commands/main.md", main_help)
# Get subparsers from the main parser
# The subparsers are stored in _subparsers._group_actions[0].choices
subparsers_action = None
for action in args.parser._subparsers._group_actions:
if hasattr(action, "choices"):
subparsers_action = action
break
if subparsers_action is None:
raise RuntimeError("Could not find subparsers in the main parser")
for command in subcommands:
print(f"Running for {command}")
result = subprocess.run(["freqtrade", command, "--help"], capture_output=True, text=True)
_write_partial_file(f"docs/commands/{command}.md", result.stdout)
if command in subparsers_action.choices:
subparser = subparsers_action.choices[command]
help_output = _get_help_output(subparser)
_write_partial_file(f"docs/commands/{command}.md", help_output)
else:
print(f" Warning: subcommand '{command}' not found in parser")
# freqtrade-client still uses subprocess as requested
print("Running for freqtrade-client")
result_client = subprocess.run(["freqtrade-client", "--show"], capture_output=True, text=True)