Skip to content

Commit 914f30e

Browse files
committed
Add tag_release.py script.
1 parent 7e580ae commit 914f30e

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

scripts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory contains scripts which help with the development of python-tcod.

scripts/tag_release.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
4+
import argparse
5+
import datetime
6+
import re
7+
import subprocess
8+
from typing import Any, Tuple
9+
10+
parser = argparse.ArgumentParser(
11+
description="Tags and releases the next version of this project."
12+
)
13+
14+
parser.add_argument("tag", help="Semantic version number to use as the tag.")
15+
16+
parser.add_argument(
17+
"-e", "--edit", action="store_true", help="Force edits of git commits."
18+
)
19+
20+
parser.add_argument(
21+
"-n", "--dry-run", action="store_true", help="Don't modify files."
22+
)
23+
24+
parser.add_argument(
25+
"-v", "--verbose", action="store_true", help="Print debug information."
26+
)
27+
28+
29+
def parse_changelog(args: Any) -> Tuple[str, str]:
30+
"""Return an updated changelog and and the list of changes."""
31+
with open("CHANGELOG.rst", "r") as file:
32+
match = re.match(
33+
pattern=r"(.*?Unreleased\n---+\n)(.+?)(\n*[^\n]+\n---+\n.*)",
34+
string=file.read(),
35+
flags=re.DOTALL,
36+
)
37+
assert match
38+
header, changes, tail = match.groups()
39+
tag = "%s - %s" % (args.tag, datetime.date.today().isoformat())
40+
41+
tagged = "\n%s\n%s\n%s" % (tag, "-" * len(tag), changes)
42+
if args.verbose:
43+
print(tagged)
44+
45+
return "".join((header, tagged, tail)), changes
46+
47+
48+
def main() -> None:
49+
if len(sys.argv) == 1:
50+
parser.print_help(sys.stderr)
51+
sys.exit(1)
52+
53+
args = parser.parse_args()
54+
if args.verbose:
55+
print(args)
56+
57+
new_changelog, changes = parse_changelog(args)
58+
59+
if not args.dry_run:
60+
with open("CHANGELOG.rst", "w") as f:
61+
f.write(new_changelog)
62+
edit = ["-e"] if args.edit else []
63+
subprocess.check_call(
64+
["git", "commit", "-avm", "Prepare %s release." % args.tag] + edit
65+
)
66+
subprocess.check_call(
67+
["git", "tag", args.tag, "-am", "%s\n\n%s" % (args.tag, changes)]
68+
+ edit
69+
)
70+
71+
72+
if __name__ == "__main__":
73+
main()

0 commit comments

Comments
 (0)