Auto-update Docs Reference section action (#13127)

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn Jocher 2024-05-25 21:25:51 +02:00 committed by GitHub
parent 2e65e6fcb3
commit 627453c26a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 116 additions and 37 deletions

View file

@ -7,13 +7,13 @@ Note: Must be run from repository root directory. Do not run from docs directory
"""
import re
import subprocess
from collections import defaultdict
from pathlib import Path
# Get package root i.e. /Users/glennjocher/PycharmProjects/ultralytics/ultralytics
from ultralytics.utils import ROOT as PACKAGE_DIR
# Constants
FILE = Path(__file__).resolve()
PACKAGE_DIR = FILE.parents[1] / "ultralytics" # i.e. /Users/glennjocher/PycharmProjects/ultralytics/ultralytics
REFERENCE_DIR = PACKAGE_DIR.parent / "docs/en/reference"
GITHUB_REPO = "ultralytics/ultralytics"
@ -33,15 +33,18 @@ def extract_classes_and_functions(filepath: Path) -> tuple:
def create_markdown(py_filepath: Path, module_path: str, classes: list, functions: list):
"""Creates a Markdown file containing the API reference for the given Python module."""
md_filepath = py_filepath.with_suffix(".md")
exists = md_filepath.exists()
# Read existing content and keep header content between first two ---
header_content = ""
if md_filepath.exists():
if exists:
existing_content = md_filepath.read_text()
header_parts = existing_content.split("---")
for part in header_parts:
if "description:" in part or "comments:" in part:
header_content += f"---{part}---\n\n"
if not any(header_content):
header_content = "---\ndescription: TODO ADD DESCRIPTION\nkeywords: TODO ADD KEYWORDS\n---\n\n"
module_name = module_path.replace(".__init__", "")
module_path = module_path.replace(".", "/")
@ -62,6 +65,11 @@ def create_markdown(py_filepath: Path, module_path: str, classes: list, function
md_filepath.parent.mkdir(parents=True, exist_ok=True)
md_filepath.write_text(md_content)
if not exists:
# Add new markdown file to the git staging area
print(f"Created new file '{md_filepath}'")
subprocess.run(["git", "add", "-f", str(md_filepath)], check=True)
return md_filepath.relative_to(PACKAGE_DIR.parent)