PageRenderTime 22ms CodeModel.GetById 9ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tortoisehg/util/version.py

https://bitbucket.org/tortoisehg/hgtk/
Python | 73 lines | 57 code | 10 blank | 6 comment | 15 complexity | 36c36535ec649a2370ee2a07e2291b35 MD5 | raw file
 1# version.py - TortoiseHg version
 2#
 3# Copyright 2009 Steve Borho <steve@borho.org>
 4#
 5# This software may be used and distributed according to the terms of the
 6# GNU General Public License version 2, incorporated herein by reference.
 7
 8import os
 9from mercurial import ui, hg, commands, error
10from tortoisehg.util.i18n import _
11
12def liveversion():
13    'Attempt to read the version from the live repository'
14    utilpath = os.path.dirname(os.path.realpath(__file__))
15    thgpath = os.path.dirname(os.path.dirname(utilpath))
16    if not os.path.isdir(os.path.join(thgpath, '.hg')):
17        raise error.RepoError(_('repository %s not found') % thgpath)
18
19    u = ui.ui()
20    repo = hg.repository(u, path=thgpath)
21
22    u.pushbuffer()
23    commands.identify(u, repo, id=True, tags=True)
24    l = u.popbuffer().split()
25    while len(l) > 1 and l[-1][0].isalpha(): # remove non-numbered tags
26        l.pop()
27    if len(l) > 1: # tag found
28        version = l[-1]
29        if l[0].endswith('+'): # propagate the dirty status to the tag
30            version += '+'
31    elif len(l) == 1: # no tag found
32        u.pushbuffer()
33        commands.parents(u, repo, template='{latesttag}+{latesttagdistance}-')
34        version = u.popbuffer() + l[0]
35    return repo[None].branch(), version
36
37def version():
38    try:
39        branch, version = liveversion()
40        return version
41    except:
42        pass
43    try:
44        import __version__
45        return __version__.version
46    except ImportError:
47        return _('unknown')
48
49def package_version():
50    try:
51        branch, version = liveversion()
52
53        extra = None
54        if '+' in version:
55            version, extra = version.split('+', 1)
56
57        v = [int(x) for x in version.split('.')]
58        while len(v) < 3:
59            v.append(0)
60        major, minor, periodic = v
61
62        if extra != None:
63            tagdistance = int(extra.split('-', 1)[0])
64            periodic *= 10000
65            if branch == 'default':
66                periodic += tagdistance + 5000
67            else:
68                periodic += tagdistance + 1000
69
70        return '.'.join([str(x) for x in (major, minor, periodic)])
71    except:
72        pass
73    return _('unknown')