Patchwork [5,of,5,stable] setup: add extra logic to try and recommend a new pip on bad Python

login
register
mail settings
Submitter Augie Fackler
Date July 24, 2017, 9:08 p.m.
Message ID <8d381b895e19ad447069.1500930493@augie-macbookpro2.roam.corp.google.com>
Download mbox | patch
Permalink /patch/22547/
State Accepted
Headers show

Comments

Augie Fackler - July 24, 2017, 9:08 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1500648391 14400
#      Fri Jul 21 10:46:31 2017 -0400
# Branch stable
# Node ID 8d381b895e19ad447069529aa5c5e7358c4b0b48
# Parent  6d82dcfad5dfb1b1a83c47055dc8a2377c59701b
setup: add extra logic to try and recommend a new pip on bad Python

Modern pip can detect supported Python versions (which we now
declare), and pull down a reasonable release. This trick was suggested
in http://bit.ly/pycon2017-build-bridges, and seems like a good
defensive maneuver so that when we want to move to Python 3 it's
less risky for existing users.

This moves the version-check logic after defining our printf function
so we can print more informative messages.
Yuya Nishihara - July 26, 2017, 2:31 p.m.
On Mon, 24 Jul 2017 17:08:13 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1500648391 14400
> #      Fri Jul 21 10:46:31 2017 -0400
> # Branch stable
> # Node ID 8d381b895e19ad447069529aa5c5e7358c4b0b48
> # Parent  6d82dcfad5dfb1b1a83c47055dc8a2377c59701b
> setup: add extra logic to try and recommend a new pip on bad Python

Queued, thanks.

> +    error = """
> +Mercurial does not support Python older than 2.7.
> +Python {py} detected.
> +{pip}
> +""".format(py=sys.version_info, pip=pip_message)

setup.py can't be parsed by Python 2.5, so using str.format() should be fine.

Patch

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -24,9 +24,6 @@  if 'HGALLOWPYTHON3':
     ])
 
 import sys, platform
-if sys.version_info < (2, 7, 0, 'final'):
-    raise SystemExit('Mercurial requires Python 2.7 or later.')
-
 if sys.version_info[0] >= 3:
     printf = eval('print')
     libdir_escape = 'unicode_escape'
@@ -37,6 +34,33 @@  else:
         end = kwargs.get('end', '\n')
         f.write(b' '.join(args) + end)
 
+# Attempt to guide users to a modern pip - this means that 2.6 users
+# should have a chance of getting a 4.2 release, and when we ratchet
+# the version requirement forward again hopefully everyone will get
+# something that works for them.
+if sys.version_info < (2, 7, 0, 'final'):
+    pip_message = ('This may be due to an out of date pip. '
+                   'Make sure you have pip >= 9.0.1.')
+    try:
+        import pip
+        pip_version = tuple([int(x) for x in pip.__version__.split('.')[:3]])
+        if pip_version < (9, 0, 1) :
+            pip_message = (
+                'Your pip version is out of date, please install '
+                'pip >= 9.0.1. pip {} detected.'.format(pip.__version__))
+        else:
+            # pip is new enough - it must be something else
+            pip_message = ''
+    except Exception:
+        pass
+    error = """
+Mercurial does not support Python older than 2.7.
+Python {py} detected.
+{pip}
+""".format(py=sys.version_info, pip=pip_message)
+    printf(error, file=sys.stderr)
+    sys.exit(1)
+
 # Solaris Python packaging brain damage
 try:
     import hashlib