hgkw/version.py
author Christian Ebert <blacktrash@gmx.net>
Wed, 14 Feb 2007 03:03:16 +0100
branchkwmap-templates
changeset 144 19b3d1de3db0
parent 124 99dc49c5bcfb
permissions -rw-r--r--
Move kwfilelog out of reposetup; only use kwfilelog if file matches Again: Message-ID: <20070213102701.GC30656@cecm.usp.br> This foremost obsoletes ugly attribute checks for kwfmatcher. We check whether we need kwfilelog in kwrepo's file(). That way kwtemplater is always an attribute of kwfilelog. Repo references are gone from kwfilelog. reposetup doc more verbose.

# $Id$

'''version.py - hgkw version.
Code stolen from Mercurial, and simplified for my needs.
'''

import os, time

unknown_version = 'unknown'

def getversion(doreload=False):
    try:
        import hgkw.__version__
        if doreload:
            reload(hgkw.__version__)
        version = hgkw.__version__.version
    except ImportError:
        version = unknown_version
    return version

def rememberversion(version=None):
    if not version and os.path.isdir('.hg'):
        # get version from Mercurial
        p = os.popen('hg --quiet identify 2> %s' % os.devnull)
        ident = p.read()[:-1]
        if not p.close() and ident:
            if ident[-1] != '+':
                version = ident
            else:
                version = ident[:-1]
                version += time.strftime('+%Y%m%d')
    if version and version != getversion(): # write version
        directory = os.path.dirname(__file__)
        for suff in ['py', 'pyc', 'pyo']:
            try:
                os.unlink(os.path.join(directory, '__version__.%s' % suff))
            except OSError:
                pass
        f = open(os.path.join(directory, '__version__.py'), 'w')
        try:
            f.write('# this file is auto-generated\n')
            f.write('version = %r\n' % version)
        finally:
            f.close()
        # reload file
        getversion(doreload=True)