forked from GjjvdBurg/signal2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_release.py
262 lines (194 loc) · 6.55 KB
/
make_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Do-nothing script for making a release
This idea comes from here:
https://blog.danslimmon.com/2019/07/15/do-nothing-scripting-the-key-to-gradual-automation/
Author: Gertjan van den Burg
Date: 2019-07-23
"""
import os
import sys
import tempfile
import colorama
def colored(msg, color=None, style=None):
colors = {
"red": colorama.Fore.RED,
"green": colorama.Fore.GREEN,
"cyan": colorama.Fore.CYAN,
"yellow": colorama.Fore.YELLOW,
"magenta": colorama.Fore.MAGENTA,
None: "",
}
styles = {
"bright": colorama.Style.BRIGHT,
"dim": colorama.Style.DIM,
None: "",
}
pre = colors[color] + styles[style]
post = colorama.Style.RESET_ALL
return f"{pre}{msg}{post}"
def cprint(msg, color=None, style=None):
print(colored(msg, color=color, style=style))
def wait_for_enter():
input(colored("\nPress Enter to continue", style="dim"))
print()
def get_package_name():
with open("./setup.py", "r") as fp:
nameline = next(
(l.strip() for l in fp if l.startswith("NAME = ")), None
)
return nameline.split("=")[-1].strip().strip('"')
def get_package_version(pkgname):
ctx = {}
with open(f"{pkgname.lower()}/__version__.py", "r") as fp:
exec(fp.read(), ctx)
return ctx["__version__"]
class Step:
def pre(self, context):
pass
def post(self, context):
wait_for_enter()
def run(self, context):
try:
self.pre(context)
self.action(context)
self.post(context)
except KeyboardInterrupt:
cprint("\nInterrupted.", color="red")
raise SystemExit(1)
def instruct(self, msg):
cprint(msg, color="green")
def print_run(self, msg):
cprint("Run:", color="cyan", style="bright")
self.print_cmd(msg)
def print_cmd(self, msg):
cprint("\t" + msg, color="cyan", style="bright")
def do_cmd(self, cmd):
cprint(f"Going to run: {cmd}", color="magenta", style="bright")
wait_for_enter()
os.system(cmd)
class GitToMaster(Step):
def action(self, context):
self.instruct("Make sure you're on master and changes are merged in")
self.print_run("git checkout master")
class UpdateChangelog(Step):
def action(self, context):
self.instruct(f"Update change log for version {context['version']}")
self.print_run("vi CHANGELOG.md")
class UpdateReadme(Step):
def action(self, context):
self.instruct(f"Update readme if necessary")
self.print_run("vi README.md")
class RunTests(Step):
def action(self, context):
self.instruct("Run the unit tests")
self.print_run("make test")
class BumpVersionPackage(Step):
def action(self, context):
self.instruct(f"Update __version__.py with new version")
self.do_cmd(f"vi {context['pkgname']}/__version__.py")
def post(self, context):
wait_for_enter()
context["version"] = self._get_version(context)
def _get_version(self, context):
# Get the version from the version file
return get_package_version(context["pkgname"])
class MakeClean(Step):
def action(self, context):
self.do_cmd("make clean")
class MakeDocs(Step):
def action(self, context):
self.do_cmd("make docs")
class MakeDist(Step):
def action(self, context):
self.do_cmd("make dist")
class PushToTestPyPI(Step):
def action(self, context):
self.do_cmd(
"twine upload --repository-url https://test.pypi.org/legacy/ dist/*"
)
class InstallFromTestPyPI(Step):
def action(self, context):
tmpvenv = tempfile.mkdtemp(prefix="s2h_venv_")
self.do_cmd(
f"python -m venv {tmpvenv} && source {tmpvenv}/bin/activate && "
"pip install --no-cache-dir --index-url "
"https://test.pypi.org/simple/ "
"--extra-index-url https://pypi.org/simple "
f"{context['pkgname']}=={context['version']}"
)
context["tmpvenv"] = tmpvenv
class TestPackage(Step):
def action(self, context):
self.instruct(
f"Ensure that the following command gives version {context['version']}"
)
self.do_cmd(
f"source {context['tmpvenv']}/bin/activate && signal2html -V"
)
class RemoveVenv(Step):
def action(self, context):
self.do_cmd(f"rm -rf {context['tmpvenv']}")
class GitTagVersion(Step):
def action(self, context):
self.do_cmd(f"git tag v{context['version']}")
class GitAdd(Step):
def action(self, context):
self.instruct("Add everything to git and commit")
self.print_run("git gui")
class PushToPyPI(Step):
def action(self, context):
self.do_cmd("twine upload dist/*")
class PushToGitHub(Step):
def action(self, context):
self.do_cmd("git push -u --tags origin master")
class WaitForTravis(Step):
def action(self, context):
self.instruct(
"Wait for Travis to complete and verify that its successful"
)
class WaitForAppVeyor(Step):
def action(self, context):
self.instruct(
"Wait for AppVeyor to complete and verify that its successful"
)
class WaitForRTD(Step):
def action(self, context):
self.instruct(
"Wait for ReadTheDocs to complete and verify that its successful"
)
def main(target=None):
colorama.init()
procedure = [
("gittomaster", GitToMaster()),
("gitadd1", GitAdd()),
("push1", PushToGitHub()),
("bumpversion", BumpVersionPackage()),
("changelog", UpdateChangelog()),
("readme", UpdateReadme()),
("clean", MakeClean()),
# ("tests", RunTests()),
("dist", MakeDist()),
("testpypi", PushToTestPyPI()),
("install", InstallFromTestPyPI()),
("testpkg", TestPackage()),
("remove_venv", RemoveVenv()),
("gitadd2", GitAdd()),
("pypi", PushToPyPI()),
("tag", GitTagVersion()),
("push2", PushToGitHub()),
]
context = {}
context["pkgname"] = get_package_name()
context["version"] = get_package_version(context["pkgname"])
skip = True if target else False
for name, step in procedure:
if not name == target and skip:
continue
skip = False
step.run(context)
cprint("\nDone!", color="yellow", style="bright")
if __name__ == "__main__":
target = sys.argv[1] if len(sys.argv) > 1 else None
main(target=target)