version.py
changeset 119 df859bfdd3be
child 132 0e857c4a67de
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/version.py	Thu Feb 12 22:34:54 2009 +0200
@@ -0,0 +1,91 @@
+"""
+    Figuring out the project version
+
+    Currently this only supports mercurial
+"""
+
+def version_mercurial (path) :
+    """
+        Returns a (branch, tags, parents, modified) tuple for the given repo's working copy
+    """
+
+    # code adapted from mercurial.commands.identify
+    from mercurial import ui, hg, util
+    from mercurial.node import short
+    
+    # open the repo
+    repo = hg.repository(ui.ui(), path)
+
+    # the working copy change context
+    ctx = repo.workingctx()
+
+    # branch
+    branch = util.tolocal(ctx.branch())
+    
+    # map default -> None
+    if branch == 'default' :
+        branch = None
+    
+    # list of tags, without 'tip' tag
+    tags = [tag for tag in ctx.tags() if tag != 'tip']
+
+    # ctx's parents
+    parents = [short(p.node()) for p in ctx.parents()]
+
+    # local modifications?
+    modified = bool(ctx.files() + ctx.deleted())
+
+    # done
+    return (branch, tags, parents, modified)
+
+def version_string (path='.') :
+    """
+        Return a version string representing the version of the software at the given path.
+
+        Currently, this assumes that the given path points to a local Mercurial repo.
+    """
+    
+    # get info
+    branch, tags, parents, modified = version_mercurial(path)
+
+    # tags: <tag> [ "-" <tag> [ ... ]]
+    if tags :
+        return '-'.join(tags)
+
+    # revision: <parent> [ "+" <parent> [ ... ]] [ "+" ]
+    revision = '+'.join(p for p in parents) + ('+' if modified else '')
+    
+    if branch :
+        # branch: "(" <branch> ")" <revision>
+        return '(%s)%s' % (branch, revision)
+
+    else :
+        # plain: <revision>
+        return revision
+
+def version_link_hg (hgweb_url, path='.') :
+    """
+        Returns a link to a hgweb page for this version
+    """
+
+    # URL for revision ID
+    rev_url = lambda rev: '<a href="%(url)s/rev/%(rev)s">%(rev)s</a>' % dict(url=hgweb_url, rev=rev)
+
+     # get info
+    branch, tags, parents, modified = version_mercurial(path)
+
+    # tags: <tag> [ "-" <tag> [ ... ]] [ "+" ]
+    if tags :
+        return '-'.join(rev_url(tag) for tag in tags) + ('+' if modified else '')
+
+    # revision: <parent> [ "+" <parent> [ ... ]] [ "+" ]
+    revision = '+'.join(rev_url(p) for p in parents) + ('+' if modified else '')
+    
+    if branch :
+        # branch: "(" <branch> ")" <revision> [ "+" ]
+        return '(%s)%s' % (rev_url(branch), revision) + ('+' if modified else '')
+
+    else :
+        # plain: <revision>
+        return revision
+