hgkw/version.py
branchkwmap-templates
changeset 124 99dc49c5bcfb
equal deleted inserted replaced
123:6cf933de685a 124:99dc49c5bcfb
       
     1 # $Id$
       
     2 
       
     3 '''version.py - hgkw version.
       
     4 Code stolen from Mercurial, and simplified for my needs.
       
     5 '''
       
     6 
       
     7 import os, time
       
     8 
       
     9 unknown_version = 'unknown'
       
    10 
       
    11 def getversion(doreload=False):
       
    12     try:
       
    13         import hgkw.__version__
       
    14         if doreload:
       
    15             reload(hgkw.__version__)
       
    16         version = hgkw.__version__.version
       
    17     except ImportError:
       
    18         version = unknown_version
       
    19     return version
       
    20 
       
    21 def rememberversion(version=None):
       
    22     if not version and os.path.isdir('.hg'):
       
    23         # get version from Mercurial
       
    24         p = os.popen('hg --quiet identify 2> %s' % os.devnull)
       
    25         ident = p.read()[:-1]
       
    26         if not p.close() and ident:
       
    27             if ident[-1] != '+':
       
    28                 version = ident
       
    29             else:
       
    30                 version = ident[:-1]
       
    31                 version += time.strftime('+%Y%m%d')
       
    32     if version and version != getversion(): # write version
       
    33         directory = os.path.dirname(__file__)
       
    34         for suff in ['py', 'pyc', 'pyo']:
       
    35             try:
       
    36                 os.unlink(os.path.join(directory, '__version__.%s' % suff))
       
    37             except OSError:
       
    38                 pass
       
    39         f = open(os.path.join(directory, '__version__.py'), 'w')
       
    40         try:
       
    41             f.write('# this file is auto-generated\n')
       
    42             f.write('version = %r\n' % version)
       
    43         finally:
       
    44             f.close()
       
    45         # reload file
       
    46         getversion(doreload=True)