Skip to content

Allow to disable warning for root user package management #10990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/10990.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option to install and uninstall commands to opt-out from running-as-root warning.
9 changes: 9 additions & 0 deletions src/pip/_internal/cli/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,15 @@ def _handle_no_use_pep517(
"of pip is available for download. Implied with --no-index.",
)

warn_about_root_user: Callable[..., Option] = partial(
Option,
"--no-warn-when-using-as-a-root-user",
dest="warn_about_root_user",
default=True,
action="store_false",
help="Do not warn when used as a root user",
)


def _handle_merge_hash(
option: Option, opt_str: str, value: str, parser: OptionParser
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ def add_options(self) -> None:
default=True,
help="Do not warn about broken dependencies",
)

self.cmd_opts.add_option(cmdoptions.no_binary())
self.cmd_opts.add_option(cmdoptions.only_binary())
self.cmd_opts.add_option(cmdoptions.prefer_binary())
self.cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.progress_bar())
self.cmd_opts.add_option(cmdoptions.warn_about_root_user())

index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
Expand Down Expand Up @@ -464,8 +464,8 @@ def run(self, options: Values, args: List[str]) -> int:
self._handle_target_dir(
options.target_dir, target_temp_dir, options.upgrade
)

warn_if_run_as_root()
if options.warn_about_root_user:
warn_if_run_as_root()
return SUCCESS

def _handle_target_dir(
Expand Down
7 changes: 4 additions & 3 deletions src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pip._vendor.packaging.utils import canonicalize_name

from pip._internal.cli import cmdoptions
from pip._internal.cli.base_command import Command
from pip._internal.cli.req_command import SessionCommandMixin, warn_if_run_as_root
from pip._internal.cli.status_codes import SUCCESS
Expand Down Expand Up @@ -53,7 +54,7 @@ def add_options(self) -> None:
action="store_true",
help="Don't ask for confirmation of uninstall deletions.",
)

self.cmd_opts.add_option(cmdoptions.warn_about_root_user())
self.parser.insert_option_group(0, self.cmd_opts)

def run(self, options: Values, args: List[str]) -> int:
Expand Down Expand Up @@ -100,6 +101,6 @@ def run(self, options: Values, args: List[str]) -> int:
)
if uninstall_pathset:
uninstall_pathset.commit()

warn_if_run_as_root()
if options.warn_about_root_user:
warn_if_run_as_root()
return SUCCESS