| 1 | from os import path |
| 2 | import re |
| 3 | from xml.dom import minidom |
| 4 | import django |
| 5 | |
| 6 | def get_svn_revision(): |
| 7 | """ |
| 8 | Returns the SVN revision in the form SVN-XXXX |
| 9 | where XXXX is the revision number. |
| 10 | |
| 11 | Returns SVN-unknown if anything goes wrong. |
| 12 | """ |
| 13 | rev = None |
| 14 | entries_path = '%s/.svn/entries' % django.__path__[0] |
| 15 | |
| 16 | if path.exists(entries_path): |
| 17 | entries = open(entries_path, 'r').read() |
| 18 | # Versions >= 7 of the entries file are flat text. The first line is |
| 19 | # the version number. The next set of digits after 'dir' is the revision. |
| 20 | if re.match('(\d+)', entries): |
| 21 | rev_match = re.search('\d+\s+dir\s+(\d+)', entries) |
| 22 | if rev_match: |
| 23 | rev = rev_match.groups()[0] |
| 24 | # Older XML versions of the file specify revision as an attribute of |
| 25 | # the first entries node. |
| 26 | else: |
| 27 | dom = minidom.parse(entries_path) |
| 28 | rev = dom.getElementsByTagName('entry')[0].getAttribute('revision') |
| 29 | |
| 30 | if rev: |
| 31 | return u'SVN-%s' % rev |
| 32 | return u'SVN-unknown' |
| 33 | |