forked from harvard-edge/cs249r_book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset.py
More file actions
414 lines (351 loc) · 15.7 KB
/
Copy pathreset.py
File metadata and controls
414 lines (351 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"""
Reset command for TinyTorch CLI: resets package and user data.
"""
import json
import shutil
from datetime import datetime
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 package files or user progress data"
def add_arguments(self, parser: ArgumentParser) -> None:
# Lets bare `tito package reset [--force]` (with no SUBCOMMAND token)
# reset the package directly, matching the "tito package reset --force"
# example in PackageCommand's own help text.
parser.add_argument("--force", action="store_true", help="Skip confirmation prompt (resets the package)")
subparsers = parser.add_subparsers(
dest='reset_command',
help='Reset subcommands',
metavar='SUBCOMMAND'
)
# Package reset (original functionality)
package_parser = subparsers.add_parser(
'package',
help='Reset tinytorch package to clean state (remove exported files)'
)
package_parser.add_argument("--force", action="store_true", help="Skip confirmation prompt")
# All data reset
all_parser = subparsers.add_parser(
'all',
help='Reset all user progress (modules + milestones + config)'
)
all_parser.add_argument("--backup", action="store_true", help="Create backup before reset")
all_parser.add_argument("--force", action="store_true", help="Skip confirmation prompt")
# Progress reset
progress_parser = subparsers.add_parser(
'progress',
help='Reset module completion tracking only'
)
progress_parser.add_argument("--backup", action="store_true", help="Create backup before reset")
progress_parser.add_argument("--force", action="store_true", help="Skip confirmation prompt")
# Milestones reset
milestones_parser = subparsers.add_parser(
'milestones',
help='Reset milestone achievements only'
)
milestones_parser.add_argument("--backup", action="store_true", help="Create backup before reset")
milestones_parser.add_argument("--force", action="store_true", help="Skip confirmation prompt")
# Config reset
config_parser = subparsers.add_parser(
'config',
help='Reset configuration to defaults'
)
config_parser.add_argument("--force", action="store_true", help="Skip confirmation prompt")
def run(self, args: Namespace) -> int:
console = self.console
if not hasattr(args, 'reset_command') or not args.reset_command:
# No SUBCOMMAND given: default to resetting the package, matching
# PackageCommand's own documented "tito package reset --force"
# example and this command's top-level description ("Reset
# tinytorch package to clean state"). The other reset types
# (all/progress/milestones/config) are still reachable as
# tito package reset <type>, listed below for discoverability.
console.print(Panel(
"[dim]Other reset types available:[/dim]\n"
"[dim] • tito package reset all - Reset all user progress (modules + milestones + config)[/dim]\n"
"[dim] • tito package reset progress - Reset module completion tracking only[/dim]\n"
"[dim] • tito package reset milestones - Reset milestone achievements only[/dim]\n"
"[dim] • tito package reset config - Reset configuration to defaults[/dim]",
title="Reset Command Group",
border_style="bright_yellow"
))
return self._reset_package(args)
# Execute the appropriate subcommand
if args.reset_command == 'package':
return self._reset_package(args)
elif args.reset_command == 'all':
return self._reset_all(args)
elif args.reset_command == 'progress':
return self._reset_progress(args)
elif args.reset_command == 'milestones':
return self._reset_milestones(args)
elif args.reset_command == 'config':
return self._reset_config(args)
else:
console.print(Panel(
f"[red]Unknown reset subcommand: {args.reset_command}[/red]",
title="Error",
border_style="red"
))
return 1
def _reset_package(self, args: Namespace) -> int:
"""Reset tinytorch package (original functionality)."""
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 args.force:
console.print()
console.print(Panel(
"[yellow]This will remove all exported Python files from the tinytorch package.[/yellow]\n"
"[yellow]Notebooks in modules will be preserved.[/yellow]",
title="Warning",
border_style="yellow"
))
console.print()
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 all exported files:\n", style="bold red")
# Simple approach: remove all .py files except __init__.py
files_removed = 0
for py_file in tinytorch_path.rglob("*.py"):
if py_file.name != "__init__.py":
try:
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 as e:
reset_text.append(f" ❌ Failed to remove {py_file}: {e}\n", style="red")
# 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 module complete 01 - Re-export modules\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
def _create_backup(self) -> Path:
"""Create timestamped backup of .tito folder."""
console = self.console
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_dir = Path(f".tito_backup_{timestamp}")
tito_dir = Path(".tito")
if tito_dir.exists():
shutil.copytree(tito_dir, backup_dir)
console.print(f"[green]✅ Backup created: {backup_dir}[/green]")
return backup_dir
def _reset_all(self, args: Namespace) -> int:
"""Reset all user progress data."""
console = self.console
# Ask for confirmation
if not args.force:
console.print()
console.print(Panel(
"[bold red]⚠️ This will reset all progress[/bold red]\n\n"
"[yellow]This will clear:[/yellow]\n"
" • Module completion tracking\n"
" • Milestone achievements\n"
" • Configuration settings\n\n"
"[dim]Your code in modules will not be deleted.[/dim]",
title="Warning",
border_style="red"
))
console.print()
try:
response = input("Continue? (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("\n[cyan]Reset cancelled.[/cyan]",
title="Cancelled", border_style="cyan"))
return 0
# Create backup if requested
if args.backup:
self._create_backup()
# Reset all data files
tito_dir = Path(".tito")
tito_dir.mkdir(parents=True, exist_ok=True)
# Reset progress.json
progress_file = tito_dir / "progress.json"
progress_file.write_text(json.dumps({
"version": "1.0",
"completed_modules": [],
"completion_dates": {}
}, indent=2))
# Reset milestones.json
milestones_file = tito_dir / "milestones.json"
milestones_file.write_text(json.dumps({
"version": "1.0",
"completed_milestones": [],
"completion_dates": {}
}, indent=2))
# Reset config.json
config_file = tito_dir / "config.json"
config_file.write_text(json.dumps({
"logo_theme": "standard"
}, indent=2))
console.print(Panel(
"[green]✅ All progress reset![/green]\n\n"
"You're ready to start fresh.\\n"
"Run: [cyan]tito module start 01[/cyan]",
title="🔄 Reset Complete",
border_style="green"
))
return 0
def _reset_progress(self, args: Namespace) -> int:
"""Reset module completion tracking only."""
console = self.console
# Ask for confirmation
if not args.force:
console.print()
console.print(Panel(
"[bold yellow]⚠️ This will reset module completion tracking[/bold yellow]\n\n"
"[dim]Milestone achievements will be preserved.[/dim]",
title="Warning",
border_style="yellow"
))
console.print()
try:
response = input("Continue? (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("\n[cyan]Reset cancelled.[/cyan]",
title="Cancelled", border_style="cyan"))
return 0
# Create backup if requested
if args.backup:
self._create_backup()
# Reset progress.json
tito_dir = Path(".tito")
tito_dir.mkdir(parents=True, exist_ok=True)
progress_file = tito_dir / "progress.json"
progress_file.write_text(json.dumps({
"version": "1.0",
"completed_modules": [],
"completion_dates": {}
}, indent=2))
console.print(Panel(
"[green]✅ Module progress reset![/green]\n\n"
"You can re-complete modules with:\n"
"[cyan]tito module complete XX[/cyan]",
title="🔄 Progress Reset",
border_style="green"
))
return 0
def _reset_milestones(self, args: Namespace) -> int:
"""Reset milestone achievements only."""
console = self.console
# Ask for confirmation
if not args.force:
console.print()
console.print(Panel(
"[bold yellow]⚠️ This will reset milestone achievements[/bold yellow]\n\n"
"[dim]Module completion will be preserved.[/dim]",
title="Warning",
border_style="yellow"
))
console.print()
try:
response = input("Continue? (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("\n[cyan]Reset cancelled.[/cyan]",
title="Cancelled", border_style="cyan"))
return 0
# Create backup if requested
if args.backup:
self._create_backup()
# Reset milestones.json
tito_dir = Path(".tito")
tito_dir.mkdir(parents=True, exist_ok=True)
milestones_file = tito_dir / "milestones.json"
milestones_file.write_text(json.dumps({
"version": "1.0",
"completed_milestones": [],
"completion_dates": {}
}, indent=2))
console.print(Panel(
"[green]✅ Milestone achievements reset![/green]\n\n"
"You can re-run milestones with:\n"
"[cyan]tito milestone run XX[/cyan]",
title="🔄 Milestones Reset",
border_style="green"
))
return 0
def _reset_config(self, args: Namespace) -> int:
"""Reset configuration to defaults."""
console = self.console
# Ask for confirmation
if not args.force:
console.print()
console.print(Panel(
"[bold yellow]⚠️ This will reset configuration to defaults[/bold yellow]",
title="Warning",
border_style="yellow"
))
console.print()
try:
response = input("Continue? (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("\n[cyan]Reset cancelled.[/cyan]",
title="Cancelled", border_style="cyan"))
return 0
# Reset config.json
tito_dir = Path(".tito")
tito_dir.mkdir(parents=True, exist_ok=True)
config_file = tito_dir / "config.json"
config_file.write_text(json.dumps({
"logo_theme": "standard"
}, indent=2))
console.print(Panel(
"[green]✅ Configuration reset to defaults![/green]",
title="🔄 Config Reset",
border_style="green"
))
return 0