-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathupgrade.py
More file actions
592 lines (508 loc) · 18.5 KB
/
upgrade.py
File metadata and controls
592 lines (508 loc) · 18.5 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Upgrade command for upgrading existing projects to newer ASP versions."""
import difflib
import logging
import pathlib
import re
import shutil
import subprocess
import tempfile
import click
from rich.console import Console
from rich.prompt import Prompt
from ..utils.generation_metadata import metadata_to_cli_args
from ..utils.logging import handle_cli_error
from ..utils.upgrade import (
DependencyChange,
FileCompareResult,
compare_all_files,
group_results_by_action,
merge_pyproject_dependencies,
write_merged_dependencies,
)
from ..utils.version import get_current_version
from .enhance import get_project_asp_config
console = Console()
def _ensure_uvx_available() -> bool:
"""Check if uvx is available."""
try:
subprocess.run(["uvx", "--version"], capture_output=True, check=True)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
return False
def _run_create_command(
args: list[str],
output_dir: pathlib.Path,
project_name: str,
version: str | None = None,
) -> bool:
"""Run the create command to generate a template.
Args:
args: CLI arguments for create command
output_dir: Directory to output the template
project_name: Name for the project
version: Optional ASP version to use (uses uvx if specified)
Returns:
True if successful, False otherwise
"""
# Build the command
if version:
cmd = ["uvx", f"agent-starter-pack@{version}", "create"]
else:
cmd = ["agent-starter-pack", "create"]
cmd.extend([project_name])
cmd.extend(["--output-dir", str(output_dir)])
cmd.extend(["--auto-approve", "--skip-deps", "--skip-checks"])
cmd.extend(args)
logging.debug(f"Running command: {' '.join(cmd)}")
try:
result = subprocess.run(
cmd,
capture_output=True,
text=True,
encoding="utf-8",
timeout=300, # 5 minute timeout
)
if result.returncode != 0:
logging.error(f"Command failed: {result.stderr}")
return False
return True
except subprocess.TimeoutExpired:
logging.error("Command timed out")
return False
except Exception as e:
logging.error(f"Error running command: {e}")
return False
def _display_version_header(old_version: str, new_version: str) -> None:
"""Display the upgrade version header."""
console.print()
console.print(f"[bold blue]📦 Upgrading {old_version} → {new_version}[/bold blue]")
console.print()
def _display_results(
groups: dict[str, list[FileCompareResult]],
dep_changes: list[DependencyChange],
dry_run: bool = False,
) -> None:
"""Display the upgrade results grouped by action."""
if groups["auto_update"]:
console.print("[bold green]Auto-updating (unchanged by you):[/bold green]")
for result in groups["auto_update"]:
console.print(f" [green]✓[/green] {result.path}")
console.print()
preserved_user_modified = [
r for r in groups["preserve"] if r.preserve_type == "asp_unchanged"
]
if preserved_user_modified:
console.print(
"[bold cyan]Preserving (you modified, ASP unchanged):[/bold cyan]"
)
for result in preserved_user_modified:
console.print(f" [cyan]✓[/cyan] {result.path}")
console.print()
skipped = [
r for r in groups["skip"] if r.category in ("agent_code", "config_files")
]
if skipped:
console.print("[dim]Skipping (your code):[/dim]")
for result in skipped:
console.print(f" [dim]-[/dim] {result.path}")
console.print()
if groups["new"]:
console.print("[bold yellow]New files in ASP:[/bold yellow]")
for result in groups["new"]:
console.print(f" [yellow]+[/yellow] {result.path}")
console.print()
if groups["removed"]:
console.print("[bold yellow]Removed in ASP:[/bold yellow]")
for result in groups["removed"]:
console.print(f" [yellow]-[/yellow] {result.path}")
console.print()
if groups["conflict"]:
console.print("[bold red]Conflicts (both changed):[/bold red]")
for result in groups["conflict"]:
console.print(f" [red]⚠[/red] {result.path}")
if not dry_run:
console.print("[dim] You'll be prompted to resolve each conflict.[/dim]")
console.print()
if dep_changes:
console.print("[bold]Dependencies:[/bold]")
for change in dep_changes:
if change.change_type == "updated":
console.print(
f" [green]✓[/green] Updated: {change.name} "
f"{change.old_version} → {change.new_version}"
)
elif change.change_type == "added":
console.print(
f" [green]+[/green] Added: {change.name}{change.new_version}"
)
elif change.change_type == "kept":
console.print(
f" [cyan]✓[/cyan] Kept: {change.name}{change.old_version}"
)
elif change.change_type == "removed":
console.print(
f" [yellow]-[/yellow] Removed: {change.name}{change.old_version}"
)
console.print()
def _handle_conflict(
result: FileCompareResult,
project_dir: pathlib.Path,
new_template_dir: pathlib.Path,
auto_approve: bool,
) -> str:
"""Handle a file conflict interactively.
Args:
result: The conflict result
project_dir: Path to current project
new_template_dir: Path to new template
auto_approve: If True, keep user's version
Returns:
Action taken: "kept", "updated", or "skipped"
"""
if auto_approve:
console.print(f" [dim]Keeping your version: {result.path}[/dim]")
return "kept"
console.print(f"\n[bold yellow]Conflict: {result.path}[/bold yellow]")
console.print(f" Reason: {result.reason}")
choice = Prompt.ask(
" (v)iew diff, (k)eep yours, (u)se new, (s)kip",
choices=["v", "k", "u", "s"],
default="k",
)
if choice == "v":
# Show diff using Python's difflib (cross-platform)
current_file = project_dir / result.path
new_file = new_template_dir / result.path
try:
current_lines = current_file.read_text(encoding="utf-8").splitlines(
keepends=True
)
new_lines = new_file.read_text(encoding="utf-8").splitlines(keepends=True)
diff_lines = list(
difflib.unified_diff(
current_lines,
new_lines,
fromfile=f"Your version: {result.path}",
tofile=f"New ASP version: {result.path}",
)
)
diff_output = "".join(diff_lines)
console.print()
if diff_output:
# Limit output to ~2000 characters
if len(diff_output) > 2000:
console.print(diff_output[:2000])
console.print("[dim]... (truncated)[/dim]")
else:
console.print(diff_output)
else:
console.print("[dim]No differences found[/dim]")
except Exception as e:
console.print(f"[red]Could not show diff: {e}[/red]")
# Ask again after viewing
choice = Prompt.ask(
" (k)eep yours, (u)se new, (s)kip",
choices=["k", "u", "s"],
default="k",
)
if choice == "k":
console.print(" [cyan]Keeping your version[/cyan]")
return "kept"
elif choice == "u":
return "updated"
else:
return "skipped"
def _copy_file(src: pathlib.Path, dst: pathlib.Path) -> bool:
"""Copy a file, creating parent directories as needed."""
if not src.exists():
return False
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
return True
def _apply_changes(
groups: dict[str, list[FileCompareResult]],
project_dir: pathlib.Path,
new_template_dir: pathlib.Path,
auto_approve: bool,
dry_run: bool,
) -> dict[str, int]:
"""Apply the upgrade changes to the project."""
counts = {
"updated": 0,
"added": 0,
"removed": 0,
"skipped": 0,
"conflicts_kept": 0,
"conflicts_updated": 0,
}
if dry_run:
console.print("[bold yellow]Dry run - no changes made[/bold yellow]")
return counts
for result in groups["auto_update"]:
if _copy_file(new_template_dir / result.path, project_dir / result.path):
counts["updated"] += 1
for result in groups["new"]:
should_add = (
auto_approve
or Prompt.ask(
f" Add new file {result.path}?", choices=["y", "n"], default="y"
)
== "y"
)
if should_add:
if _copy_file(new_template_dir / result.path, project_dir / result.path):
counts["added"] += 1
else:
counts["skipped"] += 1
for result in groups["removed"]:
file_path = project_dir / result.path
should_remove = (
auto_approve
or Prompt.ask(
f" Remove file {result.path}?", choices=["y", "n"], default="y"
)
== "y"
)
if should_remove and file_path.exists():
file_path.unlink()
counts["removed"] += 1
elif not should_remove:
counts["skipped"] += 1
if groups["conflict"]:
console.print()
console.print("[bold]Resolving conflicts:[/bold]")
for result in groups["conflict"]:
action = _handle_conflict(result, project_dir, new_template_dir, auto_approve)
if action == "updated":
if _copy_file(new_template_dir / result.path, project_dir / result.path):
counts["conflicts_updated"] += 1
elif action == "kept":
counts["conflicts_kept"] += 1
else:
counts["skipped"] += 1
return counts
def _update_pyproject_metadata(
project_dir: pathlib.Path,
new_version: str,
) -> None:
"""Update the asp_version in pyproject.toml.
Args:
project_dir: Path to project directory
new_version: New ASP version
"""
pyproject_path = project_dir / "pyproject.toml"
if not pyproject_path.exists():
return
try:
content = pyproject_path.read_text(encoding="utf-8")
content = re.sub(
r'(asp_version\s*=\s*")[^"]*(")',
rf"\g<1>{new_version}\g<2>",
content,
)
pyproject_path.write_text(content, encoding="utf-8")
except Exception as e:
logging.warning(f"Could not update asp_version: {e}")
@click.command()
@click.argument(
"project_path",
type=click.Path(exists=True, path_type=pathlib.Path),
default=".",
required=False,
)
@click.option(
"--dry-run",
is_flag=True,
help="Preview changes without applying them",
)
@click.option(
"--auto-approve",
"-y",
is_flag=True,
help="Auto-apply non-conflicting changes without prompts",
)
@click.option(
"--debug",
is_flag=True,
help="Enable debug logging",
)
@handle_cli_error
def upgrade(
project_path: pathlib.Path,
dry_run: bool,
auto_approve: bool,
debug: bool,
) -> None:
"""Upgrade project to newer agent-starter-pack version.
Compares your project against both old and new template versions to
intelligently apply updates while preserving your customizations.
Uses 3-way comparison:
- If you didn't modify a file, it's auto-updated
- If ASP didn't change a file, your modifications are preserved
- If both changed, you're prompted to resolve the conflict
"""
if debug:
logging.basicConfig(level=logging.DEBUG, force=True)
console.print("[dim]Debug mode enabled[/dim]")
# Resolve project path
project_dir = project_path.resolve()
metadata = get_project_asp_config(project_dir)
if not metadata:
console.print(
"[bold red]Error:[/bold red] No agent-starter-pack metadata found."
)
console.print("Ensure pyproject.toml has \\[tool.agent-starter-pack] section.")
raise SystemExit(1)
old_version = metadata.get("asp_version")
if not old_version:
console.print(
"[bold red]Error:[/bold red] No asp_version found in project metadata."
)
console.print(
"The project metadata is missing the version. "
"Please ensure pyproject.toml has asp_version in \\[tool.agent-starter-pack]."
)
raise SystemExit(1)
new_version = get_current_version()
# Check if upgrade is needed
if old_version == new_version:
console.print(
f"[bold green]✅[/bold green] Project is already at version {new_version}"
)
return
# Check if uvx is available for re-templating old version
if not _ensure_uvx_available():
console.print(
"[bold red]Error:[/bold red] 'uvx' is required for upgrade but not installed."
)
console.print(
"[dim]Install uv to enable upgrade: curl -LsSf https://astral.sh/uv/install.sh | sh[/dim]"
)
raise SystemExit(1)
_display_version_header(old_version, new_version)
# Get project name and CLI args from metadata
project_name = metadata.get("name", project_dir.name)
agent_directory = metadata.get("agent_directory", "app")
cli_args = metadata_to_cli_args(metadata)
# Create temp directories for re-templating
temp_base = pathlib.Path(tempfile.mkdtemp(prefix="asp_upgrade_"))
old_template_dir = temp_base / "old"
new_template_dir = temp_base / "new"
try:
console.print("[dim]Generating template versions for comparison...[/dim]")
# Re-template old version
console.print(f"[dim] - Old template (v{old_version})...[/dim]")
if not _run_create_command(
cli_args, old_template_dir, project_name, old_version
):
console.print(
f"[bold red]Error:[/bold red] Failed to generate old template (v{old_version})"
)
console.print(
"[dim]This version may not be available. Try upgrading from a more recent version.[/dim]"
)
raise SystemExit(1)
# Re-template new version
console.print(f"[dim] - New template (v{new_version})...[/dim]")
if not _run_create_command(cli_args, new_template_dir, project_name):
console.print(
f"[bold red]Error:[/bold red] Failed to generate new template (v{new_version})"
)
raise SystemExit(1)
# The templates are created in subdirectories named after the project
old_template_project = old_template_dir / project_name
new_template_project = new_template_dir / project_name
console.print()
# Compare all files
console.print("[dim]Comparing files...[/dim]")
results = compare_all_files(
project_dir,
old_template_project,
new_template_project,
agent_directory,
)
# Group by action
groups = group_results_by_action(results)
# Handle dependency merging
dep_result = merge_pyproject_dependencies(
project_dir / "pyproject.toml",
old_template_project / "pyproject.toml",
new_template_project / "pyproject.toml",
)
console.print()
# Display results
_display_results(groups, dep_result.changes, dry_run)
# Check if there's anything to do
total_changes = (
len(groups["auto_update"])
+ len(groups["new"])
+ len(groups["removed"])
+ len(groups["conflict"])
)
if total_changes == 0 and not dep_result.changes:
console.print("[bold green]✅[/bold green] No changes needed!")
return
# Confirm before applying
if not auto_approve and not dry_run:
prompt_text = "\nProceed with upgrade?"
if groups["conflict"]:
prompt_text = "\nProceed? (you'll resolve conflicts next)"
proceed = Prompt.ask(
prompt_text,
choices=["y", "n"],
default="y",
)
if proceed != "y":
console.print("[yellow]Upgrade cancelled.[/yellow]")
return
# Apply changes
counts = _apply_changes(
groups,
project_dir,
new_template_project,
auto_approve,
dry_run,
)
# Apply dependency changes
if not dry_run and dep_result.changes:
write_merged_dependencies(
project_dir / "pyproject.toml",
dep_result.merged_deps,
)
# Update metadata version
if not dry_run:
_update_pyproject_metadata(project_dir, new_version)
# Summary
console.print()
if dry_run:
console.print(
"[bold yellow]Dry run complete.[/bold yellow] "
"Run without --dry-run to apply changes."
)
else:
console.print(f" Updated: {counts['updated']} files")
console.print(f" Added: {counts['added']} files")
console.print(f" Removed: {counts['removed']} files")
if counts["conflicts_kept"] or counts["conflicts_updated"]:
console.print(
f" Conflicts: {counts['conflicts_updated']} updated, "
f"{counts['conflicts_kept']} kept yours"
)
console.print()
console.print("[bold green]✅ Upgrade complete![/bold green]")
finally:
# Cleanup temp directories
shutil.rmtree(temp_base, ignore_errors=True)