python-pip/emit-a-warning-when-running-with-root-privileges.patch

38 lines
1.3 KiB
Diff
Raw Normal View History

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