|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | | -'''Utility functions used by scons and sphinx for version extraction. |
4 | | -''' |
| 3 | +"""Utility functions used by scons and sphinx for version extraction.""" |
5 | 4 |
|
6 | 5 | import os |
7 | 6 | import re |
|
21 | 20 |
|
22 | 21 |
|
23 | 22 | def gitinfo(): |
24 | | - 'Extract dictionary of version data from git records.' |
25 | | - from subprocess import Popen, PIPE |
| 23 | + "Extract dictionary of version data from git records." |
| 24 | + from subprocess import PIPE, Popen |
| 25 | + |
26 | 26 | global _cached_gitinfo |
27 | 27 | if _cached_gitinfo is not None: |
28 | 28 | return _cached_gitinfo |
29 | | - nullfile = open(os.devnull, 'w') |
30 | | - kw = dict(stdout=PIPE, stderr=nullfile, cwd=MYDIR, |
31 | | - universal_newlines=True) |
32 | | - proc = Popen(['git', 'describe', '--match=v[[:digit:]]*'], **kw) |
| 29 | + nullfile = open(os.devnull, "w") |
| 30 | + kw = dict(stdout=PIPE, stderr=nullfile, cwd=MYDIR, universal_newlines=True) |
| 31 | + proc = Popen(["git", "describe", "--match=v[[:digit:]]*"], **kw) |
33 | 32 | desc = proc.stdout.read() |
34 | 33 | if proc.wait(): |
35 | 34 | _cached_gitinfo = {} |
36 | 35 | return gitinfo() |
37 | | - proc = Popen(['git', 'log', '-1', '--format=%H %ci'], **kw) |
| 36 | + proc = Popen(["git", "log", "-1", "--format=%H %ci"], **kw) |
38 | 37 | glog = proc.stdout.read() |
39 | | - words = desc.strip().split('-') |
40 | | - vtag = words[0].lstrip('v') |
41 | | - vnum = int((words[1:2] or ['0'])[0]) |
| 38 | + words = desc.strip().split("-") |
| 39 | + vtag = words[0].lstrip("v") |
| 40 | + vnum = int((words[1:2] or ["0"])[0]) |
42 | 41 | rv = {} |
43 | | - rv['commit'], rv['date'] = glog.strip().split(None, 1) |
44 | | - rv['patchnumber'] = vnum |
45 | | - rv['version'] = vtag |
| 42 | + rv["commit"], rv["date"] = glog.strip().split(None, 1) |
| 43 | + rv["patchnumber"] = vnum |
| 44 | + rv["version"] = vtag |
46 | 45 | if vnum: |
47 | | - rv['version'] += '.post' + str(vnum) |
| 46 | + rv["version"] += ".post" + str(vnum) |
48 | 47 | _cached_gitinfo = rv |
49 | 48 | return gitinfo() |
50 | 49 |
|
| 50 | + |
51 | 51 | _cached_gitinfo = None |
52 | 52 |
|
53 | 53 |
|
54 | 54 | def getversion(): |
55 | 55 | """Extract version from gitinfo and/or gitarchive.cfg file. |
56 | 56 |
|
57 | | - Process gitinfo first and make sure it matches FALLBACK_VERSION |
58 | | - in gitarchive.cfg. Use expanded data from gitarchive.cfg when |
59 | | - these sources are from git archive bundle. |
| 57 | + Process gitinfo first and make sure it matches FALLBACK_VERSION in |
| 58 | + gitarchive.cfg. Use expanded data from gitarchive.cfg when these |
| 59 | + sources are from git archive bundle. |
60 | 60 | """ |
61 | 61 | from fallback_version import FALLBACK_VERSION |
62 | | - gitarchivecfgfile = os.path.join(MYDIR, 'gitarchive.cfg') |
| 62 | + |
| 63 | + gitarchivecfgfile = os.path.join(MYDIR, "gitarchive.cfg") |
63 | 64 | assert os.path.isfile(gitarchivecfgfile) |
64 | 65 | with open(gitarchivecfgfile) as fp: |
65 | 66 | gacontent = fp.read() |
66 | | - gaitems = re.findall(r'^(\w+) *= *(\S.*?)\s*$', gacontent, re.M) |
| 67 | + gaitems = re.findall(r"^(\w+) *= *(\S.*?)\s*$", gacontent, re.M) |
67 | 68 | ga = dict(gaitems) |
68 | 69 | gi = gitinfo() |
69 | 70 | rv = {} |
70 | 71 | if gi: |
71 | 72 | afb = FALLBACK_VERSION |
72 | | - gfb = gi['version'].split('.post')[0] + '.post0' |
| 73 | + gfb = gi["version"].split(".post")[0] + ".post0" |
73 | 74 | if gfb != afb: |
74 | 75 | raise RuntimeError(EMSG_BAD_FALLBACK_VERSION.format(afb, gfb)) |
75 | 76 | rv.update(gi) |
76 | 77 | else: |
77 | 78 | # Not a git repository. Require that gitarchive.cfg is expanded. |
78 | | - if '$Format:' in ga['commit']: |
| 79 | + if "$Format:" in ga["commit"]: |
79 | 80 | raise RuntimeError(EMSG_GIT_MISSING) |
80 | | - rv['commit'] = ga['commit'] |
81 | | - rv['date'] = ga['date'] |
| 81 | + rv["commit"] = ga["commit"] |
| 82 | + rv["date"] = ga["date"] |
82 | 83 | # First assume we have an undetermined post-release. Keep version |
83 | 84 | # suffix as ".post0", but set patchnumber to ensure we are post |
84 | 85 | # FALLBACK_VERSION. |
85 | | - rv['version'] = FALLBACK_VERSION |
86 | | - rv['patchnumber'] = 1 |
| 86 | + rv["version"] = FALLBACK_VERSION |
| 87 | + rv["patchnumber"] = 1 |
87 | 88 | # If we are at version tag turn this to regular release. |
88 | | - mx = re.search(r'\btag: v(\d[^,]*)', ga['refnames']) |
| 89 | + mx = re.search(r"\btag: v(\d[^,]*)", ga["refnames"]) |
89 | 90 | if mx: |
90 | | - rv['version'] = mx.group(1) |
91 | | - rv['patchnumber'] = 0 |
| 91 | + rv["version"] = mx.group(1) |
| 92 | + rv["patchnumber"] = 0 |
92 | 93 | # rv['version'] is resolved, let's parse its subfields. |
93 | | - vbase = rv['version'].split('.post')[0] |
94 | | - mx = re.match(r'^(\d+)\.(\d+)(?:\.(\d+))?((?:[ab]|rc)\d*)?', vbase) |
95 | | - rv['major'] = int(mx.group(1)) |
96 | | - rv['minor'] = int(mx.group(2)) |
97 | | - rv['micro'] = int(mx.group(3) or 0) |
98 | | - rv['prerelease'] = mx.group(4) |
| 94 | + vbase = rv["version"].split(".post")[0] |
| 95 | + mx = re.match(r"^(\d+)\.(\d+)(?:\.(\d+))?((?:[ab]|rc)\d*)?", vbase) |
| 96 | + rv["major"] = int(mx.group(1)) |
| 97 | + rv["minor"] = int(mx.group(2)) |
| 98 | + rv["micro"] = int(mx.group(3) or 0) |
| 99 | + rv["prerelease"] = mx.group(4) |
99 | 100 | return rv |
0 commit comments