Skip to content

Commit

Permalink
Add option --user-requested for "pip freeze" and "pip list"
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkJoy committed Nov 4, 2023
1 parent 05a0e28 commit 6f74d9a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/pip/_internal/commands/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ def add_options(self) -> None:
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)
Expand All @@ -110,6 +119,7 @@ def run(self, options: Values, args: List[str]) -> int:
skip=skip,
exclude_editable=options.exclude_editable,
exclude_dependencies=options.exclude_dependencies,
user_requested=options.user_requested,
):
sys.stdout.write(line + "\n")
return SUCCESS
10 changes: 10 additions & 0 deletions src/pip/_internal/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ def add_options(self) -> None:
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)

Expand Down Expand Up @@ -183,6 +192,7 @@ def run(self, options: Values, args: List[str]) -> int:
editables_only=options.editable,
include_editables=options.include_editable,
exclude_dependencies=options.exclude_dependencies,
user_requested=options.user_requested,
skip=skip,
)
]
Expand Down
6 changes: 6 additions & 0 deletions src/pip/_internal/metadata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ def iter_installed_distributions(
editables_only: bool = False,
user_only: bool = False,
exclude_dependencies: bool = False,
user_requested: bool = False,
) -> Iterator[BaseDistribution]:
"""Return a list of installed distributions.
Expand All @@ -668,6 +669,9 @@ def iter_installed_distributions(
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())
Expand All @@ -680,6 +684,8 @@ def iter_installed_distributions(
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:
Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/operations/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand All @@ -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)
Expand Down

0 comments on commit 6f74d9a

Please sign in to comment.