[PATCH 4 of 4] setup: set the version string consistently for both archive and in-place builds

Gilles Moris gilles.moris at free.fr
Tue Oct 6 08:18:31 UTC 2009


 setup.py |  72 ++++++++++++++++++++++++------------
 1 files changed, 48 insertions(+), 24 deletions(-)


# HG changeset patch
# User Gilles Moris <gilles.moris at free.fr>
# Date 1249977954 -7200
# Node ID 64e24cd73ef8f9d54a40770ecfcb29ab842c6630
# Parent  bbaf3c703c77f2756c27a8b39a3824be9a13ccc4
setup: set the version string consistently for both archive and in-place builds

If the rev is not based on a tag, this includes mention to the latest tag and the
distance to it.

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -97,6 +97,22 @@
 except ImportError:
     pass
 
+def runcmd(cmd):
+    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                         stderr=subprocess.PIPE, env=env)
+    out, err = p.communicate()
+    # If root is executing setup.py, but the repository is owned by
+    # another user (as in "sudo python setup.py install") we will get
+    # trust warnings since the .hg/hgrc file is untrusted. That is
+    # fine, we don't want to load it anyway.
+    err = [e for e in err.splitlines()
+           if not e.startswith('Not trusting file')]
+    if err:
+        sys.stderr.write('warning: could not establish Mercurial '
+                         'version:\n%s\n' % '\n'.join(err))
+        return ''
+    return out
+
 version = None
 
 if os.path.isdir('.hg'):
@@ -112,39 +128,47 @@
         # under Windows. Otherwise, the subprocess will fail with
         # error 0xc0150004. See: http://bugs.python.org/issue3440
         env['SystemRoot'] = os.environ['SystemRoot']
+
     cmd = [sys.executable, 'hg', 'id', '-i', '-t']
+    l = runcmd(cmd).split()
+    while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
+        l.pop()
 
-    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                         stderr=subprocess.PIPE, env=env)
-    out, err = p.communicate()
+    latesttag = ''
+    if len(l) == 1: # no tag found for that rev
+        # then search for latest tag
+        cmd = [sys.executable, 'hg', 'parents', '--template',
+               ' [{latesttag}+{latesttagdistance}]']
+        latesttag = runcmd(cmd)
 
-    # If root is executing setup.py, but the repository is owned by
-    # another user (as in "sudo python setup.py install") we will get
-    # trust warnings since the .hg/hgrc file is untrusted. That is
-    # fine, we don't want to load it anyway.
-    err = [e for e in err.splitlines()
-           if not e.startswith('Not trusting file')]
-    if err:
-        sys.stderr.write('warning: could not establish Mercurial '
-                         'version:\n%s\n' % '\n'.join(err))
-    else:
-        l = out.split()
-        while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
-            l.pop()
-        if l:
-            version = l[-1] # latest tag or revision number
-            if l[0].endswith('+') and len(l) > 1:
-                # for an unclean working directory based on a tag, we also
-                # need a '+'
-                version += '+'
-            if version.endswith('+'):
-                version += time.strftime('%Y%m%d')
+    if l:
+        version = l[-1] # latest tag or revision number
+        if l[0].endswith('+') and len(l) > 1:
+            # for an dirty working directory based on a tag, we also
+            # need a '+'
+            version += '+'
+        if version.endswith('+'):
+            version += time.strftime('%Y%m%d')
+        version += latesttag
 elif os.path.exists('.hg_archival.txt'):
     hgarchival = open('.hg_archival.txt')
+    latesttag = latesttagdistance = None
     for line in hgarchival:
         if line.startswith('node:'):
             version = line.split(':')[1].strip()[:12]
+        if line.startswith('tag:'):
+            version = line.split(':')[1].strip()
+            # stop on the first tag we find
             break
+        # otherwise, try to fallback to the latest tag
+        if line.startswith('latesttag:'):
+            latesttag = line.split(':', 1)[1].strip()
+        if line.startswith('latesttagdistance:'):
+            latesttagdistance = line.split(':')[1].strip()
+    if latesttag and latesttagdistance:
+        # note that if distance is 0, it will not enter, which is expected
+        version += _(' [%s+%s]') % \
+                   (latesttag, latesttagdistance)
 
 if version:
     f = open("mercurial/__version__.py", "w")



More information about the Mercurial-devel mailing list