Files
TinyTorch/tito/commands/reset.py
T
Vijay Janapa Reddi 15f5a84863 RESTORE: Complete CLI functionality in new architecture
- Ported all commands from bin/tito.py to new tito/ CLI architecture
- Added InfoCommand with system info and module status
- Added TestCommand with pytest integration
- Added DoctorCommand with environment diagnosis
- Added SyncCommand for nbdev export functionality
- Added ResetCommand for package cleanup
- Added JupyterCommand for notebook server
- Added NbdevCommand for nbdev development tools
- Added SubmitCommand and StatusCommand (placeholders)
- Fixed missing imports in tinytorch/core/tensor.py
- All commands now work with 'tito' command in shell
- Maintains professional architecture while restoring full functionality

Commands restored:
 info - System information and module status
 test - Run module tests with pytest
 doctor - Environment diagnosis
 sync - Export notebooks to package
 reset - Clean tinytorch package
 nbdev - nbdev development commands
 jupyter - Start Jupyter server
 submit - Module submission
 status - Module status
 notebooks - Build notebooks from Python files

The CLI now has both the professional architecture and all original functionality.
2025-07-10 22:39:23 -04:00

98 lines
4.3 KiB
Python

"""
Reset command for TinyTorch CLI: resets tinytorch package to clean state.
"""
import shutil
from argparse import ArgumentParser, Namespace
from pathlib import Path
from rich.panel import Panel
from rich.text import Text
from .base import BaseCommand
class ResetCommand(BaseCommand):
@property
def name(self) -> str:
return "reset"
@property
def description(self) -> str:
return "Reset tinytorch package to clean state"
def add_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument("--force", action="store_true", help="Skip confirmation prompt")
def run(self, args: Namespace) -> int:
console = self.console
console.print(Panel("🔄 Resetting TinyTorch Package",
title="Package Reset", border_style="bright_yellow"))
tinytorch_path = Path("tinytorch")
if not tinytorch_path.exists():
console.print(Panel("[yellow]⚠️ TinyTorch package directory not found. Nothing to reset.[/yellow]",
title="Nothing to Reset", border_style="yellow"))
return 0
# Ask for confirmation unless --force is used
if not (hasattr(args, 'force') and args.force):
console.print("\n[yellow]This will remove all exported Python files from the tinytorch package.[/yellow]")
console.print("[yellow]Notebooks in modules/ will be preserved.[/yellow]\n")
try:
response = input("Are you sure you want to reset? (y/N): ").strip().lower()
if response not in ['y', 'yes']:
console.print(Panel("[cyan]Reset cancelled.[/cyan]",
title="Cancelled", border_style="cyan"))
return 0
except KeyboardInterrupt:
console.print(Panel("[cyan]Reset cancelled.[/cyan]",
title="Cancelled", border_style="cyan"))
return 0
reset_text = Text()
reset_text.append("🗑️ Removing generated files:\n", style="bold red")
# Remove generated Python files but keep __init__.py files and directory structure
files_removed = 0
for py_file in tinytorch_path.rglob("*.py"):
if py_file.name != "__init__.py":
# Check if it's an auto-generated file
try:
with open(py_file, 'r') as f:
first_line = f.readline().strip()
if "AUTOGENERATED" in first_line or "_modidx.py" in str(py_file):
rel_path = py_file.relative_to(tinytorch_path)
reset_text.append(f" 🗑️ tinytorch/{rel_path}\n", style="red")
py_file.unlink()
files_removed += 1
except Exception:
# If we can't read the file, skip it for safety
pass
# Remove __pycache__ directories
for pycache in tinytorch_path.rglob("__pycache__"):
if pycache.is_dir():
reset_text.append(f" 🗑️ {pycache}/\n", style="red")
shutil.rmtree(pycache)
# Remove .pytest_cache if it exists
pytest_cache = Path(".pytest_cache")
if pytest_cache.exists():
reset_text.append(f" 🗑️ .pytest_cache/\n", style="red")
shutil.rmtree(pytest_cache)
if files_removed > 0:
reset_text.append(f"\n✅ Reset complete! Removed {files_removed} generated files.\n", style="bold green")
reset_text.append("\n💡 Next steps:\n", style="bold yellow")
reset_text.append(" • Run: tito sync - Re-export notebooks\n", style="white")
reset_text.append(" • Run: tito sync --module setup - Export specific module\n", style="white")
reset_text.append(" • Run: tito test --all - Test everything\n", style="white")
console.print(Panel(reset_text, title="Reset Complete", border_style="green"))
else:
console.print(Panel("[yellow]No generated files found to remove.[/yellow]",
title="Nothing to Reset", border_style="yellow"))
return 0