Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ required by
* `--outdated (-o)`: Show the latest version but only for packages that are outdated.
* `--all (-a)`: Show all packages (even those not compatible with current system).
* `--top-level (-T)`: Only show explicitly defined packages.
* `--with-groups`: Show dependency group names.
* `--no-truncate`: Do not truncate the output based on the terminal width.
* `--format (-f)`: Specify the output format (`json` or `text`). Default is `text`. `json` cannot be combined with the `--tree` option.

Expand Down
93 changes: 91 additions & 2 deletions src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from poetry.console.commands.env_command import EnvCommand
from poetry.console.commands.group_command import GroupCommand
from poetry.packages.transitive_package_info import group_sort_key
from poetry.utils.constants import POETRY_SYSTEM_PROJECT_NAME


Expand Down Expand Up @@ -74,6 +75,7 @@ class ShowCommand(GroupCommand, EnvCommand):
"Show all packages (even those not compatible with current system).",
),
option("top-level", "T", "Show only top-level dependencies."),
option("with-groups", None, "Show dependency group names."),
option(
"no-truncate",
None,
Expand Down Expand Up @@ -113,6 +115,13 @@ def handle(self) -> int:
)
return 1

if self.option("with-groups") and self.option("tree"):
self.line_error(
"<error>Error: Cannot use --tree and --with-groups at the same"
" time.</error>"
)
return 1

if self.option("why"):
if self.option("tree") and package is None:
self.line_error(
Expand Down Expand Up @@ -289,16 +298,28 @@ def _display_packages_information(
show_all = self.option("all")
show_top_level = self.option("top-level")
show_why = self.option("why")
show_groups = self.option("with-groups")
width = (
sys.maxsize
if self.option("no-truncate")
else shutil.get_terminal_size().columns
)
name_length = version_length = latest_length = required_by_length = 0
groups_length = 0
latest_packages = {}
latest_statuses = {}
installed_repo = InstalledRepository.load(self.env)
requires = root.all_requires
package_groups: dict[NormalizedName, list[NormalizedName]] = {}
default_groups = ""
formatted_groups: dict[NormalizedName, str] = {}
if show_groups:
package_groups = self._package_groups(root)
default_groups = self._format_groups([])
formatted_groups = {
package_name: self._format_groups(groups)
for package_name, groups in package_groups.items()
}

# Computing widths
for locked in locked_packages:
Expand Down Expand Up @@ -349,6 +370,12 @@ def _display_packages_information(
required_by_length,
len(" from " + ",".join(required_by.keys())),
)

if show_groups:
groups_length = max(
groups_length,
len(formatted_groups.get(locked.name, default_groups)),
)
else:
name_length = max(name_length, current_length)
version_length = max(
Expand All @@ -366,6 +393,12 @@ def _display_packages_information(
required_by_length, len(" from " + ",".join(required_by.keys()))
)

if show_groups:
groups_length = max(
groups_length,
len(formatted_groups.get(locked.name, default_groups)),
)

if self.option("format") == OutputFormats.JSON:
packages = []

Expand Down Expand Up @@ -403,6 +436,11 @@ def _display_packages_information(
if required_by:
package["required_by"] = list(required_by.keys())

if show_groups:
package["groups"] = [
str(group) for group in package_groups.get(locked.name, [])
]

package["description"] = locked.description

packages.append(package)
Expand All @@ -412,10 +450,21 @@ def _display_packages_information(
return 0

write_version = name_length + version_length + 3 <= width
write_latest = name_length + version_length + latest_length + 3 <= width
write_groups = (
show_groups and name_length + version_length + groups_length + 3 <= width
)
group_column_length = groups_length if write_groups else 0
write_latest = (
name_length + version_length + group_column_length + latest_length + 3
<= width
)

why_end_column = (
name_length + version_length + latest_length + required_by_length
name_length
+ version_length
+ group_column_length
+ latest_length
+ required_by_length
)
write_why = show_why and (why_end_column + 3) <= width
write_description = (why_end_column + 24) <= width
Expand Down Expand Up @@ -460,6 +509,11 @@ def _display_packages_information(
locked, root=self.poetry.file.path.parent
)
line += f" <b>{version:{version_length}}</b>"

if write_groups:
groups = formatted_groups.get(locked.name, default_groups)
line += f" {groups:{groups_length}}"

if show_latest:
latest = latest_packages[locked.pretty_name]
update_status = latest_statuses[locked.pretty_name]
Expand Down Expand Up @@ -494,6 +548,9 @@ def _display_packages_information(
if show_latest:
remaining -= latest_length

if write_groups:
remaining -= groups_length

if len(locked.description) > remaining:
description = description[: remaining - 3] + "..."

Expand All @@ -503,6 +560,38 @@ def _display_packages_information(

return 0

def _package_groups(
self, root: ProjectPackage
) -> dict[NormalizedName, list[NormalizedName]]:
active_groups = root.dependency_group_names(include_optional=True)
lock_data = self.poetry.locker.lock_data

if (
lock_data.get("metadata", {}).get("lock-version")
and self.poetry.locker.is_locked_groups_and_markers()
):
return {
package.name: sorted(
info.groups.intersection(active_groups), key=group_sort_key
)
for package, info in self.poetry.locker.locked_packages().items()
}

package_groups: dict[NormalizedName, set[NormalizedName]] = {}
for group_name in root.dependency_group_names(include_optional=True):
group = root.dependency_group(group_name)
for dependency in group.dependencies_for_locking:
package_groups.setdefault(dependency.name, set()).add(group_name)

return {
package_name: sorted(groups, key=group_sort_key)
for package_name, groups in package_groups.items()
}

@staticmethod
def _format_groups(groups: list[NormalizedName]) -> str:
return ", ".join(groups) if groups else "-"

def _display_packages_tree_information(
self, locked_repository: Repository, root: ProjectPackage
) -> int:
Expand Down
129 changes: 129 additions & 0 deletions tests/console/commands/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,135 @@ def test_show_basic_with_group_options(
assert tester.io.fetch_output() == expected


@pytest.mark.parametrize(
("options", "expected"),
[
(
"--with-groups",
"""\
cachy 0.1.0 main Cachy package
pytest 3.7.3 test Pytest package
""",
),
(
"--with time --with-groups",
"""\
cachy 0.1.0 main Cachy package
pendulum 2.0.0 time Pendulum package
pytest 3.7.3 test Pytest package
""",
),
],
)
def test_show_with_groups(
options: str,
expected: str,
tester: CommandTester,
poetry: Poetry,
installed: Repository,
) -> None:
_configure_project_with_groups(poetry, installed)

tester.execute(options)

assert tester.io.fetch_output() == expected


def test_show_with_groups_json(
tester: CommandTester, poetry: Poetry, installed: Repository
) -> None:
_configure_project_with_groups(poetry, installed)

tester.execute("--with time --without test --with-groups --format json")

assert json.loads(tester.io.fetch_output()) == [
{
"name": "cachy",
"version": "0.1.0",
"groups": ["main"],
"description": "Cachy package",
"installed_status": "installed",
},
{
"name": "pendulum",
"version": "2.0.0",
"groups": ["time"],
"description": "Pendulum package",
"installed_status": "installed",
},
]


def test_show_with_groups_uses_lock_groups_for_transitive_packages(
tester: CommandTester, poetry: Poetry, installed: Repository
) -> None:
poetry.package.add_dependency(Factory.create_dependency("cachy", "^0.1.0"))

cachy_010 = get_package("cachy", "0.1.0")
cachy_010.description = "Cachy package"
cachy_010.add_dependency(Factory.create_dependency("msgpack", "^1.0.0"))

msgpack_100 = get_package("msgpack", "1.0.0")
msgpack_100.description = "Msgpack package"

installed.add_package(cachy_010)
installed.add_package(msgpack_100)

assert isinstance(poetry.locker, DummyLocker)
poetry.locker.mock_lock_data(
{
"package": [
{
"name": "cachy",
"version": "0.1.0",
"description": "Cachy package",
"optional": False,
"python-versions": "*",
"groups": ["main"],
"files": [],
"dependencies": {"msgpack": "^1.0.0"},
},
{
"name": "msgpack",
"version": "1.0.0",
"description": "Msgpack package",
"optional": False,
"python-versions": "*",
"groups": ["main"],
"files": [],
},
],
"metadata": {
"lock-version": "2.1",
"python-versions": "*",
"content-hash": "123456789",
},
}
)

tester.execute("--with-groups")

assert (
tester.io.fetch_output()
== """\
cachy 0.1.0 main Cachy package
msgpack 1.0.0 main Msgpack package
"""
)


def test_show_with_groups_cannot_be_combined_with_tree(
tester: CommandTester,
) -> None:
tester.execute("--tree --with-groups")

assert tester.status_code == 1
assert (
tester.io.fetch_error()
== "Error: Cannot use --tree and --with-groups at the same time.\n"
)


@output_format_parametrize
def test_show_basic_with_installed_packages_single(
output_format: str,
Expand Down
Loading