Skip to content

Commit

Permalink
fail only when requested service is not available in available_services
Browse files Browse the repository at this point in the history
  • Loading branch information
Anas Husseini committed Dec 4, 2024
1 parent d00a445 commit 2130235
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
22 changes: 7 additions & 15 deletions craft_application/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
All errors inherit from craft_cli.CraftError.
"""

from __future__ import annotations

import os
Expand Down Expand Up @@ -275,7 +276,6 @@ class UbuntuProClientNotFoundError(UbuntuProApiError):
"""Raised when Ubuntu Pro client was not found on the system."""

def __init__(self, path: str) -> None:

message = f'The Ubuntu Pro client was not found on the system at "{path}"'

super().__init__(message=message)
Expand All @@ -285,7 +285,6 @@ class UbuntuProDetachedError(InvalidUbuntuProStateError):
"""Raised when Ubuntu Pro is not attached, but Pro services were requested."""

def __init__(self) -> None:

message = "Ubuntu Pro is requested, but was found detached."
resolution = 'Attach Ubuntu Pro to continue. See "pro" command for details.'

Expand All @@ -296,7 +295,6 @@ class UbuntuProAttachedError(InvalidUbuntuProStateError):
"""Raised when Ubuntu Pro is attached, but Pro services were not requested."""

def __init__(self) -> None:

message = "Ubuntu Pro is not requested, but was found attached."
resolution = 'Detach Ubuntu Pro to continue. See "pro" command for details.'

Expand All @@ -310,7 +308,6 @@ class InvalidUbuntuProServiceError(InvalidUbuntuProStateError):
# if so where is the list of supported service names?

def __init__(self, invalid_services: set[str]) -> None:

invalid_services_str = "".join(invalid_services)

message = "Invalid Ubuntu Pro Services were requested."
Expand All @@ -325,20 +322,15 @@ def __init__(self, invalid_services: set[str]) -> None:


class InvalidUbuntuProStatusError(InvalidUbuntuProStateError):
"""Raised when the incorrect set of Pro Services are enabled."""

def __init__(
self, requested_services: set[str], available_services: set[str]
) -> None:
"""Raised when a set of requested Pro Services are disabled."""

enable_services_str = " ".join(requested_services - available_services)
disable_services_str = " ".join(available_services - requested_services)
def __init__(self, requested_services: set[str]) -> None:
requested_services_str = ", ".join(requested_services)

message = "Incorrect Ubuntu Pro Services were enabled."
message = "Some of the requested Ubuntu Pro Services are disabled."
resolution = (
"Please enable or disable the following services.\n"
f"Enable: {enable_services_str}\n"
f"Disable: {disable_services_str}\n"
"Please enable the following services.\n"
f"Enable: {requested_services_str}\n"
'See "pro" command for details.'
)

Expand Down
4 changes: 2 additions & 2 deletions craft_application/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def process_part(
*, part_yaml_data: dict[str, Any], processor: GrammarProcessor
) -> dict[str, Any]:
"""Process grammar for a given part."""
for key in part_yaml_data:
for key, _ in part_yaml_data.items():
unprocessed_grammar = part_yaml_data[key]

# ignore non-grammar keywords
Expand Down Expand Up @@ -120,7 +120,7 @@ def self_check(value: Any) -> bool: # noqa: ANN401
# TODO: make checker optional in craft-grammar.
processor = GrammarProcessor(arch=arch, target_arch=target_arch, checker=self_check)

for part_name in parts_yaml_data:
for part_name, _ in parts_yaml_data.items():
parts_yaml_data[part_name] = process_part(
part_yaml_data=parts_yaml_data[part_name], processor=processor
)
Expand Down
24 changes: 15 additions & 9 deletions craft_application/util/pro_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class ProServices(set[str]):
pro_executable: Path | None = next(
(path for path in PRO_CLIENT_PATHS if path.exists()), None
)
# locations to check for pro client

def __str__(self) -> str:
"""Convert to string for display to user."""
Expand Down Expand Up @@ -168,7 +167,7 @@ def is_pro_attached(cls) -> bool:
return response["data"]["attributes"]["is_attached"] # type: ignore [no-any-return]

@classmethod
def get_pro_services(cls) -> ProServices:
def _get_pro_services(cls) -> set[str]:
"""Return set of enabled Ubuntu Pro services in the environment.
The returned set only includes services relevant to lifecycle commands.
Expand All @@ -179,9 +178,15 @@ def get_pro_services(cls) -> ProServices:
service_names = {service["name"] for service in enabled_services}

# remove any services that aren't relevant to build services
service_names = service_names.intersection(cls.supported_services)
return service_names.intersection(cls.supported_services)

@classmethod
def get_pro_services(cls) -> ProServices:
"""Return a class of enabled Ubuntu Pro services in the environment.
return cls(service_names)
The returned set only includes services relevant to lifecycle commands.
"""
return cls(cls._get_pro_services())

def validate(
self,
Expand All @@ -205,21 +210,22 @@ def validate(
raise UbuntuProDetachedError

if (
ValidatorOptions._DETACHED in options
ValidatorOptions._DETACHED in options # type: ignore [reportPrivateUsage]
and not self
and not self.managed_mode
): # type: ignore [reportPrivateUsage]
):
# Pro rock is not requested but the host is attached
raise UbuntuProAttachedError

# second, check that the set of enabled pro services in the environment matches
# the services specified in this set
available_services = self._get_pro_services()
if (
ValidatorOptions.ENABLEMENT in options
and ((available_services := self.get_pro_services()) != self)
and not self.managed_mode
and str(self) != self.empty_placeholder
and not self.issubset(available_services)
):
raise InvalidUbuntuProStatusError(self, available_services)
raise InvalidUbuntuProStatusError(self)

except UbuntuProClientNotFoundError:
# If The pro client was not found, we may be on a non Ubuntu
Expand Down

0 comments on commit 2130235

Please sign in to comment.