Spaces:
Sleeping
Sleeping
File size: 5,249 Bytes
e2812ac |
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 |
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""
Validation utilities for multi-mode deployment readiness.
This module provides functions to check if environments are properly
configured for multi-mode deployment (Docker, direct Python, notebooks, clusters).
"""
import subprocess
import tomllib
from pathlib import Path
def validate_multi_mode_deployment(env_path: Path) -> tuple[bool, list[str]]:
"""
Validate that an environment is ready for multi-mode deployment.
Checks:
1. pyproject.toml exists
2. uv.lock exists and is up-to-date
3. pyproject.toml has [project.scripts] with server entry point
4. server/app.py has a main() function
5. Required dependencies are present
Returns:
Tuple of (is_valid, list of issues found)
"""
issues = []
# Check pyproject.toml exists
pyproject_path = env_path / "pyproject.toml"
if not pyproject_path.exists():
issues.append("Missing pyproject.toml")
return False, issues
# Check uv.lock exists
lockfile_path = env_path / "uv.lock"
if not lockfile_path.exists():
issues.append("Missing uv.lock - run 'uv lock' to generate it")
else:
# Check if uv.lock is up-to-date (optional, can be expensive)
# We can add a check using `uv lock --check` if needed
try:
result = subprocess.run(
["uv", "lock", "--check", "--directory", str(env_path)],
capture_output=True,
text=True,
timeout=5,
)
if result.returncode != 0:
issues.append(
"uv.lock is out of date with pyproject.toml - run 'uv lock' to update"
)
except (subprocess.TimeoutExpired, FileNotFoundError):
# If uv is not available or times out, skip this check
pass
# Parse pyproject.toml
try:
with open(pyproject_path, "rb") as f:
pyproject = tomllib.load(f)
except Exception as e:
issues.append(f"Failed to parse pyproject.toml: {e}")
return False, issues
# Check [project.scripts] section
scripts = pyproject.get("project", {}).get("scripts", {})
if "server" not in scripts:
issues.append("Missing [project.scripts] server entry point")
# Check server entry point format
server_entry = scripts.get("server", "")
if server_entry and ":main" not in server_entry:
issues.append(
f"Server entry point should reference main function, got: {server_entry}"
)
# Check required dependencies
deps = [dep.lower() for dep in pyproject.get("project", {}).get("dependencies", [])]
has_openenv = any(
dep.startswith("openenv") and not dep.startswith("openenv-core") for dep in deps
)
has_legacy_core = any(dep.startswith("openenv-core") for dep in deps)
if not (has_openenv or has_legacy_core):
issues.append("Missing required dependency: openenv>=0.2.0")
elif has_legacy_core and not has_openenv:
issues.append(
"Dependency on openenv-core is deprecated; use openenv>=0.2.0 instead"
)
# Check server/app.py exists
server_app = env_path / "server" / "app.py"
if not server_app.exists():
issues.append("Missing server/app.py")
else:
# Check for main() function (flexible - with or without parameters)
app_content = server_app.read_text(encoding="utf-8")
if "def main(" not in app_content:
issues.append("server/app.py missing main() function")
# Check if main() is callable
if "__name__" not in app_content or "main()" not in app_content:
issues.append(
"server/app.py main() function not callable (missing if __name__ == '__main__')"
)
return len(issues) == 0, issues
def get_deployment_modes(env_path: Path) -> dict[str, bool]:
"""
Check which deployment modes are supported by the environment.
Returns:
Dictionary with deployment mode names and whether they're supported
"""
modes = {
"docker": False,
"openenv_serve": False,
"uv_run": False,
"python_module": False,
}
# Check Docker
dockerfile = env_path / "server" / "Dockerfile"
modes["docker"] = dockerfile.exists()
# Check multi-mode deployment readiness
is_valid, _ = validate_multi_mode_deployment(env_path)
if is_valid:
modes["openenv_serve"] = True
modes["uv_run"] = True
modes["python_module"] = True
return modes
def format_validation_report(env_name: str, is_valid: bool, issues: list[str]) -> str:
"""
Format a validation report for display.
Returns:
Formatted report string
"""
if is_valid:
return f"[OK] {env_name}: Ready for multi-mode deployment"
report = [f"[FAIL] {env_name}: Not ready for multi-mode deployment", ""]
report.append("Issues found:")
for issue in issues:
report.append(f" - {issue}")
return "\n".join(report)
|