terom@119: """ terom@119: Figuring out the project version terom@119: terom@119: Currently this only supports mercurial terom@119: """ terom@119: terom@119: def version_mercurial (path) : terom@119: """ terom@119: Returns a (branch, tags, parents, modified) tuple for the given repo's working copy terom@119: """ terom@119: terom@119: # code adapted from mercurial.commands.identify terom@119: from mercurial import ui, hg, util terom@119: from mercurial.node import short terom@119: terom@119: # open the repo terom@119: repo = hg.repository(ui.ui(), path) terom@119: terom@119: # the working copy change context terom@119: ctx = repo.workingctx() terom@119: terom@119: # branch terom@119: branch = util.tolocal(ctx.branch()) terom@119: terom@119: # map default -> None terom@119: if branch == 'default' : terom@119: branch = None terom@119: terom@119: # list of tags, without 'tip' tag terom@119: tags = [tag for tag in ctx.tags() if tag != 'tip'] terom@119: terom@119: # ctx's parents terom@119: parents = [short(p.node()) for p in ctx.parents()] terom@119: terom@119: # local modifications? terom@119: modified = bool(ctx.files() + ctx.deleted()) terom@119: terom@119: # done terom@119: return (branch, tags, parents, modified) terom@119: terom@119: def version_string (path='.') : terom@119: """ terom@119: Return a version string representing the version of the software at the given path. terom@119: terom@119: Currently, this assumes that the given path points to a local Mercurial repo. terom@119: """ terom@119: terom@119: # get info terom@119: branch, tags, parents, modified = version_mercurial(path) terom@119: terom@119: # tags: [ "-" [ ... ]] terom@119: if tags : terom@119: return '-'.join(tags) terom@119: terom@119: # revision: [ "+" [ ... ]] [ "+" ] terom@119: revision = '+'.join(p for p in parents) + ('+' if modified else '') terom@119: terom@119: if branch : terom@119: # branch: "(" ")" terom@119: return '(%s)%s' % (branch, revision) terom@119: terom@119: else : terom@119: # plain: terom@119: return revision terom@119: terom@119: def version_link_hg (hgweb_url, path='.') : terom@119: """ terom@119: Returns a link to a hgweb page for this version terom@119: """ terom@119: terom@119: # URL for revision ID terom@119: rev_url = lambda rev: '%(rev)s' % dict(url=hgweb_url, rev=rev) terom@119: terom@119: # get info terom@119: branch, tags, parents, modified = version_mercurial(path) terom@119: terom@119: # tags: [ "-" [ ... ]] [ "+" ] terom@119: if tags : terom@119: return '-'.join(rev_url(tag) for tag in tags) + ('+' if modified else '') terom@119: terom@119: # revision: [ "+" [ ... ]] [ "+" ] terom@119: revision = '+'.join(rev_url(p) for p in parents) + ('+' if modified else '') terom@119: terom@119: if branch : terom@119: # branch: "(" ")" [ "+" ] terom@119: return '(%s)%s' % (rev_url(branch), revision) + ('+' if modified else '') terom@119: terom@119: else : terom@119: # plain: terom@119: return revision terom@119: