Blame SOURCES/emit-a-warning-when-running-with-root-privileges.patch

d470f4
From aab24967a03bda3b0999d80562a6064c27d1e0e0 Mon Sep 17 00:00:00 2001
d470f4
From: Tomas Orsava <torsava@redhat.com>
d470f4
Date: Tue, 12 Nov 2019 17:15:08 +0100
d470f4
Subject: [PATCH] Downstream only patch
d470f4
d470f4
Emit a warning to the user if pip install is run with root privileges
d470f4
Issue upstream: https://github.com/pypa/pip/issues/4288
d470f4
---
d470f4
 src/pip/_internal/commands/install.py | 19 +++++++++++++++++++
d470f4
 1 file changed, 19 insertions(+)
d470f4
d470f4
diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py
d470f4
index 5842d18..a6104b4 100644
d470f4
--- a/src/pip/_internal/commands/install.py
d470f4
+++ b/src/pip/_internal/commands/install.py
d470f4
@@ -12,6 +12,8 @@ import logging
d470f4
 import operator
d470f4
 import os
d470f4
 import shutil
d470f4
+import sys
d470f4
+from os import path
d470f4
 from optparse import SUPPRESS_HELP
d470f4
 
d470f4
 from pip._vendor import pkg_resources
d470f4
@@ -281,6 +283,23 @@ class InstallCommand(RequirementCommand):
d470f4
     def run(self, options, args):
d470f4
         # type: (Values, List[Any]) -> int
d470f4
         cmdoptions.check_install_build_global(options)
d470f4
+
d470f4
+        def is_venv():
d470f4
+            return (hasattr(sys, 'real_prefix') or
d470f4
+                    (hasattr(sys, 'base_prefix') and
d470f4
+                     sys.base_prefix != sys.prefix))
d470f4
+
d470f4
+        # Check whether we have root privileges and aren't in venv/virtualenv
d470f4
+        if os.getuid() == 0 and not is_venv():
d470f4
+            command = path.basename(sys.argv[0])
d470f4
+            if command == "__main__.py":
d470f4
+                command = path.basename(sys.executable) + " -m pip"
d470f4
+            logger.warning(
d470f4
+                "Running pip install with root privileges is "
d470f4
+                "generally not a good idea. Try `%s install --user` instead."
d470f4
+                % command
d470f4
+            )
d470f4
+
d470f4
         upgrade_strategy = "to-satisfy-only"
d470f4
         if options.upgrade:
d470f4
             upgrade_strategy = options.upgrade_strategy
d470f4
-- 
d470f4
2.20.1
d470f4