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

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