gotmax23 / rpms / python3x-pip

Forked from rpms/python3x-pip 2 years ago
Clone

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

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