Skip to content

Commit d46fef8

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

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def is_pro_attached(cls) -> bool:
168168
return response["data"]["attributes"]["is_attached"] # type: ignore [no-any-return]
169169

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

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

186192
def validate(
187193
self,
@@ -205,21 +211,22 @@ def validate(
205211
raise UbuntuProDetachedError
206212

207213
if (
208-
ValidatorOptions._DETACHED in options
214+
ValidatorOptions._DETACHED in options # type: ignore [reportPrivateUsage]
209215
and not self
210216
and not self.managed_mode
211-
): # type: ignore [reportPrivateUsage]
217+
):
212218
# Pro rock is not requested but the host is attached
213219
raise UbuntuProAttachedError
214220

215221
# second, check that the set of enabled pro services in the environment matches
216222
# the services specified in this set
223+
available_services = self._get_pro_services()
217224
if (
218225
ValidatorOptions.ENABLEMENT in options
219-
and ((available_services := self.get_pro_services()) != self)
220-
and not self.managed_mode
226+
and str(self) != self.empty_placeholder
227+
and str(self) not in available_services
221228
):
222-
raise InvalidUbuntuProStatusError(self, available_services)
229+
raise InvalidUbuntuProStatusError(self)
223230

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

0 commit comments

Comments
 (0)