Files
odoo-expertos.com/directory/commands/deploy.py
T
2026-08-28 10:11:11 -03:00

149 lines
4.2 KiB
Python

"""
Deploy command for pushing changes to Git.
"""
import logging
import subprocess
from pathlib import Path
from datetime import datetime
from typing import Optional
import typer
from rich.console import Console
from ..config import get_settings
logger = logging.getLogger(__name__)
console = Console()
def deploy_command(
message: Optional[str] = None,
push: bool = True,
) -> None:
"""
Deploy exported files via Git.
Adds all changes in the export directory, commits them,
and optionally pushes to the remote repository.
Args:
message: Custom commit message (auto-generated if not provided)
push: Whether to push to remote after commit
"""
console.print("\n[bold blue]Deploying changes[/bold blue]\n")
settings = get_settings()
project_root = Path(settings.export_dir).parent
# Verify we're in a git repo
if not (project_root / ".git").exists():
console.print("[red]Not a git repository![/red]")
console.print(f"Expected .git directory in {project_root}")
raise typer.Exit(1)
try:
# Check for changes
result = subprocess.run(
["git", "status", "--porcelain"],
cwd=project_root,
capture_output=True,
text=True,
check=True,
)
if not result.stdout.strip():
console.print("[yellow]No changes to deploy.[/yellow]")
return
# Show changes
console.print("[bold]Changes to deploy:[/bold]")
console.print(result.stdout)
# Confirm deployment
if not typer.confirm("Deploy these changes?"):
console.print("[yellow]Deployment cancelled.[/yellow]")
raise typer.Exit(0)
# Generate commit message if not provided
if not message:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M")
message = f"Directory update: {timestamp}"
# Stage changes
console.print("Staging changes...")
subprocess.run(
["git", "add", "export/", "static/images/companies/"],
cwd=project_root,
check=True,
)
# Commit
console.print(f"Committing: {message}")
subprocess.run(
["git", "commit", "-m", message],
cwd=project_root,
check=True,
)
# Push if requested
if push:
console.print("Pushing to remote...")
subprocess.run(
["git", "push"],
cwd=project_root,
check=True,
)
console.print("[green]Changes pushed to remote.[/green]")
else:
console.print("[yellow]Changes committed locally but not pushed.[/yellow]")
console.print("\n[bold green]Deployment complete![/bold green]")
# Show deployment info
console.print("\nVercel will automatically deploy the changes.")
console.print("Check deployment status at: https://vercel.com/dashboard")
except subprocess.CalledProcessError as e:
console.print(f"[red]Git command failed: {e}[/red]")
if e.stderr:
console.print(f"Error: {e.stderr}")
raise typer.Exit(1)
except Exception as e:
console.print(f"[red]Deployment failed: {e}[/red]")
logger.exception("Deployment error")
raise typer.Exit(1)
def status_command() -> None:
"""Show git status of the project."""
settings = get_settings()
project_root = Path(settings.export_dir).parent
try:
# Git status
result = subprocess.run(
["git", "status"],
cwd=project_root,
capture_output=True,
text=True,
check=True,
)
console.print(result.stdout)
# Last commit
console.print("\n[bold]Last commit:[/bold]")
result = subprocess.run(
["git", "log", "-1", "--oneline"],
cwd=project_root,
capture_output=True,
text=True,
check=True,
)
console.print(result.stdout)
except subprocess.CalledProcessError as e:
console.print(f"[red]Git command failed: {e}[/red]")
raise typer.Exit(1)