refactor: remove all inline comments from code
This commit is contained in:
@@ -1,12 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Update README.md with latest project information.
|
||||
|
||||
This script:
|
||||
- Updates Changelog from git commits
|
||||
- Updates Dependencies from pyproject.toml
|
||||
- Updates Commands from available scripts
|
||||
- Updates CI/CD badges
|
||||
"""
|
||||
|
||||
import re
|
||||
import subprocess
|
||||
@@ -17,19 +9,16 @@ from typing import Any
|
||||
|
||||
|
||||
def get_project_root() -> Path:
|
||||
"""Get project root directory."""
|
||||
return Path(__file__).parent.parent
|
||||
|
||||
|
||||
def get_pyproject() -> dict[str, Any]:
|
||||
"""Parse pyproject.toml."""
|
||||
root = get_project_root()
|
||||
with open(root / "pyproject.toml", "rb") as f:
|
||||
return tomllib.load(f)
|
||||
|
||||
|
||||
def get_latest_commits(count: int = 10) -> list[dict[str, str]]:
|
||||
"""Get latest commits with hash, message, date."""
|
||||
result = subprocess.run(
|
||||
["git", "log", "--format=%H|%s|%ad|%an", "--date=short", f"-n{count}"],
|
||||
capture_output=True,
|
||||
@@ -54,7 +43,6 @@ def get_latest_commits(count: int = 10) -> list[dict[str, str]]:
|
||||
|
||||
|
||||
def get_last_tag() -> str | None:
|
||||
"""Get last git tag."""
|
||||
result = subprocess.run(
|
||||
["git", "describe", "--tags", "--abbrev=0"],
|
||||
capture_output=True,
|
||||
@@ -65,20 +53,17 @@ def get_last_tag() -> str | None:
|
||||
|
||||
|
||||
def get_ignored_files() -> set[str]:
|
||||
"""Get list of ignored file patterns from .gitignore."""
|
||||
gitignore_path = get_project_root() / ".gitignore"
|
||||
ignored = set()
|
||||
if gitignore_path.exists():
|
||||
for line in gitignore_path.read_text().splitlines():
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#"):
|
||||
# Remove trailing slash for directories
|
||||
ignored.add(line.rstrip("/"))
|
||||
return ignored
|
||||
|
||||
|
||||
def commit_has_tracked_changes(commit_hash: str) -> bool:
|
||||
"""Check if commit has changes to tracked (non-ignored) files."""
|
||||
result = subprocess.run(
|
||||
["git", "diff-tree", "--no-commit-id", "--name-only", "-r", commit_hash],
|
||||
capture_output=True,
|
||||
@@ -92,7 +77,6 @@ def commit_has_tracked_changes(commit_hash: str) -> bool:
|
||||
for file_path in result.stdout.strip().split("\n"):
|
||||
if not file_path:
|
||||
continue
|
||||
# Check if file or its parent dir is ignored
|
||||
parts = file_path.split("/")
|
||||
is_ignored = False
|
||||
for i in range(len(parts)):
|
||||
@@ -113,7 +97,6 @@ def commit_has_tracked_changes(commit_hash: str) -> bool:
|
||||
|
||||
|
||||
def commit_has_skip_ci_message(commit_hash: str) -> bool:
|
||||
"""Check if commit message contains [skip ci] or similar."""
|
||||
result = subprocess.run(
|
||||
["git", "log", "-1", "--format=%s", commit_hash],
|
||||
capture_output=True,
|
||||
@@ -125,7 +108,6 @@ def commit_has_skip_ci_message(commit_hash: str) -> bool:
|
||||
|
||||
|
||||
def commit_only_changes_readme(commit_hash: str) -> bool:
|
||||
"""Check if commit only changes README.md."""
|
||||
result = subprocess.run(
|
||||
["git", "diff-tree", "--no-commit-id", "--name-only", "-r", commit_hash],
|
||||
capture_output=True,
|
||||
@@ -137,7 +119,6 @@ def commit_only_changes_readme(commit_hash: str) -> bool:
|
||||
|
||||
|
||||
def get_commits_since_tag(tag: str | None) -> list[dict[str, str]]:
|
||||
"""Get commits since last tag (only commits with tracked file changes)."""
|
||||
if tag:
|
||||
result = subprocess.run(
|
||||
["git", "log", "--format=%H|%s|%ad|%an", "--date=short", f"{tag}..HEAD"],
|
||||
@@ -159,13 +140,10 @@ def get_commits_since_tag(tag: str | None) -> list[dict[str, str]]:
|
||||
parts = line.split("|")
|
||||
if len(parts) >= 4:
|
||||
commit_hash = parts[0]
|
||||
# Skip commits with [skip ci] in message
|
||||
if commit_has_skip_ci_message(commit_hash):
|
||||
continue
|
||||
# Skip commits that only change README.md
|
||||
if commit_only_changes_readme(commit_hash):
|
||||
continue
|
||||
# Skip commits that only change ignored files
|
||||
if not commit_has_tracked_changes(commit_hash):
|
||||
continue
|
||||
commits.append(
|
||||
@@ -180,7 +158,6 @@ def get_commits_since_tag(tag: str | None) -> list[dict[str, str]]:
|
||||
|
||||
|
||||
def categorize_commits(commits: list[dict[str, str]]) -> dict[str, list[str]]:
|
||||
"""Categorize commits by type (feat, fix, docs, etc.)."""
|
||||
categories: dict[str, list[str]] = {
|
||||
"Added": [],
|
||||
"Changed": [],
|
||||
@@ -208,7 +185,6 @@ def categorize_commits(commits: list[dict[str, str]]) -> dict[str, list[str]]:
|
||||
|
||||
|
||||
def format_changelog(commits: list[dict[str, str]], version: str = "v0.1.0") -> str:
|
||||
"""Format commits as GitHub Releases changelog."""
|
||||
categorized = categorize_commits(commits)
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
|
||||
@@ -223,7 +199,6 @@ def format_changelog(commits: list[dict[str, str]], version: str = "v0.1.0") ->
|
||||
|
||||
|
||||
def get_dependencies(pyproject: dict[str, Any]) -> dict[str, list[str]]:
|
||||
"""Extract dependencies from pyproject.toml."""
|
||||
deps: dict[str, list[str]] = {
|
||||
"runtime": [],
|
||||
"tests": [],
|
||||
@@ -232,11 +207,9 @@ def get_dependencies(pyproject: dict[str, Any]) -> dict[str, list[str]]:
|
||||
"docs": [],
|
||||
}
|
||||
|
||||
# Runtime dependencies
|
||||
for dep in pyproject.get("project", {}).get("dependencies", []):
|
||||
deps["runtime"].append(dep)
|
||||
|
||||
# Dev dependency groups
|
||||
dep_groups = pyproject.get("dependency-groups", {})
|
||||
|
||||
if "tests" in dep_groups:
|
||||
@@ -263,7 +236,6 @@ def get_dependencies(pyproject: dict[str, Any]) -> dict[str, list[str]]:
|
||||
|
||||
|
||||
def get_available_commands() -> list[dict[str, str]]:
|
||||
"""Get available uv commands from pyproject.toml."""
|
||||
commands = [
|
||||
{"cmd": "uv sync", "desc": "Install dependencies"},
|
||||
{"cmd": "uv run python -m app.main", "desc": "Start development server"},
|
||||
@@ -285,7 +257,6 @@ def get_available_commands() -> list[dict[str, str]]:
|
||||
|
||||
|
||||
def update_dependencies_section(content: str, deps: dict[str, list[str]]) -> str:
|
||||
"""Update Dependencies section in README."""
|
||||
section_pattern = r"(## Dependencies\n.*?)(\n## |\Z)"
|
||||
|
||||
deps_text = "## Dependencies\n\n"
|
||||
@@ -313,7 +284,6 @@ def update_dependencies_section(content: str, deps: dict[str, list[str]]) -> str
|
||||
|
||||
|
||||
def update_commands_section(content: str, commands: list[dict[str, str]]) -> str:
|
||||
"""Update Commands section in README."""
|
||||
section_pattern = r"(## Available Commands\n.*?\|.*?\n\|---\|.*?\n)(.*?)(\n## |\Z)"
|
||||
|
||||
commands_table = "| Command | Description |\n|---------|-------------|\n"
|
||||
@@ -327,7 +297,6 @@ def update_commands_section(content: str, commands: list[dict[str, str]]) -> str
|
||||
|
||||
|
||||
def update_changelog_section(content: str, changelog: str) -> str:
|
||||
"""Update Changelog section in README."""
|
||||
section_pattern = r"(## Changelog\n)(.*?)(\Z)"
|
||||
|
||||
replacement = f"\\1\n{changelog}\n\\3"
|
||||
@@ -335,14 +304,6 @@ def update_changelog_section(content: str, changelog: str) -> str:
|
||||
|
||||
|
||||
def update_readme(check_only: bool = False) -> bool:
|
||||
"""Update README.md with latest information.
|
||||
|
||||
Args:
|
||||
check_only: If True, only check if update needed (for CI)
|
||||
|
||||
Returns:
|
||||
True if changes were made (or needed in check mode)
|
||||
"""
|
||||
readme_path = get_project_root() / "README.md"
|
||||
|
||||
if not readme_path.exists():
|
||||
@@ -352,17 +313,14 @@ def update_readme(check_only: bool = False) -> bool:
|
||||
content = readme_path.read_text()
|
||||
original_content = content
|
||||
|
||||
# Get data
|
||||
pyproject = get_pyproject()
|
||||
commits = get_commits_since_tag(get_last_tag())
|
||||
deps = get_dependencies(pyproject)
|
||||
commands = get_available_commands()
|
||||
|
||||
# Generate changelog
|
||||
version = get_last_tag() or "v0.1.0"
|
||||
changelog = format_changelog(commits, version)
|
||||
|
||||
# Update sections
|
||||
content = update_changelog_section(content, changelog)
|
||||
content = update_dependencies_section(content, deps)
|
||||
content = update_commands_section(content, commands)
|
||||
@@ -375,7 +333,6 @@ def update_readme(check_only: bool = False) -> bool:
|
||||
print("README.md is up to date")
|
||||
return needs_update
|
||||
|
||||
# Write updated content
|
||||
if content != original_content:
|
||||
readme_path.write_text(content)
|
||||
print("README.md updated successfully")
|
||||
@@ -386,7 +343,6 @@ def update_readme(check_only: bool = False) -> bool:
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point."""
|
||||
import sys
|
||||
|
||||
check_only = "--check" in sys.argv
|
||||
|
||||
Reference in New Issue
Block a user