-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare
executable file
·82 lines (62 loc) · 2.06 KB
/
prepare
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import re
import datetime
import git
from libff.__version__ import __version__
def get_version(commit):
"""Extract the version number from this commit's __version__.py.
"""
for line in get_lines(commit, "libff/__version__.py"):
match = re.match(r"^__version__ = (\d+)$", line)
if match:
return int(match.group(1))
else:
raise ValueError("unable to find __version__")
def find_commits(repo, current_version):
"""Find all recent commits that were made after the version number was
changed, i.e. after the last release.
"""
commits = []
commit = repo.head.commit
while True:
version = get_version(commit.parents[0])
if version < current_version:
return commits
commits.append(commit)
commit = commit.parents[0]
def get_lines(commit, filename):
"""Extract the lines of a file from a specific commit.
"""
try:
blob = commit.tree / filename
except KeyError:
return []
else:
text = blob.data_stream.read().decode("utf-8")
return text.splitlines()
repo = git.Repo()
if repo.is_dirty():
raise SystemExit("ERROR: there are uncommitted changes!")
current_version = __version__
lines = []
lines.append(f"### Version {current_version + 1} - ({datetime.date.today()})")
lines.append("")
for commit in reversed(find_commits(repo, current_version)):
lines.append(f"- {commit.message.splitlines()[0]}")
lines.append("")
lines += get_lines(repo.head.commit, "CHANGES.md")
with open("CHANGES.md", "w") as fobj:
for line in lines:
print(line, file=fobj)
with open("libff/__version__.py", "w") as fobj:
for line in get_lines(repo.head.commit, "libff/__version__.py"):
if re.match(r"^__version__ = (\d+)$", line):
print(f"__version__ = {current_version + 1}", file=fobj)
break
else:
print(line, file=fobj)
else:
raise ValueError("unable to find __version__")
print()
print("Please edit CHANGES.md now and then ./publish")
print()