forked from Flexget/Flexget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_tools.py
167 lines (143 loc) · 5.4 KB
/
dev_tools.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
import fileinput
import io
import os
import shutil
import subprocess
import zipfile
import click
import requests
def _get_version():
with open('flexget/_version.py') as f:
g = globals()
loc = {}
exec(f.read(), g, loc)
if not loc['__version__']:
raise click.ClickException('Could not find __version__ from flexget/_version.py')
return loc['__version__']
@click.group()
def cli():
pass
@cli.command()
def version():
"""Prints the version number of the source"""
click.echo(_get_version())
@cli.command()
@click.argument('bump_type', type=click.Choice(['dev', 'release']))
def bump_version(bump_type):
"""Bumps version to the next release, or development version."""
cur_ver = _get_version()
click.echo('current version: %s' % cur_ver)
ver_split = cur_ver.split('.')
if 'dev' in ver_split[-1]:
if bump_type == 'dev':
# If this is already a development version, increment the dev count by 1
ver_split[-1] = 'dev%d' % (int(ver_split[-1].strip('dev') or 0) + 1)
else:
# Just strip off dev tag for next release version
ver_split = ver_split[:-1]
else:
# Increment the revision number by one
if len(ver_split) == 2:
# We don't have a revision number, assume 0
ver_split.append('1')
else:
if 'b' in ver_split[2]:
# beta version
minor, beta = ver_split[-1].split('b')
ver_split[-1] = f'{minor}b{int(beta) + 1}'
else:
ver_split[-1] = str(int(ver_split[-1]) + 1)
if bump_type == 'dev':
ver_split.append('dev')
new_version = '.'.join(ver_split)
for line in fileinput.FileInput('flexget/_version.py', inplace=1):
if line.startswith('__version__ ='):
line = "__version__ = '%s'\n" % new_version
print(line, end='')
click.echo('new version: %s' % new_version)
@cli.command()
@click.option("--version", 'ui_version', default='', type=click.Choice(['v2', 'v1', '']))
def bundle_webui(ui_version: str = ""):
"""Bundle webui for release packaging"""
ui_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'flexget', 'ui')
def download_extract(url, dest_path):
print(dest_path)
r = requests.get(url)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall(dest_path)
if ui_version in ['', 'v1']:
# WebUI V1
click.echo('Bundle WebUI v1...')
try:
# Remove existing
app_path = os.path.join(ui_path, 'v1', 'app')
if os.path.exists(app_path):
shutil.rmtree(app_path)
# Just stashed the old webui zip on a random github release for easy hosting.
# It doesn't get updated anymore,
# we should probably stop bundling it with releases soon.
download_extract(
'https://github.com/Flexget/Flexget/releases/download/v3.0.6/webui_v1.zip',
os.path.join(ui_path, 'v1'),
)
except OSError as e:
click.echo('Unable to download and extract WebUI v1 due to %e' % str(e))
raise click.Abort()
if ui_version in ['', 'v2']:
# WebUI V2
try:
click.echo('Bundle WebUI v2...')
# Remove existing
app_path = os.path.join(ui_path, 'v2', 'dist')
if os.path.exists(app_path):
shutil.rmtree(app_path)
release = requests.get(
'https://api.github.com/repos/Flexget/webui/releases/latest'
).json()
v2_package = None
for asset in release['assets']:
if asset['name'] == 'dist.zip':
v2_package = asset['browser_download_url']
break
if not v2_package:
click.echo('Unable to find dist.zip in assets')
raise click.Abort()
download_extract(v2_package, os.path.join(ui_path, 'v2'))
except (OSError, ValueError) as e:
click.echo('Unable to download and extract WebUI v2 due to %s' % str(e))
raise click.Abort()
@cli.command()
@click.argument('files', nargs=-1)
def autoformat(files):
"""Reformat code with black and isort"""
if not files:
project_root = os.path.dirname(os.path.realpath(__file__))
files = (project_root,)
venv_path = os.environ['VIRTUAL_ENV']
if not venv_path:
raise Exception('Virtualenv and activation required')
# black and ruff config are in pyproject.toml
subprocess.call(('ruff', '--fix', *files))
subprocess.call(('black', *files))
@cli.command()
@click.argument('version')
def get_changelog(version):
version = version.lstrip("v")
changelog_lines = []
with requests.get(
"https://raw.githubusercontent.com/Flexget/wiki/main/ChangeLog.md", stream=True
) as resp:
lines = resp.iter_lines(decode_unicode=True)
for line in lines:
if line.startswith(f"## {version}"):
break
else:
click.echo(f"Could not find version {version} in changelog", err=True)
return
for line in lines:
if line.startswith("## ") or line.startswith("<!---"):
break
changelog_lines.append(line)
click.echo("\n".join(changelog_lines).strip())
if __name__ == '__main__':
cli()