Skip to content

Commit 5ec9aba

Browse files
authored
Merge pull request #150 from asherf/format-black
A script to bump versions.
2 parents d399b86 + 940a50e commit 5ec9aba

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

update_version_from_git.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
"""
2+
Adapted from https://github.com/pygame/pygameweb/blob/master/pygameweb/builds/update_version_from_git.py
3+
4+
For updating the version from git.
5+
__init__.py contains a __version__ field.
6+
Update that.
7+
If we are on master, we want to update the version as a pre-release.
8+
git describe --tags
9+
With these:
10+
__init__.py
11+
__version__= '0.0.2'
12+
git describe --tags
13+
0.0.1-22-g729a5ae
14+
We want this:
15+
__init__.py
16+
__version__= '0.0.2.dev22.g729a5ae'
17+
Get the branch/tag name with this.
18+
git symbolic-ref -q --short HEAD || git describe --tags --exact-match
19+
"""
20+
21+
import os
22+
import re
23+
import subprocess
24+
25+
26+
def migrate_source_attribute(attr, to_this, target_file, regex):
27+
"""Updates __magic__ attributes in the source file"""
28+
change_this = re.compile(regex, re.S)
29+
new_file = []
30+
found = False
31+
32+
with open(target_file, "r") as fp:
33+
lines = fp.readlines()
34+
35+
for line in lines:
36+
if line.startswith(attr):
37+
found = True
38+
line = re.sub(change_this, to_this, line)
39+
new_file.append(line)
40+
41+
if found:
42+
with open(target_file, "w") as fp:
43+
fp.writelines(new_file)
44+
45+
46+
def migrate_version(target_file, new_version):
47+
"""Updates __version__ in the source file"""
48+
regex = r"['\"](.*)['\"]"
49+
migrate_source_attribute(
50+
"__version__",
51+
"'{new_version}'".format(new_version=new_version),
52+
target_file,
53+
regex,
54+
)
55+
56+
57+
def is_master_branch():
58+
cmd = "git rev-parse --abbrev-ref HEAD"
59+
tag_branch = subprocess.check_output(cmd, shell=True)
60+
return tag_branch in [b"master\n"]
61+
62+
63+
def git_tag_name():
64+
cmd = "git describe --tags"
65+
tag_branch = subprocess.check_output(cmd, shell=True)
66+
tag_branch = tag_branch.decode().strip()
67+
return tag_branch
68+
69+
70+
def get_git_version_info():
71+
cmd = "git describe --tags"
72+
ver_str = subprocess.check_output(cmd, shell=True)
73+
ver, commits_since, githash = ver_str.decode().strip().split("-")
74+
if ver[0] == "v":
75+
ver = ver[1:]
76+
return ver, commits_since, githash
77+
78+
79+
def prerelease_version():
80+
""" return what the prerelease version should be.
81+
https://packaging.python.org/tutorials/distributing-packages/#pre-release-versioning
82+
0.0.2.dev22
83+
"""
84+
ver, commits_since, githash = get_git_version_info()
85+
initpy_ver = get_version()
86+
87+
assert len(initpy_ver.split(".")) in [
88+
3,
89+
4,
90+
], "django_prometheus/__init__.py version should be like 0.0.2.dev"
91+
assert (
92+
initpy_ver > ver
93+
), "the django_prometheus/__init__.py version should be newer than the last tagged release."
94+
return "{initpy_ver}.{commits_since}".format(
95+
initpy_ver=initpy_ver, commits_since=commits_since
96+
)
97+
98+
99+
def get_version():
100+
version_file = open("django_prometheus/__init__.py", "r").read()
101+
version_match = re.search(
102+
r'^__version__ = [\'"]([^\'"]*)[\'"]', version_file, re.MULTILINE
103+
)
104+
if version_match:
105+
return version_match.group(1)
106+
raise RuntimeError("Unable to find version string.")
107+
108+
109+
def release_version_correct():
110+
"""Makes sure the:
111+
- prerelease verion for master is correct.
112+
- release version is correct for tags.
113+
"""
114+
if is_master_branch():
115+
# update for a pre release version.
116+
initpy = os.path.abspath("django_prometheus/__init__.py")
117+
118+
new_version = prerelease_version()
119+
print(
120+
"updating version in __init__.py to {new_version}".format(
121+
new_version=new_version
122+
)
123+
)
124+
assert (
125+
len(new_version.split(".")) >= 4
126+
), "django_prometheus/__init__.py version should be like 0.0.2.dev"
127+
migrate_version(initpy, new_version)
128+
else:
129+
assert False, "No non-master deployments yet"
130+
# check that we are a tag with the same version as in __init__.py
131+
assert (
132+
get_version() == git_tag_name()
133+
), "git tag/branch name not the same as django_prometheus/__init__.py __verion__"
134+
135+
136+
if __name__ == "__main__":
137+
release_version_correct()

0 commit comments

Comments
 (0)