Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functionality created for the info command. #186

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 115 additions & 17 deletions dem/cli/command/info_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,147 @@
from dem.cli.console import stdout, stderr
from dem.core.platform import Platform
from rich.table import Table
import typer

image_status_messages = {
ToolImage.NOT_AVAILABLE: "[red]Error: Required image is not available![/]",
ToolImage.LOCAL_ONLY: "Image is available locally.",
ToolImage.REGISTRY_ONLY: "Image is available in the registry.",
ToolImage.LOCAL_AND_REGISTRY: "Image is available locally and in the registry.",
ToolImage.NOT_AVAILABLE: "[red]Error: not available![/]",
ToolImage.LOCAL_ONLY: "Local",
ToolImage.REGISTRY_ONLY: "Registry",
ToolImage.LOCAL_AND_REGISTRY: "Local and Registry",
}

def print_info(dev_env: DevEnv) -> None:
""" Print information about the given Development Environment.
def print_local_dev_env_info(dev_env: DevEnv) -> None:
""" Print information about the given local Development Environment.

Args:
dev_env -- the Development Environment to print information about
"""
tool_info_table = Table()
tool_info_table.add_column("Image")
tool_info_table.add_column("Status")
tool_info_table.add_column("Availability")

for tool_image in dev_env.tool_images:
for tool_image in sorted(dev_env.tool_images, key=lambda x: x.name):
tool_info_table.add_row(tool_image.name,
image_status_messages[tool_image.availability])
if dev_env.is_installed:
installation_status = "[green]Installed[/]"
else:
installation_status = "Not installed"
stdout.print(f"\n[bold]Development Environment: {dev_env.name}[/]\n")
stdout.print(f"Installation state: {installation_status}\n")
stdout.print(tool_info_table)

def execute(platform: Platform, arg_dev_env_name: str) -> None:
""" Print information about the given Development Environment.
if dev_env.is_installed and dev_env.get_tool_image_status() == DevEnv.Status.REINSTALL_NEEDED:
stderr.print("\n[red]Error: Incomplete local install! The Dev Env must be reinstalled![/]")

if dev_env.get_tool_image_status() == DevEnv.Status.UNAVAILABLE_IMAGE:
stderr.print("\n[red]Error: Required image could not be found either locally or in the registry![/]")

def local_info(platform: Platform, dev_env_name: str) -> None:
""" Gather and print information about the given local Development Environment.

Args:
platform -- the platform
arg_dev_env_name -- the name of the Development Environment to print information about
dev_env_name -- the name of the Development Environment to print information about

Raises:
typer.Abort -- if the Development Environment is unknown
"""
platform.assign_tool_image_instances_to_all_dev_envs()

dev_env = platform.get_dev_env_by_name(arg_dev_env_name)
dev_env = platform.get_dev_env_by_name(dev_env_name)

if dev_env is None:
for catalog in platform.dev_env_catalogs.catalogs:
stderr.print(f"[red]Error: Unknown Development Environment: {dev_env_name}[/]\n")
raise typer.Abort()

print_local_dev_env_info(dev_env)

def print_cat_dev_env_info(dev_env: DevEnv, cat_name: str) -> None:
""" Print information about the given catalog Development Environment.

Args:
dev_env -- the Development Environment to print information about
cat_name -- the name of the catalog the Development Environment belongs to
"""
tool_info_table = Table()
tool_info_table.add_column("Image")
tool_info_table.add_column("Availability")

for tool_image in sorted(dev_env.tool_images, key=lambda x: x.name):
tool_info_table.add_row(tool_image.name,
image_status_messages[tool_image.availability])
stdout.print(f"\n[bold]Development Environment: {dev_env.name}[/]\n")
stdout.print(f"Catalog: {cat_name}\n")
stdout.print(tool_info_table)

if dev_env.get_tool_image_status() == DevEnv.Status.UNAVAILABLE_IMAGE:
stderr.print("\n[red]Error: Required image could not be found in the registry![/]")

def cat_dev_env_info(platform: Platform, dev_env_name: str, selected_cats: list[str]) -> None:
""" Gather and print information about the given catalog Development Environment.

Args:
platform -- the platform
dev_env_name -- the name of the Development Environment to print information about
selected_cats -- the selected catalog names, empty list means all catalogs
"""
for catalog in platform.dev_env_catalogs.catalogs:
if catalog.name in selected_cats or not selected_cats:
catalog.request_dev_envs()
dev_env = catalog.get_dev_env_by_name(arg_dev_env_name)
dev_env = catalog.get_dev_env_by_name(dev_env_name)
if dev_env:
dev_env.assign_tool_image_instances(platform.tool_images)
print_cat_dev_env_info(dev_env, catalog.name)
break
else:
stderr.print(f"[red]Error: Unknown Development Environment: {dev_env_name}[/]\n")

if dev_env is None:
stderr.print(f"[red]Error: Unknown Development Environment: {arg_dev_env_name}[/]\n")
def selected_cats_info(platform: Platform, dev_env_name: str, selected_cats: list[str]) -> None:
""" Print information about the given Development Environment from the selected catalogs.

Args:
platform -- the platform
dev_env_name -- the name of the Development Environment to print information about
selected_cats -- the selected catalog names

Raises:
typer.Abort -- if the selected catalog is unknown
"""
available_cats = set([cat.name for cat in platform.dev_env_catalogs.catalogs])
selected_cats = set(selected_cats)

if not selected_cats.issubset(available_cats):
stderr.print(f"[red]Error: Unknown catalog(s): {', '.join(selected_cats - available_cats)}[/]\n")
raise typer.Abort()

cat_dev_env_info(platform, dev_env_name, selected_cats)

def all_cats_info(platform: Platform, dev_env_name: str) -> None:
""" Print information about the given Development Environment from all catalogs.

Args:
platform -- the platform
dev_env_name -- the name of the Development Environment to print information about
"""
cat_dev_env_info(platform, dev_env_name, [])

def execute(platform: Platform, arg_dev_env_name: str, cat: bool, selected_cats: list[str]) -> None:
""" Print information about the given Development Environment.

Args:
platform -- the platform
arg_dev_env_name -- the name of the Development Environment to print information about
cat -- the flag to print information about the Development Environment from the catalogs
selected_cats -- the selected catalog names

Raises:
typer.Abort -- if the Development Environment is unknown or if the selected catalog is
unknown
"""
if not cat:
local_info(platform, arg_dev_env_name)
elif selected_cats:
selected_cats_info(platform, arg_dev_env_name, selected_cats)
else:
print_info(dev_env)
all_cats_info(platform, arg_dev_env_name)
12 changes: 6 additions & 6 deletions dem/cli/command/list_tools_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def list_local_tools(platform: Platform) -> None:
# tool_image[0] is the name of the tool image and tool_image[1] is the ToolImage instance
stdout.print(f" {tool_image[0]}")

def update_tools_from_selected_regs(platform: Platform, specified_regs: list[str]) -> None:
def update_tools_from_selected_regs(platform: Platform, selected_regs: list[str]) -> None:
""" Update the tools from the selected registries only.

Args:
platform -- the Platform
specified_regs -- the selected registry names
selected_regs -- the selected registry names

Exceptions:
typer.Abort -- if an unknown registry is specified
Expand All @@ -44,14 +44,14 @@ def update_tools_from_selected_regs(platform: Platform, specified_regs: list[str
platform.disable_tool_update = True

available_regs = set([reg["name"] for reg in platform.config_file.registries])
specified_regs = set(specified_regs)
selected_regs = set(selected_regs)

if not specified_regs.issubset(available_regs):
for unkown_reg in specified_regs - available_regs:
if not selected_regs.issubset(available_regs):
for unkown_reg in selected_regs - available_regs:
stderr.print(f"[red]Error: Registry {unkown_reg} is not available![/]")
raise typer.Abort()

platform.tool_images.update(reg_selection=specified_regs)
platform.tool_images.update(reg_selection=selected_regs)

def list_tools_from_regs(platform: Platform, table: Table) -> None:
""" List the available tools from the registries.
Expand Down
15 changes: 11 additions & 4 deletions dem/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,23 @@ def list_tools(reg: Annotated[bool, typer.Option(help="List the available tools
else:
raise InternalError("Error: The platform hasn't been initialized properly!")

@typer_cli.command()
@typer_cli.command(context_settings={"allow_extra_args": True})
def info(dev_env_name: Annotated[str, typer.Argument(help="Name of the Development Environment to get info about.",
autocompletion=autocomplete_dev_env_name)]) -> None:
autocompletion=autocomplete_dev_env_name)],
cat: Annotated[bool, typer.Option(help="Get the Dev Env from the catalogs")] = False,
ctx: Annotated[typer.Context, typer.Option()] = None) -> None:
"""
Get information about the specified Development Environment available locally or in the catalogs.

--cat: DEM will search for the Dev Env in the catalogs and will print the details of the first
match. You can specifiy the catalogs' name to search in after this option. If no catalog is
specified, all the available catalogs will be used. If the Dev Env is not found in the catalogs,
an error message will be printed.

Note: Autocomplete only works with the locally avialable Dev Envs.
"""
if platform:
info_cmd.execute(platform, dev_env_name)
if platform and ctx:
info_cmd.execute(platform, dev_env_name, cat, ctx.args)
else:
raise InternalError("Error: The platform hasn't been initialized properly!")

Expand Down
2 changes: 0 additions & 2 deletions dem/core/dev_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ def assign_tool_image_instances(self, tool_images: ToolImages) -> None:
def get_tool_image_status(self) -> Status:
""" Get the status of the Tool Images.

Can only be used if the Dev Env is insalled.

This method checks the availability of the assigned Tool Images.
If at least one of the Tool Images is unkonwn: NOT_AVAILABLE.
If at least one of the Tool Images is only available in the registry: REINSTALL_NEEDED.
Expand Down
Loading
Loading