code: def get_version():
      
      try:
          f = open('eztemplate/version.py', 'r')
      except IOError as e:
          if e.errno != errno.ENOENT:
              raise
          m = None
      else:
          m = re.match('^\s*__version__\s*=\s*(?P<version>.*)$', f.read(), re.M)
          f.close()
  
      __version__ = ast.literal_eval(m.group('version')) if m else None
  
      try:
          git_version = subprocess.check_output(['git', 'describe', '--dirty'])
      except:
          if __version__ is None:
              raise ValueError("cannot determine version number")
          return __version__
  
      m = re.match(r'^\s*'
                   r'(?P<version>\S+?)'
                   r'(-(?P<post>\d+)-(?P<commit>g[0-9a-f]+))?'
                   r'(-(?P<dirty>dirty))?'
                   r'\s*$', git_version.decode())
      if not m:
          raise ValueError("cannot parse git describe output")
  
      git_version = m.group('version')
      post = m.group('post')
      commit = m.group('commit')
      dirty = m.group('dirty')
  
      local = []
  
      if post:
          post = int(post)
          if post:
              git_version += '.post%d' % (post,)
              if commit:
                  local.append(commit)
  
      if dirty:
          local.append(dirty)
  
      if local:
          git_version += '+' + '.'.join(local)
  
      if git_version != __version__:
          with open('eztemplate/version.py', 'w') as f:
              f.write("__version__ = %r\n" % (str(git_version),))
  
      return git_version
docs: Build version number from git repository tag.