version.py
changeset 119 df859bfdd3be
child 132 0e857c4a67de
equal deleted inserted replaced
118:f530c158aa07 119:df859bfdd3be
       
     1 """
       
     2     Figuring out the project version
       
     3 
       
     4     Currently this only supports mercurial
       
     5 """
       
     6 
       
     7 def version_mercurial (path) :
       
     8     """
       
     9         Returns a (branch, tags, parents, modified) tuple for the given repo's working copy
       
    10     """
       
    11 
       
    12     # code adapted from mercurial.commands.identify
       
    13     from mercurial import ui, hg, util
       
    14     from mercurial.node import short
       
    15     
       
    16     # open the repo
       
    17     repo = hg.repository(ui.ui(), path)
       
    18 
       
    19     # the working copy change context
       
    20     ctx = repo.workingctx()
       
    21 
       
    22     # branch
       
    23     branch = util.tolocal(ctx.branch())
       
    24     
       
    25     # map default -> None
       
    26     if branch == 'default' :
       
    27         branch = None
       
    28     
       
    29     # list of tags, without 'tip' tag
       
    30     tags = [tag for tag in ctx.tags() if tag != 'tip']
       
    31 
       
    32     # ctx's parents
       
    33     parents = [short(p.node()) for p in ctx.parents()]
       
    34 
       
    35     # local modifications?
       
    36     modified = bool(ctx.files() + ctx.deleted())
       
    37 
       
    38     # done
       
    39     return (branch, tags, parents, modified)
       
    40 
       
    41 def version_string (path='.') :
       
    42     """
       
    43         Return a version string representing the version of the software at the given path.
       
    44 
       
    45         Currently, this assumes that the given path points to a local Mercurial repo.
       
    46     """
       
    47     
       
    48     # get info
       
    49     branch, tags, parents, modified = version_mercurial(path)
       
    50 
       
    51     # tags: <tag> [ "-" <tag> [ ... ]]
       
    52     if tags :
       
    53         return '-'.join(tags)
       
    54 
       
    55     # revision: <parent> [ "+" <parent> [ ... ]] [ "+" ]
       
    56     revision = '+'.join(p for p in parents) + ('+' if modified else '')
       
    57     
       
    58     if branch :
       
    59         # branch: "(" <branch> ")" <revision>
       
    60         return '(%s)%s' % (branch, revision)
       
    61 
       
    62     else :
       
    63         # plain: <revision>
       
    64         return revision
       
    65 
       
    66 def version_link_hg (hgweb_url, path='.') :
       
    67     """
       
    68         Returns a link to a hgweb page for this version
       
    69     """
       
    70 
       
    71     # URL for revision ID
       
    72     rev_url = lambda rev: '<a href="%(url)s/rev/%(rev)s">%(rev)s</a>' % dict(url=hgweb_url, rev=rev)
       
    73 
       
    74      # get info
       
    75     branch, tags, parents, modified = version_mercurial(path)
       
    76 
       
    77     # tags: <tag> [ "-" <tag> [ ... ]] [ "+" ]
       
    78     if tags :
       
    79         return '-'.join(rev_url(tag) for tag in tags) + ('+' if modified else '')
       
    80 
       
    81     # revision: <parent> [ "+" <parent> [ ... ]] [ "+" ]
       
    82     revision = '+'.join(rev_url(p) for p in parents) + ('+' if modified else '')
       
    83     
       
    84     if branch :
       
    85         # branch: "(" <branch> ")" <revision> [ "+" ]
       
    86         return '(%s)%s' % (rev_url(branch), revision) + ('+' if modified else '')
       
    87 
       
    88     else :
       
    89         # plain: <revision>
       
    90         return revision
       
    91