churchyard / rpms / python2

Forked from rpms/python2 6 years ago
Clone
7143d8a
diff -U3 -r Python-2.7.14.orig/Lib/site.py Python-2.7.14/Lib/site.py
7143d8a
--- Python-2.7.14.orig/Lib/site.py	2018-01-29 15:05:04.517599815 +0100
7143d8a
+++ Python-2.7.14/Lib/site.py	2018-01-30 09:13:17.305270500 +0100
7143d8a
@@ -515,6 +515,41 @@
7143d8a
                 "'import usercustomize' failed; use -v for traceback"
7143d8a
 
7143d8a
 
7143d8a
+def handle_ambiguous_python_version():
7143d8a
+    """Warn or fail if /usr/bin/python is used
7143d8a
+
7143d8a
+    Behavior depends on the value of PYTHON_DISALLOW_AMBIGUOUS_VERSION:
7143d8a
+    - "warn" - print warning to stderr
7143d8a
+    - "1" - print error and exit with positive exit code
7143d8a
+    - otherwise: do nothing
7143d8a
+
7143d8a
+    This is a Fedora modification, see the Change page for details:
7143d8a
+    See https://fedoraproject.org/wiki/Changes/Avoid_usr_bin_python_in_RPM_Build
7143d8a
+    """
7143d8a
+    if sys.executable == "/usr/bin/python":
7143d8a
+        setting = os.environ.get("PYTHON_DISALLOW_AMBIGUOUS_VERSION")
7143d8a
+        if setting == 'warn':
7143d8a
+            print>>sys.stderr, (
7143d8a
+                "DEPRECATION WARNING: python2 invoked with /usr/bin/python.\n"
7143d8a
+                "    Use /usr/bin/python3 or /usr/bin/python2\n"
7143d8a
+                "    /usr/bin/python will be removed or switched to Python 3"
7143d8a
+                    " in the future.\n"
7143d8a
+                "    If you cannot make the switch now, please follow"
7143d8a
+                    " instructions at"
7143d8a
+                    " https://fedoraproject.org/wiki/Changes/"
7143d8a
+                    "Avoid_usr_bin_python_in_RPM_Build#Quick_Opt-Out")
7143d8a
+        elif setting == '1':
7143d8a
+            print>>sys.stderr, (
7143d8a
+                "ERROR: python2 invoked with /usr/bin/python.\n"
7143d8a
+                "    Use /usr/bin/python3 or /usr/bin/python2\n"
7143d8a
+                "    /usr/bin/python will be switched to Python 3"
7143d8a
+                    " in the future.\n"
7143d8a
+                "    More details are at"
7143d8a
+                    " https://fedoraproject.org/wiki/Changes/"
7143d8a
+                    "Avoid_usr_bin_python_in_RPM_Build#Quick_Opt-Out")
7143d8a
+            exit(1)
7143d8a
+
7143d8a
+
7143d8a
 def main():
7143d8a
     global ENABLE_USER_SITE
7143d8a
 
7143d8a
@@ -543,6 +578,7 @@
7143d8a
     # this module is run as a script, because this code is executed twice.
7143d8a
     if hasattr(sys, "setdefaultencoding"):
7143d8a
         del sys.setdefaultencoding
7143d8a
+    handle_ambiguous_python_version()
7143d8a
 
7143d8a
 main()
7143d8a