forked from KeyWeeUsr/Bear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.py
234 lines (187 loc) · 5.92 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
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
"""
Release handling for Bear.
"""
import sys
import json
from subprocess import Popen
from os import environ
from os.path import join, dirname, abspath, exists
from shutil import rmtree
from platform import platform
from urllib.parse import quote
from bear import NAME, VERSION
ROOT = dirname(abspath(__file__))
PKG = join(ROOT, NAME)
RELEASE_DIR = join(ROOT, 'release')
PYPI_REPO = environ.get('PYPI_REPO', 'https://upload.pypi.org/legacy/')
GITHUB_API = 'https://api.github.com'
GITHUB_REPO = 'KeyWeeUsr/Bear'
EXTENSIONS = {
# meh... GitHub won't handle same-named binaries
# therefore MacOS vs GNU/Linux name collision
'linux': 'linux',
'darwin': 'macos',
'win32': 'exe'
}
def run_proc(args, options=None):
"""
Run a command outside of the interpreter,
but block until it's resolved.
"""
proc = Popen(args, **({} if not options else options))
proc.communicate()
return proc.returncode == 0
def package_check():
"""Run style checkers and tests."""
return run_proc(['python', 'check.py'])
def package_clean():
"""Clean cache folders."""
run_proc(['python', '-m', 'pip', 'uninstall', '-y', NAME])
folders = [
'dist', 'build', f'{NAME}.egg-info'
]
for folder in folders:
if not exists(join(ROOT, folder)):
continue
rmtree(join(ROOT, folder))
return True
def package_install():
"""Install deps via installing the packages."""
return run_proc([
'python', '-m', 'pip', 'install', '--upgrade',
'--editable', '.[release]'
])
def package_dist():
"""Create Python package distributions: source + wheel."""
return run_proc(['python', 'setup.py', 'sdist', 'bdist_wheel'])
def package_upload():
"""Upload created distributions to PyPI."""
return run_proc([
'python', '-m', 'twine', 'upload',
'--repository-url', PYPI_REPO, 'dist/*'
])
def _create_executable():
return run_proc([
'python', '-m', 'PyInstaller', '--name', NAME, '--clean',
'--onefile', '--console', 'bear/__main__.py'
])
def create_executable_linux():
"""Create a binary for GNU/Linux platform."""
result = True
if sys.platform == 'linux':
result = _create_executable()
return result
def create_executable_macos():
"""Create a binary for MacOS platform."""
result = True
if sys.platform == 'darwin':
result = _create_executable()
return result
def create_executable_windows():
"""Create a binary for MS Windows platform."""
result = True
if sys.platform == 'win32':
result = _create_executable()
return result
def create_github_release():
"""Draft a new release for a tag (application version) on GitHub."""
import requests
result = False
resp = requests.post(
f'{GITHUB_API}/repos/{GITHUB_REPO}/releases', data=json.dumps({
'tag_name': VERSION,
'name': VERSION,
'draft': False
}), headers={
'Authorization': f'token {environ.get("GITHUB_TOKEN")}',
'Content-Type': 'application/json'
}
)
if resp.status_code == 201:
result = True
elif resp.status_code == 422:
resp = requests.get(
f'{GITHUB_API}/repos/{GITHUB_REPO}/releases/tags/{VERSION}',
headers={
'Authorization': f'token {environ.get("GITHUB_TOKEN")}'
}
)
# confirmed that release available on GitHub
result = resp.status_code == 200
else:
raise Exception((resp, resp.headers, resp.json()))
return result
def _upload_executable(binary_name, upload_name):
"""Upload GNU/Linux executable to GitHub release page."""
import requests
result = False
resp = requests.get(
f'{GITHUB_API}/repos/{GITHUB_REPO}/releases/tags/{VERSION}',
headers={
'Authorization': f'token {environ.get("GITHUB_TOKEN")}'
}
)
with open(join(ROOT, 'dist', binary_name), 'rb') as file:
content = file.read()
asset_props = f'?name={upload_name}&label={quote(platform())}'
resp = requests.post(
resp.json()['upload_url'].replace('{?name,label}', asset_props),
data=content, headers={
'Authorization': f'token {environ.get("GITHUB_TOKEN")}',
'Content-Type': 'application/octet-stream'
}
)
if resp.status_code in (201, 422):
result = True
else:
raise Exception((resp, resp.headers, resp.json()))
return result
def upload_executable_linux():
"""Upload GNU/Linux executable to GitHub release page."""
result = True
if sys.platform == 'linux':
result = _upload_executable(NAME, f'{NAME}.{EXTENSIONS[sys.platform]}')
return result
def upload_executable_macos():
"""Upload GNU/Linux executable to GitHub release page."""
result = True
if sys.platform == 'darwin':
result = _upload_executable(NAME, f'{NAME}.{EXTENSIONS[sys.platform]}')
return result
def upload_executable_windows():
"""Upload MS Windows executable to GitHub release page."""
result = True
if sys.platform == 'win32':
result = _upload_executable(
f'{NAME}.exe', f'{NAME}.{EXTENSIONS[sys.platform]}'
)
return result
CASES = [
package_clean,
package_install,
package_check,
package_dist,
# package_upload,
package_clean,
package_install,
create_github_release,
create_executable_linux,
upload_executable_linux,
create_executable_macos,
upload_executable_macos,
create_executable_windows,
upload_executable_windows
]
def main():
"""Main function for release."""
cases_len = len(CASES)
for i, case in enumerate(CASES):
current = str(i + 1).zfill(len(str(cases_len)))
print(f'<{current}/{cases_len}> ', end='')
if case():
print('Success!')
else:
print('Fail!')
exit(1)
if __name__ == '__main__':
main()