2007-08-21 11:08:02 +08:00
|
|
|
import django
|
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
|
2007-08-24 11:53:04 +08:00
|
|
|
def get_svn_revision(path=None):
|
2007-08-21 11:08:02 +08:00
|
|
|
"""
|
|
|
|
Returns the SVN revision in the form SVN-XXXX,
|
|
|
|
where XXXX is the revision number.
|
|
|
|
|
|
|
|
Returns SVN-unknown if anything goes wrong, such as an unexpected
|
|
|
|
format of internal SVN files.
|
2007-08-24 11:53:04 +08:00
|
|
|
|
|
|
|
If path is provided, it should be a directory whose SVN info you want to
|
|
|
|
inspect. If it's not provided, this will use the root django/ package
|
|
|
|
directory.
|
2007-08-21 11:08:02 +08:00
|
|
|
"""
|
|
|
|
rev = None
|
2007-08-24 11:53:04 +08:00
|
|
|
if path is None:
|
|
|
|
path = django.__path__[0]
|
|
|
|
entries_path = '%s/.svn/entries' % path
|
2007-08-21 11:08:02 +08:00
|
|
|
|
2009-04-04 04:28:23 +08:00
|
|
|
try:
|
2007-08-21 11:08:02 +08:00
|
|
|
entries = open(entries_path, 'r').read()
|
2009-04-04 04:28:23 +08:00
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
else:
|
2007-08-21 11:08:02 +08:00
|
|
|
# Versions >= 7 of the entries file are flat text. The first line is
|
|
|
|
# the version number. The next set of digits after 'dir' is the revision.
|
|
|
|
if re.match('(\d+)', entries):
|
|
|
|
rev_match = re.search('\d+\s+dir\s+(\d+)', entries)
|
|
|
|
if rev_match:
|
|
|
|
rev = rev_match.groups()[0]
|
|
|
|
# Older XML versions of the file specify revision as an attribute of
|
|
|
|
# the first entries node.
|
|
|
|
else:
|
|
|
|
from xml.dom import minidom
|
|
|
|
dom = minidom.parse(entries_path)
|
|
|
|
rev = dom.getElementsByTagName('entry')[0].getAttribute('revision')
|
|
|
|
|
|
|
|
if rev:
|
|
|
|
return u'SVN-%s' % rev
|
|
|
|
return u'SVN-unknown'
|