Skip to content

Commit b2463d5

Browse files
committed
Add new "concise" option to "show requirements"
1 parent 18ec0e7 commit b2463d5

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

doc-source/usage/show.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.. click:: repo_helper.cli.commands.show:show
22
:prog: repo-helper show
33
:nested: full
4+
5+
.. versionchanged:: 2020.12.3 Added the ``-c / --concise`` option.

repo_helper/cli/commands/show.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@
2424
#
2525

2626
# stdlib
27+
import re
2728
from datetime import datetime
2829
from functools import partial
29-
from typing import List, Optional, Union
30+
from typing import Iterable, List, Optional, Union
3031

3132
# 3rd party
3233
import click
3334
from consolekit import CONTEXT_SETTINGS
3435
from consolekit.options import colour_option, no_pager_option
3536

3637
# this package
38+
from packaging.requirements import Requirement
39+
from shippinglabel.requirements import combine_requirements, ComparableRequirement
40+
3741
from repo_helper.cli import cli_group
3842

3943
__all__ = ["show", "show_command", "version", "log", "changelog"]
@@ -205,8 +209,15 @@ def changelog(
205209
default=-1,
206210
help="The maximum depth to display. -1 means infinite depth.",
207211
)
212+
@click.option(
213+
"-c",
214+
"--concise",
215+
is_flag=True,
216+
default=False,
217+
help="Show a consolidated list of all dependencies.",
218+
)
208219
@show_command()
209-
def requirements(no_pager: bool = False, depth: int = -1):
220+
def requirements(no_pager: bool = False, depth: int = -1, concise: bool = False):
210221
"""
211222
Lists the requirements of this library, and their dependencies.
212223
"""
@@ -239,11 +250,31 @@ def requirements(no_pager: bool = False, depth: int = -1):
239250

240251
importlib_metadata.DistributionFinder.Context.path = search_path
241252

242-
for requirement in raw_requirements:
243-
tree.append(str(requirement))
244-
deps = list(list_requirements(str(requirement), depth=depth - 1))
245-
if deps:
246-
tree.append(deps)
253+
if concise:
254+
concise_requirements = []
255+
256+
def flatten(iterable: Iterable[Union[Requirement, Iterable]]):
257+
for item in iterable:
258+
if isinstance(item, str):
259+
yield item
260+
else:
261+
yield from flatten(item)
262+
263+
for requirement in raw_requirements:
264+
concise_requirements.append(requirement)
265+
# TODO: remove "extra == " marker
266+
for req in flatten(list_requirements(str(requirement), depth=depth - 1)):
267+
concise_requirements.append(ComparableRequirement(re.sub('; extra == ".*"', "", req)))
268+
269+
concise_requirements = sorted(set(combine_requirements(concise_requirements)))
270+
tree = list(map(str, concise_requirements))
271+
272+
else:
273+
for requirement in raw_requirements:
274+
tree.append(str(requirement))
275+
deps = list(list_requirements(str(requirement), depth=depth - 1))
276+
if deps:
277+
tree.append(deps)
247278

248279
buf.extend(make_tree(tree))
249280

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ packaging>=20.4
1515
pre-commit>=2.7.1
1616
requests>=2.25.0
1717
ruamel-yaml>=0.16.12
18-
shippinglabel>=0.5.0
18+
shippinglabel>=0.5.2
1919
southwark>=0.6.0
2020
tomlkit>=0.7.0
2121
typing-extensions>=3.7.4.3

0 commit comments

Comments
 (0)