Skip to content

Commit

Permalink
addons - Modify all moving parts so we use rez
Browse files Browse the repository at this point in the history
We want to use `rez` to find and load addons, this commit changes all
the parts where we interacted with the `AddonLibrary` to adapt it to the
`RezRepo` class.
  • Loading branch information
Minkiu committed Jun 21, 2023
1 parent 4e78497 commit 46d4418
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 254 deletions.
23 changes: 12 additions & 11 deletions api/addons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ def register_addon_endpoints():
tags=[f"{addon_name} {addon_version}"],
)

library = AddonLibrary.getinstance()
for addon_name, addon_definition in library.items():
for version in addon_definition.versions:
addon = addon_definition.versions[version]
for addon_name, addon_definition in addons_library.items():
for package in addon_definition.iter_packages():
version = package.version
addon = addons_library.addon(addon_name, version)
print(addon)
print(type(addon))
addon_router = APIRouter(
prefix=f"/{addon_name}/{version}",
tags=[f"{addon_definition.friendly_name} {version}"],
tags=[f"{addon_definition.name} {version}"],
)

for route in addon_router.routes:
Expand Down Expand Up @@ -102,16 +104,15 @@ async def list_addons(
active_versions = await addon_library.get_active_versions()

for addon_name, addon_version_dict in addon_library.get_available_addons().items():
addon_active_versions = active_versions.get(addon_name)
addon_active_versions = active_versions.get(addon_name, {})

if not addon_active_versions:
addon = addon_library.addon(addon_name, addon_version_dict["latest"].version)
latest_version = addon_library.get_latest_version(addon_name)
addon = addon_library.addon(addon_name, str(latest_version.version))
else:
addon = addon_library.addon(addon_name, addon_active_versions["production"])
addon = addon_library.addon(addon_name, str(addon_active_versions["production"]))
logging.error(f"ADDON IS {addon} ({type(addon)})")

addon_version_dict.pop("latest")

versions = {}

for version_number in addon_version_dict:
Expand Down Expand Up @@ -142,7 +143,7 @@ async def list_addons(
result.append(
AddonListItem(
name=addon.name,
title=addon.friendly_name,
title=addon.name,
versions=versions,
description=addon.__doc__ or "",
production_version=addon_active_versions.get("production"),
Expand Down
12 changes: 8 additions & 4 deletions api/addons/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ async def remove_override(
variant: str = "production",
project_name: str | None = None,
):
if (addon := AddonLibrary.addon(addon_name, addon_version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, addon_version)) is None:
raise NotFoundException(f"Addon {addon_name} {addon_version} not found")

# TODO: ensure the path is not a part of a group
Expand Down Expand Up @@ -62,7 +63,8 @@ async def pin_override(
variant: str = "production",
project_name: str | None = None,
):
if (addon := AddonLibrary.addon(addon_name, addon_version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, addon_version)) is None:
raise NotFoundException(f"Addon {addon_name} {addon_version} not found")

if project_name:
Expand Down Expand Up @@ -136,7 +138,8 @@ async def remove_site_override(
user_name: str,
path: list[str],
):
if (addon := AddonLibrary.addon(addon_name, addon_version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, addon_version)) is None:
raise NotFoundException(f"Addon {addon_name} {addon_version} not found")

overrides = await addon.get_project_site_overrides(project_name, user_name, site_id)
Expand Down Expand Up @@ -171,7 +174,8 @@ async def pin_site_override(
user_name: str,
path: list[str],
):
if (addon := AddonLibrary.addon(addon_name, addon_version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, addon_version)) is None:
raise NotFoundException(f"Addon {addon_name} {addon_version} not found")

overrides = await addon.get_project_site_overrides(project_name, user_name, site_id)
Expand Down
17 changes: 11 additions & 6 deletions api/addons/project_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ async def get_addon_project_settings_schema(
site: str | None = Query(None, regex="^[a-z0-9-]+$"),
) -> dict[str, Any]:
"""Return the JSON schema of the addon settings."""

if (addon := AddonLibrary.addon(addon_name, version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, version)) is None:
raise NotFoundException(f"Addon {addon_name} {version} not found")

model = addon.get_settings_model()
Expand Down Expand Up @@ -76,7 +76,9 @@ async def get_addon_project_settings(
variant: str = Query("production"),
site: str | None = Query(None, regex="^[a-z0-9-]+$"),
) -> dict[str, Any]:
if (addon := AddonLibrary.addon(addon_name, version)) is None:

addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, version)) is None:
raise NotFoundException(f"Addon {addon_name} {version} not found")

if site:
Expand All @@ -98,7 +100,8 @@ async def get_addon_project_overrides(
variant: str = Query("production"),
site: str | None = Query(None, regex="^[a-z0-9-]+$"),
):
addon = AddonLibrary.addon(addon_name, version)
addon_library = AddonLibrary()
addon = addon_library.addon(addon_name, version)
studio_settings = await addon.get_studio_settings(variant=variant)
if studio_settings is None:
return {}
Expand Down Expand Up @@ -140,7 +143,8 @@ async def set_addon_project_settings(
) -> EmptyResponse:
"""Set the studio overrides of the given addon."""

addon = AddonLibrary.addon(addon_name, version)
addon_library = AddonLibrary()
addon = addon_library.addon(addon_name, version)
model = addon.get_settings_model()
if model is None:
raise BadRequestException(f"Addon {addon_name} has no settings")
Expand Down Expand Up @@ -237,7 +241,8 @@ async def delete_addon_project_overrides(
site: str | None = Query(None, regex="^[a-z0-9-]+$"),
):
# Ensure the addon and the project exist
_ = AddonLibrary.addon(addon_name, version)
addon_library = AddonLibrary()
_ = addon_library.addon(addon_name, version)
_ = await ProjectEntity.load(project_name)

if not site:
Expand Down
11 changes: 6 additions & 5 deletions api/addons/site_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ async def get_addon_site_settings_schema(
user: CurrentUser,
) -> dict[str, Any]:
"""Return the JSON schema of the addon site settings."""

if (addon := AddonLibrary.addon(addon_name, version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, version)) is None:
raise NotFoundException(f"Addon {addon_name} {version} not found")

model = addon.get_site_settings_model()
Expand All @@ -47,8 +47,8 @@ async def get_addon_site_settings(
site: str = Query(...),
) -> dict[str, Any]:
"""Return the JSON schema of the addon site settings."""

if (addon := AddonLibrary.addon(addon_name, version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, version)) is None:
raise NotFoundException(f"Addon {addon_name} {version} not found")

model = addon.get_site_settings_model()
Expand Down Expand Up @@ -77,7 +77,8 @@ async def set_addon_site_settings(
user: CurrentUser,
site: str = Query(..., title="Site ID", regex="^[a-z0-9-]+$"),
) -> EmptyResponse:
if (addon := AddonLibrary.addon(addon_name, version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, version)) is None:
raise NotFoundException(f"Addon {addon_name} {version} not found")

model = addon.get_site_settings_model()
Expand Down
19 changes: 10 additions & 9 deletions api/addons/studio_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ async def get_addon_settings_schema(
user: CurrentUser,
) -> dict[str, Any]:
"""Return the JSON schema of the addon settings."""

if (addon := AddonLibrary.addon(addon_name, addon_version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, addon_version)) is None:
raise NotFoundException(f"Addon {addon_name} {addon_version} not found")

model = addon.get_settings_model()
Expand All @@ -58,8 +58,8 @@ async def get_addon_studio_settings(
variant: str = Query("production"),
) -> dict[str, Any]:
"""Return the settings (including studio overrides) of the given addon."""

if (addon := AddonLibrary.addon(addon_name, addon_version)) is None:
addon_library = AddonLibrary()
if (addon := addon_library.addon(addon_name, addon_version)) is None:
raise NotFoundException(f"Addon {addon_name} {addon_version} not found")

settings = await addon.get_studio_settings(variant=variant)
Expand All @@ -80,8 +80,8 @@ async def set_addon_studio_settings(

if not user.is_manager:
raise ForbiddenException

addon = AddonLibrary.addon(addon_name, addon_version)
addon_library = AddonLibrary()
addon = addon_library.addon(addon_name, addon_version)
original = await addon.get_studio_settings(variant=variant)
existing = await addon.get_studio_overrides(variant=variant)
model = addon.get_settings_model()
Expand Down Expand Up @@ -138,8 +138,8 @@ async def get_addon_studio_overrides(
):
if not user.is_manager:
raise ForbiddenException

addon = AddonLibrary.addon(addon_name, addon_version)
addon_library = AddonLibrary()
addon = addon_library.addon(addon_name, addon_version)
settings = await addon.get_studio_settings(variant=variant)
if settings is None:
return {}
Expand All @@ -160,7 +160,8 @@ async def delete_addon_studio_overrides(
raise ForbiddenException

# Ensure addon exists
_ = AddonLibrary.addon(addon_name, addon_version)
addon_library = AddonLibrary()
_ = addon_library.addon(addon_name, addon_version)

await Postgres.execute(
"""
Expand Down
2 changes: 1 addition & 1 deletion ayon_server/addons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

from ayon_server.addons.addon import BaseServerAddon
from ayon_server.addons.definition import ServerAddonDefinition
from ayon_server.addons.library import AddonLibrary
from ayon_server.addons.rezlibrary import RezRepo as AddonLibrary
from ayon_server.addons.models import SSOOption
8 changes: 4 additions & 4 deletions ayon_server/addons/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def __init__(self, definition: "ServerAddonDefinition", addon_dir: str):
def __repr__(self) -> str:
return f"<Addon name='{self.definition.name}' version='{self.version}'>"

@property
def friendly_name(self) -> str:
"""Return the friendly name of the addon."""
return f"{self.definition.friendly_name} {self.version}"
# @property
# def friendly_name(self) -> str:
# """Return the friendly name of the addon."""
# return f"{self.definition.friendly_name} {self.version}"

def initialize(self) -> None:
"""Initialize the addon.
Expand Down
14 changes: 1 addition & 13 deletions ayon_server/addons/rezlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
from pathlib import Path
from typing import ItemsView

<<<<<<< HEAD
from nxtools import logging, slugify
from rez.exceptions import PackageFamilyNotFoundError
from rez.packages import Package, PackageFamily, get_package, get_latest_package
from rez.package_repository import package_repository_manager
from rez.package_search import get_plugins
=======
from nxtools import log_traceback, logging
from rez.packages import PackageFamily, get_latest_package, get_plugins
from rez.package_repository import package_repository_manager
>>>>>>> d829e4c (Improved the Rez library intitialization)

from ayon_server.addons.addon import BaseServerAddon
from ayon_server.addons.utils import classes_from_module, import_module
Expand Down Expand Up @@ -107,7 +101,6 @@ def _init_addon_from_rez_package(self, module_name, rez_package):
except AttributeError as e:
logging.error(f"Addon {module_name} - {module_init_path} is not a valid Python module.")
raise e

# It makes little sense to allow several addons from one init,
# since they would be separate rez-packages
addon_class = next(iter(classes_from_module(BaseServerAddon, addon_module)), None)
Expand All @@ -131,18 +124,13 @@ def addon(self, addon_name, addon_version):

if not addon:
# Attempt to find it and initialize it
logging.error(f"PACKAGES ARE: {self.packages}")
logging.error(f"ADDON NAME: {addon_name}")
logging.error(f"IN PKGS: {self.packages.get(addon_name)}")
logging.error(f"ADDON VERSION: {addon_version}")
logging.error(f"IN PKGS: {self.packages[addon_name].get(addon_version)}")
addon_package = self.packages.get(addon_name, {}).get(addon_version, None)

if not addon_package:
raise NotFoundException(f"Unable to find addon {addon_name}-{addon_version}")

try:
addon = self.init_addon_from_rez_package_addon(
addon = self._init_addon_from_rez_package(
module_name,
addon_package
)
Expand Down
1 change: 1 addition & 0 deletions ayon_server/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ async def startup_event() -> None:
start_event = await dispatch_event("server.started", finished=False)

library = AddonLibrary()

if library.restart_requested:
logging.warning("Restart requested, skipping addon setup")
await dispatch_event(
Expand Down
2 changes: 1 addition & 1 deletion ayon_server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AyonConfig(BaseModel):
)

rez_dir: str = Field(
default="/rezrepo",
default="/addons",
description="Path to the directory containing the Rez repository.",
)

Expand Down
84 changes: 0 additions & 84 deletions rezrepo/ayon_shotgrid/1.0.0/build.rxt

This file was deleted.

Loading

0 comments on commit 46d4418

Please sign in to comment.