--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/qmsk/irclogs/version.py Sun Sep 13 01:15:56 2009 +0300
@@ -0,0 +1,106 @@
+"""
+ Figuring out the project version
+
+ Currently this only supports mercurial
+"""
+
+# only load this once
+_VERSION = None
+
+def version_mercurial (path) :
+ """
+ Returns a (branch, tags, parents, modified) tuple for the given repo's working copy
+ """
+
+ global _VERSION
+
+ # cached?
+ if _VERSION :
+ return _VERSION
+
+ # code adapted from mercurial.commands.identify
+ from mercurial import ui, hg, encoding
+ from mercurial.node import short
+
+ # open the repo
+ repo = hg.repository(ui.ui(), path)
+
+ # the working copy change context
+ ctx = repo[None]
+
+ # branch
+ branch = encoding.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
+ _VERSION = (branch, tags, parents, modified)
+ return _VERSION
+
+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.
+ """
+
+ try :
+ # get info
+ branch, tags, parents, modified = version_mercurial(path)
+
+ except :
+ # XXX: ignore
+ raise
+
+ # 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
+