From 332b947dfc8d0f0d3a4525864b121d0f239beb4d Mon Sep 17 00:00:00 2001
From: Michal Cyprian <mcyprian@redhat.com>
Date: Mon, 26 Jun 2017 16:32:56 +0200
Subject: [PATCH] Make pip and distutils in user environment install into
 separate location

---
 00251-change-user-install-location.patch | 46 ++++++++++++++++++++++++
 python3.spec                             | 12 ++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 00251-change-user-install-location.patch

diff --git a/00251-change-user-install-location.patch b/00251-change-user-install-location.patch
new file mode 100644
index 0000000..1dcd9f2
--- /dev/null
+++ b/00251-change-user-install-location.patch
@@ -0,0 +1,46 @@
+diff --git a/Lib/distutils/command/install.py b/Lib/distutils/command/install.py
+index 9d31d13..ed44a93 100644
+--- a/Lib/distutils/command/install.py
++++ b/Lib/distutils/command/install.py
+@@ -424,8 +424,18 @@ class install(Command):
+                     raise DistutilsOptionError(
+                           "must not supply exec-prefix without prefix")
+
+-                self.prefix = os.path.normpath(sys.prefix)
+-                self.exec_prefix = os.path.normpath(sys.exec_prefix)
++                # self.prefix is set to sys.prefix + /local/
++                # if the executable is /usr/bin/python* and RPM build
++                # is not detected to make pip and distutils install into
++                # the separate location.
++                if (sys.executable.startswith("/usr/bin/python")
++                    and 'RPM_BUILD_ROOT' not in os.environ):
++                    addition = "/local"
++                else:
++                    addition = ""
++
++                self.prefix = os.path.normpath(sys.prefix) + addition
++                self.exec_prefix = os.path.normpath(sys.exec_prefix) + addition
+
+             else:
+                 if self.exec_prefix is None:
+diff --git a/Lib/site.py b/Lib/site.py
+index 4744eb0..b5fe571 100644
+--- a/Lib/site.py
++++ b/Lib/site.py
+@@ -326,7 +326,15 @@ def getsitepackages(prefixes=None):
+     return sitepackages
+
+ def addsitepackages(known_paths, prefixes=None):
+-    """Add site-packages to sys.path"""
++    """Add site-packages to sys.path.
++
++    '/usr/local' is included in PREFIXES if the executable is /usr/bin/python*
++    and RPM build is not detected to make sudo pip installed packages visible.
++
++    """
++    if (ENABLE_USER_SITE and sys.executable.startswith("/usr/bin/python")
++        and 'RPM_BUILD_ROOT' not in os.environ):
++        PREFIXES.insert(0, "/usr/local")
+     for sitedir in getsitepackages(prefixes):
+         if os.path.isdir(sitedir):
+             addsitedir(sitedir, known_paths)
diff --git a/python3.spec b/python3.spec
index af93948..9ac8875 100644
--- a/python3.spec
+++ b/python3.spec
@@ -133,7 +133,7 @@
 Summary: Version 3 of the Python programming language aka Python 3000
 Name: python3
 Version: %{pybasever}.1
-Release: 8%{?dist}
+Release: 9%{?dist}
 License: Python
 Group: Development/Languages
 
@@ -417,6 +417,12 @@ Patch243: 00243-fix-mips64-triplet.patch
 # Not yet fixed upstream: http://bugs.python.org/issue28787
 Patch249: 00249-fix-out-of-tree-dtrace-builds.patch
 
+# 00251
+# Set values of prefix and exec_prefix in distutils install command
+# to /usr/local if executable is /usr/bin/python* and RPM build
+# is not detected to make pip and distutils install into separate location
+Patch251: 00251-change-user-install-location.patch
+
 # 00252
 # Add executable option to install.py command to make it work for
 # scripts specified as an entry_points
@@ -724,6 +730,7 @@ sed -r -i s/'_PIP_VERSION = "[0-9.]+"'/'_PIP_VERSION = "%{pip_version}"'/ Lib/en
 %patch206 -p1
 %patch243 -p1
 %patch249 -p1
+%patch251 -p1
 %patch252 -p1
 %patch258 -p1
 %patch261 -p1
@@ -1695,6 +1702,9 @@ fi
 # ======================================================
 
 %changelog
+* Mon Jun 26 2017 Michal Cyprian <mcyprian@redhat.com> - 3.6.1-9
+- Make pip and distutils in user environment install into separate location
+
 * Fri Jun 23 2017 Charalampos Stratakis <cstratak@redhat.com> - 3.6.1-8
 - Fix test_alpn_protocols from test_ssl
 - Do not require rebundled setuptools dependencies