forked from SynoCommunity/spkrepo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
275 lines (242 loc) · 8.24 KB
/
manage.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
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*-
import datetime
import json
import os
import re
import shutil
from flask import current_app
from flask_migrate import MigrateCommand
from flask_script import Manager, Option
from flask_security.script import (
ActivateUserCommand,
AddRoleCommand,
CreateRoleCommand,
CreateUserCommand,
DeactivateUserCommand,
RemoveRoleCommand,
commit,
)
from flask_security.utils import encrypt_password
from spkrepo import create_app
from spkrepo.ext import db
from spkrepo.models import Architecture, Package, user_datastore
from spkrepo.utils import populate_db
manager = Manager(create_app)
manager.add_option("-c", "--config", dest="config", required=False)
# Migrate commands
manager.add_command("db", MigrateCommand)
def date_handler(obj):
return obj.isoformat() if hasattr(obj, "isoformat") else obj
def pprint(obj):
print(json.dumps(obj, default=date_handler, sort_keys=True, indent=4))
# User commands
class CreateSpkrepoUserCommand(CreateUserCommand):
"""Create a user"""
option_list = (
Option("-u", "--username", dest="username", default=None),
Option("-e", "--email", dest="email", default=None),
Option("-p", "--password", dest="password", default=None),
Option("-a", "--active", dest="active", default=""),
Option("-c", "--confirmed", dest="confirmed", default=""),
)
@commit
def run(self, **kwargs):
# handle confirmed
if re.sub(r"\s", "", str(kwargs.pop("confirmed"))).lower() in [
"",
"y",
"yes",
"1",
"active",
]:
kwargs["confirmed_at"] = datetime.datetime.now()
# sanitize active input
ai = re.sub(r"\s", "", str(kwargs["active"]))
kwargs["active"] = ai.lower() in ["", "y", "yes", "1", "active"]
from flask_security.forms import ConfirmRegisterForm
from werkzeug.datastructures import MultiDict
form = ConfirmRegisterForm(MultiDict(kwargs), csrf_enabled=False)
if form.validate():
kwargs["password"] = encrypt_password(kwargs["password"])
user_datastore.create_user(**kwargs)
print("User created successfully.")
kwargs["password"] = "****"
pprint(kwargs)
else:
print("Error creating user")
pprint(form.errors)
UserCommand = Manager(usage="Perform user actions")
UserCommand.add_command("create", CreateSpkrepoUserCommand)
UserCommand.add_command("activate", ActivateUserCommand)
UserCommand.add_command("deactivate", DeactivateUserCommand)
UserCommand.add_command("create_role", CreateRoleCommand)
UserCommand.add_command("remove_role", RemoveRoleCommand)
UserCommand.add_command("add_role", AddRoleCommand)
manager.add_command("user", UserCommand)
@MigrateCommand.command
def drop():
"""Drop database"""
db.drop_all()
@MigrateCommand.command
def create():
"""Create Database"""
db.create_all()
populate_db()
db.session.commit()
@MigrateCommand.command
def populate():
"""Populate the database with some packages"""
from spkrepo.tests.common import BuildFactory, PackageFactory, VersionFactory
with db.session.no_autoflush:
# nzbget
nzbget_package = PackageFactory(name="nzbget")
nzbget_versions = [
VersionFactory(
package=nzbget_package,
upstream_version="12.0",
version=10,
dependencies=None,
report_url=None,
install_wizard=True,
upgrade_wizard=False,
),
VersionFactory(
package=nzbget_package,
upstream_version="13.0",
version=11,
dependencies=None,
report_url=None,
install_wizard=True,
upgrade_wizard=False,
),
]
nzbget_builds = []
for version in nzbget_versions:
builds = BuildFactory.create_batch(2, version=version, active=True)
nzbget_builds.extend(builds)
# sickbeard
sickbeard_package = PackageFactory(name="sickbeard")
sickbeard_versions = [
VersionFactory(
package=sickbeard_package,
upstream_version="20140528",
version=3,
dependencies="git",
service_dependencies=[],
report_url=None,
install_wizard=False,
upgrade_wizard=False,
startable=True,
),
VersionFactory(
package=sickbeard_package,
upstream_version="20140702",
version=4,
dependencies="git",
service_dependencies=[],
report_url=None,
install_wizard=False,
upgrade_wizard=False,
startable=True,
),
]
sickbeard_builds = []
for version in sickbeard_versions:
sickbeard_builds.append(
BuildFactory(
version=version,
architectures=[Architecture.find("noarch")],
active=True,
)
)
# git
git_package = PackageFactory(name="git")
git_versions = [
VersionFactory(
package=git_package,
upstream_version="1.8.4",
version=3,
dependencies=None,
service_dependencies=[],
report_url=None,
install_wizard=False,
upgrade_wizard=False,
startable=False,
),
VersionFactory(
package=git_package,
upstream_version="2.1.2",
version=4,
dependencies=None,
service_dependencies=[],
report_url=None,
install_wizard=False,
upgrade_wizard=False,
startable=False,
),
]
git_builds = []
for version in git_versions:
builds = BuildFactory.create_batch(3, version=version, active=True)
git_builds.extend(builds)
# bitlbee
bitlbee_package = PackageFactory(name="bitlbee")
bitlbee_versions = [
VersionFactory(
package=bitlbee_package,
upstream_version="3.2.2",
version=9,
dependencies=None,
service_dependencies=[],
report_url=None,
install_wizard=False,
upgrade_wizard=False,
startable=True,
),
VersionFactory(
package=bitlbee_package,
upstream_version="3.2.3",
version=10,
dependencies=None,
service_dependencies=[],
report_url=None,
install_wizard=False,
upgrade_wizard=False,
startable=True,
),
VersionFactory(
package=bitlbee_package,
upstream_version="3.3.0",
version=11,
dependencies=None,
service_dependencies=[],
install_wizard=False,
upgrade_wizard=False,
startable=True,
),
]
bitlbee_builds = []
for version in bitlbee_versions:
builds = BuildFactory.create_batch(3, version=version, active=True)
bitlbee_builds.extend(builds)
db.session.commit()
@MigrateCommand.command
def depopulate():
"""Depopulate database"""
for package in Package.query.all():
shutil.rmtree(os.path.join(current_app.config["DATA_PATH"], package.name))
db.session.delete(package)
db.session.commit()
@manager.command
def clean():
"""Clean data path"""
# do not remove and recreate the path since it may be a docker volume
for root, dirs, files in os.walk(
os.path.join(current_app.config["DATA_PATH"]), topdown=False
):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
if __name__ == "__main__":
manager.run()