5a8e99c
From 18a617e9e0f64b727938422d4f941dfddfbf5d00 Mon Sep 17 00:00:00 2001
3d0a786
From: Tomas Orsava <torsava@redhat.com>
3d0a786
Date: Tue, 14 Feb 2017 17:10:09 +0100
5a8e99c
Subject: [PATCH] Emit a warning when running with root privileges.
3d0a786
3d0a786
---
5a8e99c
 pip/commands/install.py | 14 ++++++++++++++
5a8e99c
 1 file changed, 14 insertions(+)
3d0a786
5a8e99c
diff --git a/pip/commands/install.py b/pip/commands/install.py
5a8e99c
index 227c526..277a3d1 100644
3d0a786
--- a/pip/commands/install.py
3d0a786
+++ b/pip/commands/install.py
33d9686
@@ -6,6 +6,8 @@ import os
33d9686
 import tempfile
33d9686
 import shutil
33d9686
 import warnings
33d9686
+import sys
33d9686
+from os import path
33d9686
 try:
33d9686
     import wheel
33d9686
 except ImportError:
5a8e99c
@@ -193,6 +195,18 @@ class InstallCommand(RequirementCommand):
3d0a786
         cmdoptions.resolve_wheel_no_use_binary(options)
3d0a786
         cmdoptions.check_install_build_global(options)
3d0a786
 
5a8e99c
+        def is_venv():
5a8e99c
+            return hasattr(sys, 'real_prefix') or \
5a8e99c
+                    (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)
5a8e99c
+
5a8e99c
+        # Check whether we have root privileges and aren't in venv/virtualenv
5a8e99c
+        if os.getuid() == 0 and not is_venv():
3d0a786
+            logger.warning(
d6fcf17
+                "WARNING: Running pip install with root privileges is "
33d9686
+                "generally not a good idea. Try `%s install --user` instead."
33d9686
+                        % path.basename(sys.argv[0])
3d0a786
+            )
3d0a786
+
3d0a786
         if options.as_egg:
3d0a786
             warnings.warn(
3d0a786
                 "--egg has been deprecated and will be removed in the future. "
3d0a786
-- 
3d0a786
2.11.0
3d0a786