Skip to content

Commit 0ac0f84

Browse files
authored
Merge pull request #10990 from potiuk/fix-root-warning
2 parents d1f3c73 + 50b7d94 commit 0ac0f84

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

news/10990.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add option to install and uninstall commands to opt-out from running-as-root warning.

src/pip/_internal/cli/cmdoptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,15 @@ def _handle_no_use_pep517(
858858
"of pip is available for download. Implied with --no-index.",
859859
)
860860

861+
warn_about_root_user: Callable[..., Option] = partial(
862+
Option,
863+
"--no-warn-when-using-as-a-root-user",
864+
dest="warn_about_root_user",
865+
default=True,
866+
action="store_false",
867+
help="Do not warn when used as a root user",
868+
)
869+
861870

862871
def _handle_merge_hash(
863872
option: Option, opt_str: str, value: str, parser: OptionParser

src/pip/_internal/commands/install.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ def add_options(self) -> None:
222222
default=True,
223223
help="Do not warn about broken dependencies",
224224
)
225-
226225
self.cmd_opts.add_option(cmdoptions.no_binary())
227226
self.cmd_opts.add_option(cmdoptions.only_binary())
228227
self.cmd_opts.add_option(cmdoptions.prefer_binary())
229228
self.cmd_opts.add_option(cmdoptions.require_hashes())
230229
self.cmd_opts.add_option(cmdoptions.progress_bar())
230+
self.cmd_opts.add_option(cmdoptions.warn_about_root_user())
231231

232232
index_opts = cmdoptions.make_option_group(
233233
cmdoptions.index_group,
@@ -464,8 +464,8 @@ def run(self, options: Values, args: List[str]) -> int:
464464
self._handle_target_dir(
465465
options.target_dir, target_temp_dir, options.upgrade
466466
)
467-
468-
warn_if_run_as_root()
467+
if options.warn_about_root_user:
468+
warn_if_run_as_root()
469469
return SUCCESS
470470

471471
def _handle_target_dir(

src/pip/_internal/commands/uninstall.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from pip._vendor.packaging.utils import canonicalize_name
66

7+
from pip._internal.cli import cmdoptions
78
from pip._internal.cli.base_command import Command
89
from pip._internal.cli.req_command import SessionCommandMixin, warn_if_run_as_root
910
from pip._internal.cli.status_codes import SUCCESS
@@ -53,7 +54,7 @@ def add_options(self) -> None:
5354
action="store_true",
5455
help="Don't ask for confirmation of uninstall deletions.",
5556
)
56-
57+
self.cmd_opts.add_option(cmdoptions.warn_about_root_user())
5758
self.parser.insert_option_group(0, self.cmd_opts)
5859

5960
def run(self, options: Values, args: List[str]) -> int:
@@ -100,6 +101,6 @@ def run(self, options: Values, args: List[str]) -> int:
100101
)
101102
if uninstall_pathset:
102103
uninstall_pathset.commit()
103-
104-
warn_if_run_as_root()
104+
if options.warn_about_root_user:
105+
warn_if_run_as_root()
105106
return SUCCESS

0 commit comments

Comments
 (0)