hgkw/version.py
author Christian Ebert <blacktrash@gmx.net>
Wed, 18 Jul 2007 22:33:24 +0200
branchkwmap-templates
changeset 192 0d2a6c9f8343
parent 124 99dc49c5bcfb
permissions -rw-r--r--
Discard self_initializing_hook branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
124
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     1
# $Id$
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     2
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     3
'''version.py - hgkw version.
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     4
Code stolen from Mercurial, and simplified for my needs.
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     5
'''
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     6
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     7
import os, time
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     8
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
     9
unknown_version = 'unknown'
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    10
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    11
def getversion(doreload=False):
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    12
    try:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    13
        import hgkw.__version__
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    14
        if doreload:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    15
            reload(hgkw.__version__)
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    16
        version = hgkw.__version__.version
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    17
    except ImportError:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    18
        version = unknown_version
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    19
    return version
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    20
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    21
def rememberversion(version=None):
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    22
    if not version and os.path.isdir('.hg'):
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    23
        # get version from Mercurial
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    24
        p = os.popen('hg --quiet identify 2> %s' % os.devnull)
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    25
        ident = p.read()[:-1]
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    26
        if not p.close() and ident:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    27
            if ident[-1] != '+':
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    28
                version = ident
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    29
            else:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    30
                version = ident[:-1]
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    31
                version += time.strftime('+%Y%m%d')
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    32
    if version and version != getversion(): # write version
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    33
        directory = os.path.dirname(__file__)
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    34
        for suff in ['py', 'pyc', 'pyo']:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    35
            try:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    36
                os.unlink(os.path.join(directory, '__version__.%s' % suff))
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    37
            except OSError:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    38
                pass
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    39
        f = open(os.path.join(directory, '__version__.py'), 'w')
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    40
        try:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    41
            f.write('# this file is auto-generated\n')
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    42
            f.write('version = %r\n' % version)
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    43
        finally:
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    44
            f.close()
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    45
        # reload file
99dc49c5bcfb Setup standalone module install, version tracking
Christian Ebert <blacktrash@gmx.net>
parents:
diff changeset
    46
        getversion(doreload=True)