Skip to content

Commit

Permalink
Allow to disable warning for root user package management
Browse files Browse the repository at this point in the history
This PR implements the flag that allows to disable the root
warning when using `pip` to install packages. While there are
differing opinions on this, it seems that the final decision is
to lean forward to implement a long and not very easily
discoverable flag to accommodate the minority of users who know
what they are doing and using root installation to - for example
build optimized Dockerfiles.

Even approval, and eventually an adoption of PEP 668 to make the
problem largely go away is still a long time to go, so as
discussed in pypa#10556 (comment)
this PR adds a long and non-user friendly flag to handle the
situation.

Fixes: pypa#10556
  • Loading branch information
potiuk committed Mar 26, 2022
1 parent 7d61883 commit 530fa2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions news/10990.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The install and uinstall commands allow to opt-out from root warning
14 changes: 12 additions & 2 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ def add_options(self) -> None:
default=True,
help="Do not warn about broken dependencies",
)
self.cmd_opts.add_option(
"--no-warn-when-using-as-a-root-user-to-manage-os-packages",
dest="no_warn_about_root_user",
action="store_true",
help="Disable warning when there are legitimate uses "
"for managing os packages as root user"
"(for example in some cases of container builds)",
)

self.cmd_opts.add_option(cmdoptions.no_binary())
self.cmd_opts.add_option(cmdoptions.only_binary())
Expand Down Expand Up @@ -464,8 +472,10 @@ 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.no_warn_about_root_user:
pass
else:
warn_if_run_as_root()
return SUCCESS

def _handle_target_dir(
Expand Down
15 changes: 12 additions & 3 deletions src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ def add_options(self) -> None:
action="store_true",
help="Don't ask for confirmation of uninstall deletions.",
)

self.cmd_opts.add_option(
"--no-warn-when-using-as-a-root-user-to-manage-os-packages",
dest="no_warn_about_root_user",
action="store_true",
help="Disable warning when there are legitimate uses "
"for managing os packages as root user"
"(for example in some cases of container builds)",
)
self.parser.insert_option_group(0, self.cmd_opts)

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

warn_if_run_as_root()
if options.no_warn_about_root_user:
pass
else:
warn_if_run_as_root()
return SUCCESS

0 comments on commit 530fa2a

Please sign in to comment.