app/django/utils/version.py
changeset 54 03e267d67478
child 1279 f85ac8856dca
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 import django
       
     2 import os.path
       
     3 import re
       
     4 
       
     5 def get_svn_revision(path=None):
       
     6     """
       
     7     Returns the SVN revision in the form SVN-XXXX,
       
     8     where XXXX is the revision number.
       
     9 
       
    10     Returns SVN-unknown if anything goes wrong, such as an unexpected
       
    11     format of internal SVN files.
       
    12 
       
    13     If path is provided, it should be a directory whose SVN info you want to
       
    14     inspect. If it's not provided, this will use the root django/ package
       
    15     directory.
       
    16     """
       
    17     rev = None
       
    18     if path is None:
       
    19         path = django.__path__[0]
       
    20     entries_path = '%s/.svn/entries' % path
       
    21 
       
    22     if os.path.exists(entries_path):
       
    23         entries = open(entries_path, 'r').read()
       
    24         # Versions >= 7 of the entries file are flat text.  The first line is
       
    25         # the version number. The next set of digits after 'dir' is the revision.
       
    26         if re.match('(\d+)', entries):
       
    27             rev_match = re.search('\d+\s+dir\s+(\d+)', entries)
       
    28             if rev_match:
       
    29                 rev = rev_match.groups()[0]
       
    30         # Older XML versions of the file specify revision as an attribute of
       
    31         # the first entries node.
       
    32         else:
       
    33             from xml.dom import minidom
       
    34             dom = minidom.parse(entries_path)
       
    35             rev = dom.getElementsByTagName('entry')[0].getAttribute('revision')
       
    36 
       
    37     if rev:
       
    38         return u'SVN-%s' % rev
       
    39     return u'SVN-unknown'