changeset 9615:f51d1822d6fd

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.
author Gilles Moris <gilles.moris@free.fr>
date Sun, 18 Oct 2009 14:35:36 +0200
parents 58edd448da4f
children 0fedf8233b21
files setup.py
diffstat 1 files changed, 36 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py	Tue Aug 11 09:04:02 2009 +0200
+++ b/setup.py	Sun Oct 18 14:35:36 2009 +0200
@@ -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 'tag' in kw:
+        version =  kw['tag']
+    elif 'latesttag' in kw:
+        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")