|
1 | 1 | # type: ignore |
2 | 2 | import json |
3 | 3 | import os |
| 4 | +import subprocess |
| 5 | +import sys |
4 | 6 | import uuid |
5 | 7 | import zipfile |
6 | 8 | from string import Template |
@@ -52,6 +54,80 @@ def check_config(directory): |
52 | 54 | } |
53 | 55 |
|
54 | 56 |
|
| 57 | +def is_uv_available(): |
| 58 | + """Check if uv command is available in the system.""" |
| 59 | + try: |
| 60 | + subprocess.run(["uv", "--version"], capture_output=True, check=True) |
| 61 | + return True |
| 62 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 63 | + return False |
| 64 | + |
| 65 | + |
| 66 | +def is_uv_project(directory): |
| 67 | + """Check if this is a uv project by looking for the uv.lock file.""" |
| 68 | + uv_lock_path = os.path.join(directory, "uv.lock") |
| 69 | + |
| 70 | + # If uv.lock exists, it's definitely a uv project |
| 71 | + if os.path.exists(uv_lock_path): |
| 72 | + return True |
| 73 | + |
| 74 | + return False |
| 75 | + |
| 76 | + |
| 77 | +def is_in_virtual_env(): |
| 78 | + """Check if we're currently running in a virtual environment.""" |
| 79 | + return ( |
| 80 | + hasattr(sys, "real_prefix") |
| 81 | + or (hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix) |
| 82 | + or os.environ.get("VIRTUAL_ENV") is not None |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def run_uv_sync(directory): |
| 87 | + """Run uv sync in the specified directory.""" |
| 88 | + try: |
| 89 | + subprocess.run( |
| 90 | + ["uv", "sync"], cwd=directory, capture_output=True, text=True, check=True |
| 91 | + ) |
| 92 | + return True |
| 93 | + except subprocess.CalledProcessError as e: |
| 94 | + console.warning(f"uv sync failed: {e.stderr}") |
| 95 | + return False |
| 96 | + except FileNotFoundError: |
| 97 | + console.warning("uv command not found. Skipping dependency sync.") |
| 98 | + return False |
| 99 | + |
| 100 | + |
| 101 | +def run_uv_lock(directory): |
| 102 | + """Run uv lock to update the lock file.""" |
| 103 | + try: |
| 104 | + subprocess.run( |
| 105 | + ["uv", "lock"], cwd=directory, capture_output=True, text=True, check=True |
| 106 | + ) |
| 107 | + return True |
| 108 | + except subprocess.CalledProcessError as e: |
| 109 | + console.warning(f"uv lock failed: {e.stderr}") |
| 110 | + return False |
| 111 | + except FileNotFoundError: |
| 112 | + console.warning("uv command not found. Skipping lock file update.") |
| 113 | + return False |
| 114 | + |
| 115 | + |
| 116 | +def handle_uv_operations(directory): |
| 117 | + """Handle uv operations if uv is detected and available.""" |
| 118 | + if not is_uv_available(): |
| 119 | + return |
| 120 | + |
| 121 | + if not is_uv_project(directory): |
| 122 | + return |
| 123 | + |
| 124 | + # If we're in a virtual environment |
| 125 | + if is_in_virtual_env(): |
| 126 | + run_uv_sync(directory) |
| 127 | + # Always run uv lock to ensure lock file is up to date |
| 128 | + run_uv_lock(directory) |
| 129 | + |
| 130 | + |
55 | 131 | def generate_operate_file(entryPoints): |
56 | 132 | project_id = str(uuid.uuid4()) |
57 | 133 |
|
@@ -403,6 +479,10 @@ def pack(root): |
403 | 479 |
|
404 | 480 | with console.spinner("Packaging project ..."): |
405 | 481 | try: |
| 482 | + # Handle uv operations before packaging |
| 483 | + # Makes sure uv.lock is up to date |
| 484 | + handle_uv_operations(root) |
| 485 | + |
406 | 486 | pack_fn( |
407 | 487 | config["project_name"], |
408 | 488 | config["description"], |
|
0 commit comments