[PATCH] setup: refactor the version string to a subset of tag+tagdist-hash+date

Gilles Moris gilles.moris at free.fr
Sun Oct 18 07:40:51 CDT 2009


 setup.py |  64 ++++++++++++++++++++++++++++++++++++----------------------------
 1 files changed, 36 insertions(+), 28 deletions(-)


# HG changeset patch
# User Gilles Moris <gilles.moris at free.fr>
# Date 1255869336 -7200
# Node ID f60e338fb09a11fc3a9e46acb8101dae3c947aac
# Parent  67e886f2c7864e0ee92e7119c28da4580be1e1bf
setup: refactor the version string to a subset of tag+tagdist-hash+date

Here is an array summarizing the mercurial version string:
      [A]       [B]   [C]           [D]
[1] clone      tag   clean  =>  tag
[2] clone      hash  clean  =>  latesttag+latesttagdistance-hash
[3] clone      tag   dirty  =>  tag+date
[4] clone      hash  dirty  =>  latesttag+latesttagdistance-hash+date
[5] archive    tag   clean  =>  tag
[6] archive    hash  clean  =>  latesttag+latesttagdistance-hash

Column [A]: Mercurial built from an hg *archive* or hg *clone* working directory
Column [B]: revision built has a *tag* or else default to the SHA1 *hash*
Column [C]: working tree *clean* or *dirty*
Column [D]: Mercurial version string

Over the previous version:
- row [5] did return just the node hash, now it returns the tag
- prepend the latest tag and the distance to it to rows [2][4][6]
- append also the date to row [3]; previously, it was just the tag
- the version string is with an empty string to avoid possible TypeError
  exceptions during string manipulations
- factorize the function to run hg commands; remove the error message as it is
  no more specific to the function.

This scheme enables to have first part of the version strings that can be
compared, whether it has been built from a tagged or untagged revision.
The second part of the version adds a hash for untagged revisions and today's
date if the working tree has local modifications.
As the version string does not contain spaces or special characters, it should
not break script parsing the 'hg version' command and should be usable for use
in file names.

The new code also ensure that the version string has exactly the same version
string, whether it has been built from an archive or from a clone.

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -97,7 +97,21 @@
 except ImportError:
     pass
 
-version = None
+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:
+        return ''
+    return out
+
+version = ''
 
 if os.path.isdir('.hg'):
     # Execute hg out of this directory with a custom environment which
@@ -113,34 +127,28 @@
         # error 0xc0150004. See: http://bugs.python.org/issue3440
         env['SystemRoot'] = os.environ['SystemRoot']
     cmd = [sys.executable, 'hg', 'id', '-i', '-t']
-
-    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))
+    l = runcmd(cmd).split()
+    while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
+        l.pop()
+    if len(l) > 1: # tag found
+        version = l[-1]
+        if l[0].endswith('+'): # propagate the dirty status to the tag
+            version += '+'
+    elif len(l) == 1: # no tag found
+        cmd = [sys.executable, 'hg', 'parents', '--template',
+               '{latesttag}+{latesttagdistance}-']
+        version = runcmd(cmd) + l[0]
+    if version.endswith('+'):
+        version += time.strftime('%Y%m%d')
+elif os.path.exists('.hg_archival.txt'):
+    kw = dict([t.strip() for t in l.split(':', 1)]
+              for l in open('.hg_archival.txt'))
+    if kw.get('tag'):
+        version =  kw.get('tag')
+    elif kw.get('latesttag') and kw.get('latesttagdistance', '0') != '0':
+        version = '%(latesttag)s+%(latesttagdistance)s-%(node).12s' % kw
     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 version.endswith('+'):
-                version += time.strftime('%Y%m%d')
-elif os.path.exists('.hg_archival.txt'):
-    hgarchival = open('.hg_archival.txt')
-    for line in hgarchival:
-        if line.startswith('node:'):
-            version = line.split(':')[1].strip()[:12]
-            break
+        version = kw.get('node', '')[:12]
 
 if version:
     f = open("mercurial/__version__.py", "w")


More information about the Mercurial-devel mailing list