-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrelease.py
164 lines (131 loc) · 4.15 KB
/
release.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
from __future__ import annotations
import re
import shutil
import sys
from pathlib import Path
import invoke
from parver import Version
TASK_NAME = "RELEASE"
def drop_dir(path):
shutil.rmtree(str(path))
def _get_git_root(ctx):
return Path(ctx.run("git rev-parse --show-toplevel", hide=True).stdout.strip())
def find_version(version_path):
version_file = version_path.read_text()
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
def get_version_file(ctx):
version_path = _get_git_root(ctx) / "src" / "pythonfinder" / "__init__.py"
return version_path
def get_version(ctx):
version = find_version(get_version_file(ctx))
return version
def log(msg):
print("[release] %s" % msg)
def get_dist_dir(ctx):
return _get_git_root(ctx) / "dist"
def get_build_dir(ctx):
return _get_git_root(ctx) / "build"
def drop_dist_dirs(ctx):
log("Dropping Dist dir...")
drop_dir(get_dist_dir(ctx))
log("Dropping build dir...")
drop_dir(get_build_dir(ctx))
@invoke.task
def build_dists(ctx, drop_existing=True):
if drop_existing:
drop_dist_dirs(ctx)
log("Building sdist using %s ...." % sys.executable)
ctx.run("%s -m build --sdist" % sys.executable)
log("Building wheel using %s ...." % sys.executable)
ctx.run("%s -m build --wheel" % sys.executable)
@invoke.task(build_dists)
def upload_dists(ctx, build=False):
if build:
build_dists(ctx)
log("Uploading distributions to pypi...")
ctx.run("twine upload dist/*")
@invoke.task
def generate_changelog(ctx, commit=False, draft=False):
log("Generating changelog...")
args = []
if draft:
args.append("--draft")
ctx.run("towncrier {}".format(" ".join(args)))
if commit:
log("Committing...")
ctx.run("git add .")
ctx.run('git commit -m "Update changelog."')
@invoke.task
def tag_version(ctx, push=False):
version = get_version(ctx)
log("Tagging revision: v%s" % version)
ctx.run("git tag v%s" % version)
if push:
log("Pushing tags...")
ctx.run("git push --tags")
@invoke.task
def bump_version(
ctx,
dry_run=False,
major=False,
minor=False,
micro=True,
dev=False,
pre=False,
tag=None,
clear=False,
commit=False,
):
_current_version = get_version(ctx)
current_version = Version.parse(_current_version)
if pre and not tag:
print('Using "pre" requires a corresponding tag.')
return
if not dev and not pre:
new_version = current_version.clear(pre=True, dev=True)
if pre and dev:
print("Pre and dev cannot be used together.")
return
elif dev:
new_version = new_version.bump_dev()
elif pre:
new_version = new_version.bump_pre(tag=tag)
if major:
new_version = new_version.bump_release(0)
elif minor:
new_version = new_version.bump_release(1)
elif micro:
new_version = new_version.bump_release(2)
if clear:
new_version = new_version.clear(dev=True, pre=True, post=True)
log("Updating version to %s" % new_version.normalize())
version_file = get_version_file(ctx)
file_contents = version_file.read_text()
log("Found current version: %s" % _current_version)
if dry_run:
log("Would update to: %s" % new_version.normalize())
else:
log("Updating to: %s" % new_version.normalize())
version_file.write_text(
file_contents.replace(_current_version, str(new_version.normalize()))
)
if commit:
ctx.run(f"git add {get_version_file(ctx)}")
log("Committing...")
ctx.run('git commit -s -m "Bumped version."')
@invoke.task
def clean_mdchangelog(ctx):
root = _get_git_root(ctx)
changelog = root / "CHANGELOG.md"
content = changelog.read_text()
content = re.sub(
r"([^\n]+)\n?\s+\[[\\]+(#\d+)\]\(https://github\.com/sarugaku/[\w\-]+/issues/\d+\)",
r"\1 \2",
content,
flags=re.MULTILINE,
)
changelog.write_text(content)