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

dfbae6
From 74bb5d26e232493de43adfa1f4b42b66fd701294 Mon Sep 17 00:00:00 2001
dfbae6
From: Tomas Hrnciar <thrnciar@redhat.com>
dfbae6
Date: Sun, 26 Apr 2020 13:52:24 +0200
dfbae6
Subject: [PATCH] Downstream only patch
dfbae6
dfbae6
Emit a warning to the user if pip install is run with root privileges
dfbae6
Issue upstream: https://github.com/pypa/pip/issues/4288
dfbae6
---
dfbae6
 src/pip/_internal/commands/install.py | 19 +++++++++++++++++++
dfbae6
 1 file changed, 19 insertions(+)
dfbae6
dfbae6
diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py
dfbae6
index 70bda2e2..1e750ae1 100644
dfbae6
--- a/src/pip/_internal/commands/install.py
dfbae6
+++ b/src/pip/_internal/commands/install.py
dfbae6
@@ -13,6 +13,8 @@ import operator
dfbae6
 import os
dfbae6
 import shutil
dfbae6
 import site
dfbae6
+import sys
dfbae6
+from os import path
dfbae6
 from optparse import SUPPRESS_HELP
dfbae6
 
dfbae6
 from pip._vendor import pkg_resources
dfbae6
@@ -241,6 +243,23 @@ class InstallCommand(RequirementCommand):
dfbae6
             raise CommandError("Can not combine '--user' and '--target'")
dfbae6
 
dfbae6
         cmdoptions.check_install_build_global(options)
dfbae6
+
dfbae6
+        def is_venv():
dfbae6
+            return (hasattr(sys, 'real_prefix') or
dfbae6
+                    (hasattr(sys, 'base_prefix') and
dfbae6
+                     sys.base_prefix != sys.prefix))
dfbae6
+
dfbae6
+        # Check whether we have root privileges and aren't in venv/virtualenv
dfbae6
+        if os.getuid() == 0 and not is_venv() and not options.root_path:
dfbae6
+            command = path.basename(sys.argv[0])
dfbae6
+            if command == "__main__.py":
dfbae6
+                command = path.basename(sys.executable) + " -m pip"
dfbae6
+            logger.warning(
dfbae6
+                "Running pip install with root privileges is "
dfbae6
+                "generally not a good idea. Try `%s install --user` instead."
dfbae6
+                % command
dfbae6
+            )
dfbae6
+
dfbae6
         upgrade_strategy = "to-satisfy-only"
dfbae6
         if options.upgrade:
dfbae6
             upgrade_strategy = options.upgrade_strategy
dfbae6
-- 
dfbae6
2.23.0
dfbae6