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

52 lines
1.8 KiB
Diff
Raw Normal View History

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