Skip to content

Commit 2130235

Browse files
author
Anas Husseini
committed
fail only when requested service is not available in available_services
1 parent d00a445 commit 2130235

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

craft_application/errors.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
1818
All errors inherit from craft_cli.CraftError.
1919
"""
20+
2021
from __future__ import annotations
2122

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

277278
def __init__(self, path: str) -> None:
278-
279279
message = f'The Ubuntu Pro client was not found on the system at "{path}"'
280280

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

287287
def __init__(self) -> None:
288-
289288
message = "Ubuntu Pro is requested, but was found detached."
290289
resolution = 'Attach Ubuntu Pro to continue. See "pro" command for details.'
291290

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

298297
def __init__(self) -> None:
299-
300298
message = "Ubuntu Pro is not requested, but was found attached."
301299
resolution = 'Detach Ubuntu Pro to continue. See "pro" command for details.'
302300

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

312310
def __init__(self, invalid_services: set[str]) -> None:
313-
314311
invalid_services_str = "".join(invalid_services)
315312

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

326323

327324
class InvalidUbuntuProStatusError(InvalidUbuntuProStateError):
328-
"""Raised when the incorrect set of Pro Services are enabled."""
329-
330-
def __init__(
331-
self, requested_services: set[str], available_services: set[str]
332-
) -> None:
325+
"""Raised when a set of requested Pro Services are disabled."""
333326

334-
enable_services_str = " ".join(requested_services - available_services)
335-
disable_services_str = " ".join(available_services - requested_services)
327+
def __init__(self, requested_services: set[str]) -> None:
328+
requested_services_str = ", ".join(requested_services)
336329

337-
message = "Incorrect Ubuntu Pro Services were enabled."
330+
message = "Some of the requested Ubuntu Pro Services are disabled."
338331
resolution = (
339-
"Please enable or disable the following services.\n"
340-
f"Enable: {enable_services_str}\n"
341-
f"Disable: {disable_services_str}\n"
332+
"Please enable the following services.\n"
333+
f"Enable: {requested_services_str}\n"
342334
'See "pro" command for details.'
343335
)
344336

craft_application/grammar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def process_part(
5252
*, part_yaml_data: dict[str, Any], processor: GrammarProcessor
5353
) -> dict[str, Any]:
5454
"""Process grammar for a given part."""
55-
for key in part_yaml_data:
55+
for key, _ in part_yaml_data.items():
5656
unprocessed_grammar = part_yaml_data[key]
5757

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

123-
for part_name in parts_yaml_data:
123+
for part_name, _ in parts_yaml_data.items():
124124
parts_yaml_data[part_name] = process_part(
125125
part_yaml_data=parts_yaml_data[part_name], processor=processor
126126
)

craft_application/util/pro_services.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class ProServices(set[str]):
8787
pro_executable: Path | None = next(
8888
(path for path in PRO_CLIENT_PATHS if path.exists()), None
8989
)
90-
# locations to check for pro client
9190

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

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

181180
# remove any services that aren't relevant to build services
182-
service_names = service_names.intersection(cls.supported_services)
181+
return service_names.intersection(cls.supported_services)
182+
183+
@classmethod
184+
def get_pro_services(cls) -> ProServices:
185+
"""Return a class of enabled Ubuntu Pro services in the environment.
183186
184-
return cls(service_names)
187+
The returned set only includes services relevant to lifecycle commands.
188+
"""
189+
return cls(cls._get_pro_services())
185190

186191
def validate(
187192
self,
@@ -205,21 +210,22 @@ def validate(
205210
raise UbuntuProDetachedError
206211

207212
if (
208-
ValidatorOptions._DETACHED in options
213+
ValidatorOptions._DETACHED in options # type: ignore [reportPrivateUsage]
209214
and not self
210215
and not self.managed_mode
211-
): # type: ignore [reportPrivateUsage]
216+
):
212217
# Pro rock is not requested but the host is attached
213218
raise UbuntuProAttachedError
214219

215220
# second, check that the set of enabled pro services in the environment matches
216221
# the services specified in this set
222+
available_services = self._get_pro_services()
217223
if (
218224
ValidatorOptions.ENABLEMENT in options
219-
and ((available_services := self.get_pro_services()) != self)
220-
and not self.managed_mode
225+
and str(self) != self.empty_placeholder
226+
and not self.issubset(available_services)
221227
):
222-
raise InvalidUbuntuProStatusError(self, available_services)
228+
raise InvalidUbuntuProStatusError(self)
223229

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

0 commit comments

Comments
 (0)