Add option --exclude-dependencies for "pip freeze" and "pip list"

This commit is contained in:
MarkJoy 2023-11-04 21:29:03 +07:00
parent 2a0acb595c
commit 9d352916ea
4 changed files with 30 additions and 1 deletions

View File

@ -81,6 +81,12 @@ class FreezeCommand(Command):
action="store_true",
help="Exclude editable package from output.",
)
self.cmd_opts.add_option(
"--exclude-dependencies",
dest="exclude_dependencies",
action="store_true",
help="Exclude dependency packages from output.",
)
self.cmd_opts.add_option(cmdoptions.list_exclude())
self.parser.insert_option_group(0, self.cmd_opts)
@ -103,6 +109,7 @@ class FreezeCommand(Command):
isolated=options.isolated_mode,
skip=skip,
exclude_editable=options.exclude_editable,
exclude_dependencies=options.exclude_dependencies,
):
sys.stdout.write(line + "\n")
return SUCCESS

View File

@ -129,6 +129,12 @@ class ListCommand(IndexGroupCommand):
help="Include editable package from output.",
default=True,
)
self.cmd_opts.add_option(
"--exclude-dependencies",
dest="exclude_dependencies",
action="store_true",
help="Exclude dependency packages from output.",
)
self.cmd_opts.add_option(cmdoptions.list_exclude())
index_opts = cmdoptions.make_option_group(cmdoptions.index_group, self.parser)
@ -176,6 +182,7 @@ class ListCommand(IndexGroupCommand):
user_only=options.user,
editables_only=options.editable,
include_editables=options.include_editable,
exclude_dependencies=options.exclude_dependencies,
skip=skip,
)
]

View File

@ -649,6 +649,7 @@ class BaseEnvironment:
include_editables: bool = True,
editables_only: bool = False,
user_only: bool = False,
exclude_dependencies: bool = False,
) -> Iterator[BaseDistribution]:
"""Return a list of installed distributions.
@ -665,8 +666,20 @@ class BaseEnvironment:
:param editables_only: If True, only report editables.
:param user_only: If True, only report installations in the user
site directory.
:param exclude_dependencies: If True, dont't report distributions
that are dependencies of other installed distributions.
"""
it = self.iter_all_distributions()
if exclude_dependencies:
dists = list(self.iter_all_distributions())
it = iter(dists)
dep_keys = {
canonicalize_name(dep.name)
for dist in dists
for dep in (dist.iter_dependencies() or ())
}
it = (d for d in it if d.canonical_name not in dep_keys)
else:
it = self.iter_all_distributions()
if local_only:
it = (d for d in it if d.local)
if not include_editables:

View File

@ -30,6 +30,7 @@ def freeze(
paths: Optional[List[str]] = None,
isolated: bool = False,
exclude_editable: bool = False,
exclude_dependencies: bool = False,
skip: Container[str] = (),
) -> Generator[str, None, None]:
installations: Dict[str, FrozenRequirement] = {}
@ -38,6 +39,7 @@ def freeze(
local_only=local_only,
skip=(),
user_only=user_only,
exclude_dependencies=exclude_dependencies,
)
for dist in dists:
req = FrozenRequirement.from_dist(dist)