Spaces:
Sleeping
Sleeping
Create app/environment.py
Browse files- app/environment.py +59 -0
app/environment.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Environment configuration management."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
from app.config import EnvType, Settings, get_settings, update_settings
|
| 8 |
+
from app.executor import reset_executor
|
| 9 |
+
from app.models import EnvironmentConfig
|
| 10 |
+
from app.package_manager import reset_package_manager
|
| 11 |
+
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_environment_config() -> EnvironmentConfig:
|
| 16 |
+
"""Get the current environment configuration."""
|
| 17 |
+
settings = get_settings()
|
| 18 |
+
return EnvironmentConfig(
|
| 19 |
+
env_type=settings.env_type.value,
|
| 20 |
+
conda_env_name=settings.conda_env_name,
|
| 21 |
+
venv_path=settings.venv_path,
|
| 22 |
+
uv_venv_path=settings.uv_venv_path,
|
| 23 |
+
python_executable=settings.get_python_executable(),
|
| 24 |
+
code_storage_dir=settings.code_storage_dir,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def configure_environment(
|
| 29 |
+
env_type: str,
|
| 30 |
+
conda_name: str | None = None,
|
| 31 |
+
venv_path: str | None = None,
|
| 32 |
+
uv_venv_path: str | None = None,
|
| 33 |
+
) -> EnvironmentConfig:
|
| 34 |
+
"""Dynamically update the environment configuration."""
|
| 35 |
+
update_kwargs = {"env_type": EnvType(env_type)}
|
| 36 |
+
|
| 37 |
+
if env_type == "conda":
|
| 38 |
+
if not conda_name:
|
| 39 |
+
raise ValueError("conda_name is required for conda environment type")
|
| 40 |
+
update_kwargs["conda_env_name"] = conda_name
|
| 41 |
+
elif env_type == "venv":
|
| 42 |
+
if not venv_path:
|
| 43 |
+
raise ValueError("venv_path is required for venv environment type")
|
| 44 |
+
update_kwargs["venv_path"] = venv_path
|
| 45 |
+
elif env_type == "venv-uv":
|
| 46 |
+
if not uv_venv_path:
|
| 47 |
+
raise ValueError("uv_venv_path is required for venv-uv environment type")
|
| 48 |
+
update_kwargs["uv_venv_path"] = uv_venv_path
|
| 49 |
+
else:
|
| 50 |
+
raise ValueError(f"Invalid environment type: {env_type}")
|
| 51 |
+
|
| 52 |
+
new_settings = update_settings(**update_kwargs)
|
| 53 |
+
|
| 54 |
+
# Reset executor and package manager with new settings
|
| 55 |
+
reset_executor(new_settings)
|
| 56 |
+
reset_package_manager(new_settings)
|
| 57 |
+
|
| 58 |
+
logger.info("Environment reconfigured to: %s", env_type)
|
| 59 |
+
return get_environment_config()
|