|
24 | 24 | # |
25 | 25 |
|
26 | 26 | # stdlib |
| 27 | +import re |
27 | 28 | from datetime import datetime |
28 | 29 | from functools import partial |
29 | | -from typing import List, Optional, Union |
| 30 | +from typing import Iterable, List, Optional, Union |
30 | 31 |
|
31 | 32 | # 3rd party |
32 | 33 | import click |
33 | 34 | from consolekit import CONTEXT_SETTINGS |
34 | 35 | from consolekit.options import colour_option, no_pager_option |
35 | 36 |
|
36 | 37 | # this package |
| 38 | +from packaging.requirements import Requirement |
| 39 | +from shippinglabel.requirements import combine_requirements, ComparableRequirement |
| 40 | + |
37 | 41 | from repo_helper.cli import cli_group |
38 | 42 |
|
39 | 43 | __all__ = ["show", "show_command", "version", "log", "changelog"] |
@@ -205,8 +209,15 @@ def changelog( |
205 | 209 | default=-1, |
206 | 210 | help="The maximum depth to display. -1 means infinite depth.", |
207 | 211 | ) |
| 212 | +@click.option( |
| 213 | + "-c", |
| 214 | + "--concise", |
| 215 | + is_flag=True, |
| 216 | + default=False, |
| 217 | + help="Show a consolidated list of all dependencies.", |
| 218 | + ) |
208 | 219 | @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): |
210 | 221 | """ |
211 | 222 | Lists the requirements of this library, and their dependencies. |
212 | 223 | """ |
@@ -239,11 +250,31 @@ def requirements(no_pager: bool = False, depth: int = -1): |
239 | 250 |
|
240 | 251 | importlib_metadata.DistributionFinder.Context.path = search_path |
241 | 252 |
|
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) |
247 | 278 |
|
248 | 279 | buf.extend(make_tree(tree)) |
249 | 280 |
|
|
0 commit comments