From 470958fc23b5e1d1fae44f1946de067751192bba Mon Sep 17 00:00:00 2001 From: Daniel Klevebring Date: Sat, 26 Oct 2024 22:34:49 +0200 Subject: [PATCH] filter deps if asked --- piptools/scripts/compile.py | 17 +++++++++++++++++ piptools/scripts/options.py | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 96aa9fa9..d47c1eed 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -44,6 +44,18 @@ METADATA_FILENAMES = frozenset({"setup.py", "setup.cfg", "pyproject.toml"}) +def _filter_dependencies(dependencies, only_from_packages): + """ + Filter dependencies to include only those required by the specified packages. + """ + filtered_dependencies = [ + dep + for dep in dependencies + if dep.comes_from and dep.comes_from in only_from_packages + ] + return set(filtered_dependencies) + + def _determine_linesep( strategy: str = "preserve", filenames: tuple[str, ...] = () ) -> str: @@ -101,6 +113,7 @@ def _determine_linesep( @options.upgrade @options.upgrade_package @options.output_file +@options.only_from @options.newline @options.allow_unsafe @options.strip_extras @@ -146,6 +159,7 @@ def cli( upgrade: bool, upgrade_packages: tuple[str, ...], output_file: LazyFile | IO[Any] | None, + only_from: list[str] | None, newline: str, allow_unsafe: bool, strip_extras: bool | None, @@ -505,6 +519,9 @@ def cli( "or use --no-strip-extras to retain the existing behavior." ) + if only_from: + results = _filter_dependencies(results, only_from_packages=only_from) + ## # Output ## diff --git a/piptools/scripts/options.py b/piptools/scripts/options.py index 98fbbf7a..b13d4bde 100644 --- a/piptools/scripts/options.py +++ b/piptools/scripts/options.py @@ -191,6 +191,12 @@ def _get_default_option(option_name: str) -> Any: ), ) +only_from = click.option( + "--only-from", + multiple=True, + help="Only include dependencies from given packages; may be used more than once", +) + newline = click.option( "--newline", type=click.Choice(("LF", "CRLF", "native", "preserve"), case_sensitive=False),