from __future__ import annotations import logging from pathlib import Path from app.models import Mirror from app.services.host import host_path logger = logging.getLogger(__name__) def apply_pip(mirror: Mirror) -> dict: if not mirror.url: return {"success": False, "error": "URL میرور pip تعریف نشده"} conf_path = host_path("etc/pip.conf") conf_path.parent.mkdir(parents=True, exist_ok=True) content = f"""# Managed by Mirror Manager [global] index-url = {mirror.url.rstrip("/")} trusted-host = {mirror.url.split("//")[-1].split("/")[0]} """ conf_path.write_text(content, encoding="utf-8") return {"success": True, "message": f"pip mirror اعمال شد: {mirror.name}", "path": str(conf_path)} def apply_npm(mirror: Mirror) -> dict: if not mirror.url: return {"success": False, "error": "URL رجیstry npm تعریف نشده"} npmrc = host_path("root/.npmrc") npmrc.parent.mkdir(parents=True, exist_ok=True) content = f"# Managed by Mirror Manager\nregistry={mirror.url.rstrip('/')}\n" npmrc.write_text(content, encoding="utf-8") return {"success": True, "message": f"npm registry اعمال شد: {mirror.name}", "path": str(npmrc)}