|
12 | 12 | from subprocess import Popen as execute |
13 | 13 |
|
14 | 14 | def getRevisionNumber(): |
15 | | - curDir = os.path.dirname(os.path.realpath(__file__)) |
16 | 15 | retVal = None |
17 | | - |
18 | | - try: |
19 | | - import pysvn |
20 | | - |
21 | | - client = pysvn.Client() |
22 | | - if client.info(curDir): |
23 | | - retVal = client.info(curDir).revision.number |
24 | | - except ImportError: |
25 | | - process = execute("svn info %s" % curDir, shell=True, stdout=PIPE, stderr=PIPE) |
26 | | - svnStdout, svnStderr = process.communicate() |
27 | | - |
28 | | - if svnStdout: |
29 | | - revision = re.search("Revision:\s+([\d]+)", svnStdout) |
30 | | - |
31 | | - if revision: |
32 | | - retVal = revision.group(1) |
33 | | - except: |
34 | | - pass |
35 | | - |
36 | | - if not retVal: |
37 | | - # Reference: http://stackoverflow.com/questions/242295/how-does-one-add-a-svn-repository-build-number-to-python-code |
38 | | - entriesPath = '%s/.svn/entries' % curDir |
39 | | - |
40 | | - if os.path.exists(entriesPath): |
41 | | - entries = open(entriesPath, 'r').read() |
42 | | - # Versions >= 7 of the entries file are flat text. The first line is |
43 | | - # the version number. The next set of digits after 'dir' is the revision. |
44 | | - if re.match('(\d+)', entries): |
45 | | - match = re.search('\d+\s+dir\s+(\d+)', entries) |
46 | | - if match: |
47 | | - retVal = match.groups()[0] |
48 | | - # Older XML versions of the file specify revision as an attribute of |
49 | | - # the first entries node. |
| 16 | + filePath = None |
| 17 | + |
| 18 | + _ = os.path.dirname(__file__) |
| 19 | + while True: |
| 20 | + filePath = os.path.join(_, ".git/refs/heads/master").replace('/', os.path.sep) |
| 21 | + if os.path.exists(filePath): |
| 22 | + break |
| 23 | + else: |
| 24 | + filePath = None |
| 25 | + if _ == os.path.dirname(_): |
| 26 | + break |
50 | 27 | else: |
51 | | - from xml.dom import minidom |
52 | | - dom = minidom.parse(entriesPath) |
53 | | - retVal = dom.getElementsByTagName('entry')[0].getAttribute('revision') |
| 28 | + _ = os.path.dirname(_) |
| 29 | + if filePath: |
| 30 | + with open(filePath, "r") as f: |
| 31 | + match = re.match(r"(?i)[0-9a-f]{32}", f.read()) |
| 32 | + retVal = match.group(0) if match else None |
54 | 33 |
|
55 | | - if retVal: |
56 | | - try: |
57 | | - retVal = int(retVal) |
58 | | - except ValueError: |
59 | | - retVal = None |
| 34 | + if not retVal: |
| 35 | + process = execute("git rev-parse --verify HEAD", shell=True, stdout=PIPE, stderr=PIPE) |
| 36 | + stdout, _ = process.communicate() |
| 37 | + match = re.search(r"(?i)[0-9a-f]{32}", stdout or "") |
| 38 | + retVal = match.group(0) if match else None |
60 | 39 |
|
61 | | - return retVal |
| 40 | + return retVal[:10] if retVal else None |
0 commit comments