Getting back revision number - displayed like in GitHub commits (Issue #52)

This commit is contained in:
Miroslav Stampar
2012-07-02 13:01:20 +02:00
parent add8352804
commit 8eefe4b71f
4 changed files with 23 additions and 55 deletions

View File

@@ -12,50 +12,29 @@ from subprocess import PIPE
from subprocess import Popen as execute
def getRevisionNumber():
curDir = os.path.dirname(os.path.realpath(__file__))
retVal = None
filePath = None
try:
import pysvn
client = pysvn.Client()
if client.info(curDir):
retVal = client.info(curDir).revision.number
except ImportError:
process = execute("svn info %s" % curDir, shell=True, stdout=PIPE, stderr=PIPE)
svnStdout, svnStderr = process.communicate()
if svnStdout:
revision = re.search("Revision:\s+([\d]+)", svnStdout)
if revision:
retVal = revision.group(1)
except:
pass
_ = os.path.dirname(__file__)
while True:
filePath = os.path.join(_, ".git/refs/heads/master").replace('/', os.path.sep)
if os.path.exists(filePath):
break
else:
filePath = None
if _ == os.path.dirname(_):
break
else:
_ = os.path.dirname(_)
if filePath:
with open(filePath, "r") as f:
match = re.match(r"(?i)[0-9a-f]{32}", f.read())
retVal = match.group(0) if match else None
if not retVal:
# Reference: http://stackoverflow.com/questions/242295/how-does-one-add-a-svn-repository-build-number-to-python-code
entriesPath = '%s/.svn/entries' % curDir
process = execute("git rev-parse --verify HEAD", shell=True, stdout=PIPE, stderr=PIPE)
stdout, _ = process.communicate()
match = re.search(r"(?i)[0-9a-f]{32}", stdout or "")
retVal = match.group(0) if match else None
if os.path.exists(entriesPath):
entries = open(entriesPath, 'r').read()
# 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):
match = re.search('\d+\s+dir\s+(\d+)', entries)
if match:
retVal = 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(entriesPath)
retVal = dom.getElementsByTagName('entry')[0].getAttribute('revision')
if retVal:
try:
retVal = int(retVal)
except ValueError:
retVal = None
return retVal
return retVal[:10] if retVal else None