version.py
author Tero Marttila <terom@fixme.fi>
Thu, 12 Feb 2009 23:07:18 +0200
changeset 120 b5f746e9c3c2
parent 119 df859bfdd3be
child 132 0e857c4a67de
permissions -rw-r--r--
tag version 0.1.0-rc1
"""
    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