Spaces:
Sleeping
Sleeping
File size: 11,424 Bytes
8edee29 | 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 | """
utils/dependency_manager.py
----------------------------
Dependency inference and installation for AutoDevAgent.
Before executing generated Python code, this module scans the code
for import statements, checks whether each package is already
available, and attempts to install missing ones via pip install --user.
Design:
- Uses Python's ast module to parse imports β more reliable than
regex since it handles multi-line and aliased imports correctly.
- Only attempts installation for packages that are not already
importable β avoids unnecessary pip calls.
- Maps common import names to their pip package names where they
differ (e.g. "cv2" β "opencv-python", "PIL" β "Pillow").
- Fails gracefully β if installation fails, returns a clear message
rather than crashing. The executor will then surface the error.
- C extensions and system-level packages will fail β documented in
README as a known limitation.
Known limitation:
- pip install --user works for pure Python libraries only.
Packages with C extensions (numpy, pandas, etc.) may fail on
HuggingFace Spaces free tier. Surface a clear message if so.
Usage:
from utils.dependency_manager import DependencyManager
manager = DependencyManager()
result = manager.resolve(code)
print(result.missing) # packages that were not installed
print(result.installed) # packages successfully installed
print(result.failed) # packages that failed to install
"""
import ast
import importlib
import logging
import subprocess
import sys
from dataclasses import dataclass, field
logger = logging.getLogger(__name__)
# ------------------------------------------------------------------ #
# Import name β pip package name mapping #
# #
# Many packages have different import names vs pip install names. #
# This map covers the most common cases seen in generated code. #
# ------------------------------------------------------------------ #
IMPORT_TO_PIP: dict[str, str] = {
"cv2": "opencv-python",
"PIL": "Pillow",
"sklearn": "scikit-learn",
"bs4": "beautifulsoup4",
"yaml": "pyyaml",
"dotenv": "python-dotenv",
"dateutil": "python-dateutil",
"attr": "attrs",
"gi": "PyGObject",
"wx": "wxPython",
"Crypto": "pycryptodome",
"serial": "pyserial",
"usb": "pyusb",
"magic": "python-magic",
"Image": "Pillow",
}
# Standard library modules β never attempt to pip install these
STDLIB_MODULES: frozenset[str] = frozenset({
"abc", "ast", "asyncio", "base64", "collections", "contextlib",
"copy", "csv", "datetime", "decimal", "difflib", "email", "enum",
"fnmatch", "functools", "glob", "hashlib", "heapq", "html",
"http", "inspect", "io", "itertools", "json", "logging", "math",
"multiprocessing", "operator", "os", "pathlib", "pickle",
"platform", "pprint", "queue", "random", "re", "shutil", "signal",
"socket", "sqlite3", "ssl", "stat", "string", "struct",
"subprocess", "sys", "tempfile", "textwrap", "threading", "time",
"timeit", "traceback", "typing", "unicodedata", "unittest",
"urllib", "uuid", "warnings", "weakref", "xml", "zipfile",
"zlib", "builtins", "gc", "getpass", "getopt", "gzip",
"importlib", "inspect", "keyword", "linecache", "locale",
"numbers", "os.path", "posixpath", "pdb", "profile", "pstats",
"pty", "pwd", "resource", "select", "shelve", "sysconfig",
"syslog", "termios", "tty", "types", "dataclasses", "fractions",
"statistics", "secrets", "token", "tokenize", "trace",
})
# ------------------------------------------------------------------ #
# Result dataclass #
# ------------------------------------------------------------------ #
@dataclass
class DependencyResult:
"""
Result of a dependency resolution attempt.
Attributes:
imports: All top-level package names found in the code.
available: Packages already importable β no action needed.
installed: Packages successfully installed by this call.
failed: Packages that could not be installed.
messages: Human-readable log of what happened per package.
"""
imports: list[str] = field(default_factory=list)
available: list[str] = field(default_factory=list)
installed: list[str] = field(default_factory=list)
failed: list[str] = field(default_factory=list)
messages: list[str] = field(default_factory=list)
@property
def all_resolved(self) -> bool:
"""True if every import is either available or successfully installed."""
return len(self.failed) == 0
def summary(self) -> str:
"""Return a one-line summary suitable for the UI status bar."""
if not self.imports:
return "No external imports detected."
parts = []
if self.available:
parts.append(f"{len(self.available)} already available")
if self.installed:
parts.append(f"{len(self.installed)} installed")
if self.failed:
parts.append(f"{len(self.failed)} failed: {', '.join(self.failed)}")
return " Β· ".join(parts) if parts else "All dependencies resolved."
# ------------------------------------------------------------------ #
# Manager #
# ------------------------------------------------------------------ #
class DependencyManager:
"""
Scans generated code for imports and installs missing packages.
Workflow:
1. Parse the code with ast to extract all import names.
2. Filter out stdlib modules.
3. For each remaining import: check if importable, skip if so.
4. For missing imports: attempt pip install --user.
5. Return a DependencyResult with full accounting.
"""
def resolve(self, code: str) -> DependencyResult:
"""
Resolve all dependencies for the given code string.
Args:
code: Raw Python source code to analyse.
Returns:
DependencyResult with installed/failed/available breakdown.
"""
result = DependencyResult()
# ββ Step 1: Extract all top-level import names ββββββββββββββ #
imports = _extract_imports(code)
result.imports = imports
if not imports:
logger.debug("DependencyManager: no external imports found")
return result
logger.info("DependencyManager: found imports: %s", imports)
# ββ Step 2: Check and install each package ββββββββββββββββββ #
for import_name in imports:
pip_name = IMPORT_TO_PIP.get(import_name, import_name)
if _is_importable(import_name):
result.available.append(import_name)
result.messages.append(f"β {import_name} β already available")
logger.debug("DependencyManager: %s already available", import_name)
continue
# Attempt installation
logger.info("DependencyManager: installing %s (pip: %s)", import_name, pip_name)
success, msg = _pip_install(pip_name)
if success:
result.installed.append(import_name)
result.messages.append(f"β {import_name} β installed successfully")
logger.info("DependencyManager: installed %s", pip_name)
else:
result.failed.append(import_name)
result.messages.append(
f"β {import_name} β install failed: {msg[:120]}"
)
logger.warning(
"DependencyManager: failed to install %s: %s", pip_name, msg[:120]
)
return result
# ------------------------------------------------------------------ #
# Module-level helpers #
# ------------------------------------------------------------------ #
def _extract_imports(code: str) -> list[str]:
"""
Parse Python source code and extract top-level package names.
Uses ast.parse so it handles all valid import forms:
- import os
- import os.path
- from os import path
- from os.path import join
- import numpy as np
Filters out stdlib modules automatically.
Args:
code: Raw Python source code.
Returns:
List of unique top-level package names that are not stdlib.
"""
imports: set[str] = set()
try:
tree = ast.parse(code)
except SyntaxError:
# Code has a syntax error β executor will catch this later
logger.debug("DependencyManager: syntax error while parsing imports")
return []
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
# Take only the top-level name: "os.path" β "os"
top = alias.name.split(".")[0]
if top not in STDLIB_MODULES:
imports.add(top)
elif isinstance(node, ast.ImportFrom):
if node.module:
top = node.module.split(".")[0]
if top not in STDLIB_MODULES:
imports.add(top)
return sorted(imports)
def _is_importable(package_name: str) -> bool:
"""
Check whether a package can be imported in the current environment.
Args:
package_name: Top-level package name (e.g. "numpy", "requests").
Returns:
True if importable, False otherwise.
"""
try:
importlib.import_module(package_name)
return True
except ImportError:
return False
def _pip_install(pip_name: str) -> tuple[bool, str]:
"""
Attempt to install a package via pip install --user.
Uses --user to avoid permission errors on HuggingFace Spaces and
other environments where the site-packages directory is read-only.
Uses --quiet to suppress verbose pip output in logs.
Args:
pip_name: The pip package name to install.
Returns:
Tuple of (success: bool, message: str).
"""
try:
proc = subprocess.run(
[
sys.executable, "-m", "pip", "install",
"--user", "--quiet", "--no-warn-script-location",
pip_name,
],
capture_output=True,
text=True,
timeout=60, # pip installs can be slow on cold starts
)
if proc.returncode == 0:
return True, "installed successfully"
else:
# Surface the most useful part of pip's error output
error_output = (proc.stderr or proc.stdout).strip()
lines = error_output.splitlines()
# Keep last 5 lines β pip errors tend to be most specific there
excerpt = "\n".join(lines[-5:]) if len(lines) > 5 else error_output
return False, excerpt
except subprocess.TimeoutExpired:
return False, "pip install timed out after 60 seconds"
except Exception as e:
return False, str(e)
|