fix: restore platform kit version to 1.0.9
All checks were successful
Publish Python Package to Gitea / publish (push) Successful in 25s
All checks were successful
Publish Python Package to Gitea / publish (push) Successful in 25s
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import fnmatch
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import shutil
|
||||
@@ -110,6 +112,144 @@ def _platform_tool_paths(root: Path) -> tuple[Path, Path]:
|
||||
)
|
||||
|
||||
|
||||
def _read_manifest(root: Path) -> dict | None:
|
||||
manifest_path = root / "manifest.json"
|
||||
if not manifest_path.is_file():
|
||||
return None
|
||||
try:
|
||||
return json.loads(manifest_path.read_text(encoding="utf-8"))
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return None
|
||||
|
||||
|
||||
def _ffmpeg_platform_spec(manifest: dict) -> dict | None:
|
||||
tools = manifest.get("tools")
|
||||
if not isinstance(tools, dict):
|
||||
return None
|
||||
ffmpeg = tools.get("ffmpeg")
|
||||
if not isinstance(ffmpeg, dict):
|
||||
return None
|
||||
spec = ffmpeg.get(_platform_bin_key())
|
||||
if isinstance(spec, dict):
|
||||
return spec
|
||||
return None
|
||||
|
||||
|
||||
def _manifest_tool_urls(spec: dict) -> list[tuple[str, str]]:
|
||||
urls: list[tuple[str, str]] = []
|
||||
raw_urls = spec.get("urls")
|
||||
if isinstance(raw_urls, list):
|
||||
for item in raw_urls:
|
||||
if isinstance(item, dict):
|
||||
name = str(item.get("name") or "source")
|
||||
url = str(item.get("url") or "").strip()
|
||||
if url:
|
||||
urls.append((name, url))
|
||||
legacy = str(spec.get("url") or "").strip()
|
||||
if legacy:
|
||||
urls.append(("legacy", legacy))
|
||||
return urls
|
||||
|
||||
|
||||
def _match_archive_member(member_name: str, pattern: str) -> bool:
|
||||
normalized = member_name.replace("\\", "/").lstrip("/")
|
||||
return fnmatch.fnmatch(normalized, pattern)
|
||||
|
||||
|
||||
def _extract_tool_members(
|
||||
zip_path: Path,
|
||||
archive_paths: dict[str, str],
|
||||
) -> dict[str, bytes]:
|
||||
found: dict[str, bytes] = {}
|
||||
with zipfile.ZipFile(zip_path, "r") as zf:
|
||||
for tool_name, pattern in archive_paths.items():
|
||||
if not isinstance(pattern, str) or not pattern:
|
||||
continue
|
||||
for info in zf.infolist():
|
||||
if info.is_dir():
|
||||
continue
|
||||
if _match_archive_member(info.filename, pattern):
|
||||
found[tool_name] = zf.read(info)
|
||||
break
|
||||
return found
|
||||
|
||||
|
||||
def _try_download_ffmpeg_tools(root: Path, warnings: list[str]) -> bool:
|
||||
manifest = _read_manifest(root)
|
||||
if manifest is None:
|
||||
warnings.append("ffmpeg_manifest_missing")
|
||||
return False
|
||||
|
||||
spec = _ffmpeg_platform_spec(manifest)
|
||||
if spec is None:
|
||||
warnings.append("ffmpeg_manifest_missing")
|
||||
return False
|
||||
|
||||
archive_paths = spec.get("archive_paths")
|
||||
if not isinstance(archive_paths, dict):
|
||||
warnings.append("ffmpeg_manifest_missing")
|
||||
return False
|
||||
|
||||
urls = _manifest_tool_urls(spec)
|
||||
if not urls:
|
||||
warnings.append("ffmpeg_tool_download_failed")
|
||||
return False
|
||||
|
||||
ffmpeg_path, ffprobe_path = _platform_tool_paths(root)
|
||||
bin_dir = ffmpeg_path.parent
|
||||
download_root = _download_dir(_shared_parent(root)) / "ffmpeg-tools"
|
||||
download_root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
for source_name, url in urls:
|
||||
zip_path = download_root / f"{source_name}.zip"
|
||||
try:
|
||||
if zip_path.exists():
|
||||
zip_path.unlink()
|
||||
_download_zip(url, zip_path)
|
||||
members = _extract_tool_members(zip_path, archive_paths)
|
||||
ffmpeg_bytes = members.get("ffmpeg")
|
||||
ffprobe_bytes = members.get("ffprobe")
|
||||
if not ffmpeg_bytes or not ffprobe_bytes:
|
||||
continue
|
||||
bin_dir.mkdir(parents=True, exist_ok=True)
|
||||
ffmpeg_path.write_bytes(ffmpeg_bytes)
|
||||
ffprobe_path.write_bytes(ffprobe_bytes)
|
||||
shutil.rmtree(download_root, ignore_errors=True)
|
||||
return True
|
||||
except urllib.error.URLError:
|
||||
continue
|
||||
except (zipfile.BadZipFile, OSError, KeyError):
|
||||
continue
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
warnings.append("ffmpeg_tool_download_failed")
|
||||
return False
|
||||
|
||||
|
||||
def _maybe_ensure_ffmpeg_tools(root: Path, status: MediaAssetsStatus) -> MediaAssetsStatus:
|
||||
if status.ready:
|
||||
return status
|
||||
if not status.exists:
|
||||
return status
|
||||
if "ffmpeg_missing" not in status.warnings and "ffprobe_missing" not in status.warnings:
|
||||
return status
|
||||
|
||||
warnings = list(status.warnings)
|
||||
if _try_download_ffmpeg_tools(root, warnings):
|
||||
refreshed = _inspect_media_assets(root, source=status.source)
|
||||
for warning in warnings:
|
||||
if warning not in refreshed.warnings:
|
||||
refreshed.warnings.append(warning)
|
||||
return refreshed
|
||||
|
||||
refreshed = _inspect_media_assets(root, source=status.source)
|
||||
for warning in warnings:
|
||||
if warning not in refreshed.warnings:
|
||||
refreshed.warnings.append(warning)
|
||||
return refreshed
|
||||
|
||||
|
||||
def _inspect_media_assets(root: Path, *, source: str) -> MediaAssetsStatus:
|
||||
warnings: list[str] = []
|
||||
exists = root.exists()
|
||||
@@ -340,6 +480,17 @@ def _media_assets_root_overridden(env: dict[str, str] | None) -> bool:
|
||||
return bool((env_map.get("MEDIA_ASSETS_ROOT") or "").strip())
|
||||
|
||||
|
||||
def _needs_media_repo_content(status: MediaAssetsStatus) -> bool:
|
||||
return any(
|
||||
warning in status.warnings
|
||||
for warning in (
|
||||
"music_dir_missing",
|
||||
"fonts_dir_missing",
|
||||
"watermark_dir_missing",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def ensure_media_assets(
|
||||
data_root: str | Path | None = None,
|
||||
env: dict[str, str] | None = None,
|
||||
@@ -352,7 +503,10 @@ def ensure_media_assets(
|
||||
|
||||
status = _inspect_media_assets(root, source="local")
|
||||
if overridden:
|
||||
return status
|
||||
return _maybe_ensure_ffmpeg_tools(root, status)
|
||||
|
||||
if status.exists and not status.ready and not update and not _needs_media_repo_content(status):
|
||||
return _maybe_ensure_ffmpeg_tools(root, status)
|
||||
|
||||
if status.ready and not update:
|
||||
return status
|
||||
@@ -361,30 +515,39 @@ def ensure_media_assets(
|
||||
if _is_git_repo(root):
|
||||
warnings: list[str] = []
|
||||
if _try_download_via_git(root, warnings, clone=False):
|
||||
return _inspect_media_assets(root, source="git")
|
||||
return _maybe_ensure_ffmpeg_tools(
|
||||
root,
|
||||
_inspect_media_assets(root, source="git"),
|
||||
)
|
||||
status = _inspect_media_assets(root, source="local")
|
||||
status.warnings.extend(warnings)
|
||||
return status
|
||||
return _maybe_ensure_ffmpeg_tools(root, status)
|
||||
|
||||
shared = _shared_parent(root)
|
||||
shared.mkdir(parents=True, exist_ok=True)
|
||||
warnings: list[str] = []
|
||||
|
||||
if _try_download_via_zip(shared, root, warnings):
|
||||
return _inspect_media_assets(root, source="zip")
|
||||
return _maybe_ensure_ffmpeg_tools(
|
||||
root,
|
||||
_inspect_media_assets(root, source="zip"),
|
||||
)
|
||||
|
||||
clone_needed = not root.exists() or not status.ready or update
|
||||
if _try_download_via_git(root, warnings, clone=clone_needed):
|
||||
return _inspect_media_assets(
|
||||
return _maybe_ensure_ffmpeg_tools(
|
||||
root,
|
||||
source="git" if _is_git_repo(root) else "local",
|
||||
_inspect_media_assets(
|
||||
root,
|
||||
source="git" if _is_git_repo(root) else "local",
|
||||
),
|
||||
)
|
||||
|
||||
final = _inspect_media_assets(root, source="local")
|
||||
for warning in warnings:
|
||||
if warning not in final.warnings:
|
||||
final.warnings.append(warning)
|
||||
return final
|
||||
return _maybe_ensure_ffmpeg_tools(root, final)
|
||||
|
||||
|
||||
def resolve_ffmpeg(
|
||||
|
||||
Reference in New Issue
Block a user