orion / rpms / python3x-pip

Forked from rpms/python3x-pip 2 years ago
Clone

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

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