Add option --user-requested for "pip freeze" and "pip list"

This commit is contained in:
MarkJoy 2023-11-04 22:21:36 +07:00
parent 9d352916ea
commit 1bfb36a5fe
4 changed files with 28 additions and 0 deletions

View File

@ -87,6 +87,15 @@ class FreezeCommand(Command):
action="store_true",
help="Exclude dependency packages from output.",
)
self.cmd_opts.add_option(
"--user-requested",
dest="user_requested",
action="store_true",
help=(
"List packages that were explicitly requested by user, either directly "
"via a command line argument or indirectly via a requirements file."
),
)
self.cmd_opts.add_option(cmdoptions.list_exclude())
self.parser.insert_option_group(0, self.cmd_opts)
@ -110,6 +119,7 @@ class FreezeCommand(Command):
skip=skip,
exclude_editable=options.exclude_editable,
exclude_dependencies=options.exclude_dependencies,
user_requested=options.user_requested,
):
sys.stdout.write(line + "\n")
return SUCCESS

View File

@ -135,6 +135,15 @@ class ListCommand(IndexGroupCommand):
action="store_true",
help="Exclude dependency packages from output.",
)
self.cmd_opts.add_option(
"--user-requested",
dest="user_requested",
action="store_true",
help=(
"List packages that were explicitly requested by user, either directly "
"via a command line argument or indirectly via a requirements file."
),
)
self.cmd_opts.add_option(cmdoptions.list_exclude())
index_opts = cmdoptions.make_option_group(cmdoptions.index_group, self.parser)
@ -183,6 +192,7 @@ class ListCommand(IndexGroupCommand):
editables_only=options.editable,
include_editables=options.include_editable,
exclude_dependencies=options.exclude_dependencies,
user_requested=options.user_requested,
skip=skip,
)
]

View File

@ -650,6 +650,7 @@ class BaseEnvironment:
editables_only: bool = False,
user_only: bool = False,
exclude_dependencies: bool = False,
user_requested: bool = False,
) -> Iterator[BaseDistribution]:
"""Return a list of installed distributions.
@ -668,6 +669,9 @@ class BaseEnvironment:
site directory.
:param exclude_dependencies: If True, dont't report distributions
that are dependencies of other installed distributions.
:param user_requested: If True, report only distributions that were
explicitly requested by user, either directly via a command line argument
or indirectly via a requirements file.
"""
if exclude_dependencies:
dists = list(self.iter_all_distributions())
@ -680,6 +684,8 @@ class BaseEnvironment:
it = (d for d in it if d.canonical_name not in dep_keys)
else:
it = self.iter_all_distributions()
if user_requested:
it = (d for d in it if d.requested)
if local_only:
it = (d for d in it if d.local)
if not include_editables:

View File

@ -31,6 +31,7 @@ def freeze(
isolated: bool = False,
exclude_editable: bool = False,
exclude_dependencies: bool = False,
user_requested: bool = False,
skip: Container[str] = (),
) -> Generator[str, None, None]:
installations: Dict[str, FrozenRequirement] = {}
@ -40,6 +41,7 @@ def freeze(
skip=(),
user_only=user_only,
exclude_dependencies=exclude_dependencies,
user_requested=user_requested,
)
for dist in dists:
req = FrozenRequirement.from_dist(dist)