orion / rpms / python3x-pip

Forked from rpms/python3x-pip 2 years ago
Clone

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

bcedfd
diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py
bcedfd
index 1279d4a..aeb9d26 100644
bcedfd
--- a/src/pip/_internal/commands/install.py
bcedfd
+++ b/src/pip/_internal/commands/install.py
bcedfd
@@ -5,6 +5,8 @@ import logging
bcedfd
 import operator
bcedfd
 import os
bcedfd
 import shutil
bcedfd
+import sys
bcedfd
+from os import path
bcedfd
 from optparse import SUPPRESS_HELP
bcedfd
 
bcedfd
 from pip._vendor import pkg_resources
bcedfd
@@ -217,6 +219,23 @@ class InstallCommand(RequirementCommand):
bcedfd
 
bcedfd
     def run(self, options, args):
bcedfd
         cmdoptions.check_install_build_global(options)
bcedfd
+
bcedfd
+        def is_venv():
bcedfd
+            return (hasattr(sys, 'real_prefix') or
bcedfd
+                    (hasattr(sys, 'base_prefix') and
bcedfd
+                     sys.base_prefix != sys.prefix))
bcedfd
+
bcedfd
+        # Check whether we have root privileges and aren't in venv/virtualenv
bcedfd
+        if os.getuid() == 0 and not is_venv():
bcedfd
+            command = path.basename(sys.argv[0])
bcedfd
+            if command == "__main__.py":
bcedfd
+                command = path.basename(sys.executable) + " -m pip"
bcedfd
+            logger.warning(
bcedfd
+                "Running pip install with root privileges is "
bcedfd
+                "generally not a good idea. Try `%s install --user` instead."
bcedfd
+                % command
bcedfd
+            )
bcedfd
+
bcedfd
         upgrade_strategy = "to-satisfy-only"
bcedfd
         if options.upgrade:
bcedfd
             upgrade_strategy = options.upgrade_strategy