-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
setup.py
504 lines (402 loc) · 16.7 KB
/
setup.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env python3
import asyncio
import json
import os
import sys
import tarfile
from copy import deepcopy
from datetime import datetime as dt
from pathlib import Path
import logging
import appdirs
import click
import redbot.logging
from redbot.core.cli import confirm
from redbot.core.data_manager import (
basic_config_default,
load_basic_configuration,
instance_name,
basic_config,
cog_data_path,
core_data_path,
)
from redbot.core.json_io import JsonIO
from redbot.core.utils import safe_delete
from redbot.core import Config
from redbot.core.drivers import BackendType
from redbot.core.drivers.red_json import JSON
conversion_log = logging.getLogger("red.converter")
config_dir = None
appdir = appdirs.AppDirs("Red-DiscordBot")
if sys.platform == "linux":
if 0 < os.getuid() < 1000:
config_dir = Path(appdir.site_data_dir)
if not config_dir:
config_dir = Path(appdir.user_config_dir)
try:
config_dir.mkdir(parents=True, exist_ok=True)
except PermissionError:
print("You don't have permission to write to '{}'\nExiting...".format(config_dir))
sys.exit(1)
config_file = config_dir / "config.json"
def load_existing_config():
if not config_file.exists():
return {}
return JsonIO(config_file)._load_json()
instance_data = load_existing_config()
if instance_data is None:
instance_list = []
else:
instance_list = list(instance_data.keys())
def save_config(name, data, remove=False):
config = load_existing_config()
if remove and name in config:
config.pop(name)
else:
if name in config:
print(
"WARNING: An instance already exists with this name. "
"Continuing will overwrite the existing instance config."
)
if not confirm("Are you absolutely certain you want to continue (y/n)? "):
print("Not continuing")
sys.exit(0)
config[name] = data
JsonIO(config_file)._save_json(config)
def get_data_dir():
default_data_dir = Path(appdir.user_data_dir)
print(
"Hello! Before we begin the full configuration process we need to"
" gather some initial information about where you'd like us"
" to store your bot's data. We've attempted to figure out a"
" sane default data location which is printed below. If you don't"
" want to change this default please press [ENTER], otherwise"
" input your desired data location."
)
print()
print("Default: {}".format(default_data_dir))
new_path = input("> ")
if new_path != "":
new_path = Path(new_path)
default_data_dir = new_path
if not default_data_dir.exists():
try:
default_data_dir.mkdir(parents=True, exist_ok=True)
except OSError:
print(
"We were unable to create your chosen directory."
" You may need to restart this process with admin"
" privileges."
)
sys.exit(1)
print("You have chosen {} to be your data directory.".format(default_data_dir))
if not confirm("Please confirm (y/n):"):
print("Please start the process over.")
sys.exit(0)
return default_data_dir
def get_storage_type():
storage_dict = {1: "JSON", 2: "MongoDB"}
storage = None
while storage is None:
print()
print("Please choose your storage backend (if you're unsure, choose 1).")
print("1. JSON (file storage, requires no database).")
print("2. MongoDB")
storage = input("> ")
try:
storage = int(storage)
except ValueError:
storage = None
else:
if storage not in storage_dict:
storage = None
return storage
def get_name():
name = ""
while len(name) == 0:
print()
print(
"Please enter a name for your instance, this name cannot include spaces"
" and it will be used to run your bot from here on out."
)
name = input("> ")
if " " in name:
name = ""
return name
def basic_setup():
"""
Creates the data storage folder.
:return:
"""
default_data_dir = get_data_dir()
default_dirs = deepcopy(basic_config_default)
default_dirs["DATA_PATH"] = str(default_data_dir.resolve())
storage = get_storage_type()
storage_dict = {1: "JSON", 2: "MongoDB"}
default_dirs["STORAGE_TYPE"] = storage_dict.get(storage, 1)
if storage_dict.get(storage, 1) == "MongoDB":
from redbot.core.drivers.red_mongo import get_config_details
default_dirs["STORAGE_DETAILS"] = get_config_details()
else:
default_dirs["STORAGE_DETAILS"] = {}
name = get_name()
save_config(name, default_dirs)
print()
print(
"Your basic configuration has been saved. Please run `redbot <name>` to"
" continue your setup process and to run the bot."
)
def get_current_backend(instance) -> BackendType:
return BackendType(instance_data[instance]["STORAGE_TYPE"])
def get_target_backend(backend) -> BackendType:
if backend == "json":
return BackendType.JSON
elif backend == "mongo":
return BackendType.MONGO
async def json_to_mongov2(instance):
instance_vals = instance_data[instance]
current_data_dir = Path(instance_vals["DATA_PATH"])
load_basic_configuration(instance)
from redbot.core.drivers import red_mongo
storage_details = red_mongo.get_config_details()
core_conf = Config.get_core_conf()
new_driver = red_mongo.Mongo(cog_name="Core", identifier="0", **storage_details)
core_conf.init_custom("CUSTOM_GROUPS", 2)
custom_group_data = await core_conf.custom("CUSTOM_GROUPS").all()
curr_custom_data = custom_group_data.get("Core", {}).get("0", {})
exported_data = await core_conf.driver.export_data(curr_custom_data)
conversion_log.info("Starting Core conversion...")
await new_driver.import_data(exported_data, curr_custom_data)
conversion_log.info("Core conversion complete.")
for p in current_data_dir.glob("cogs/**/settings.json"):
cog_name = p.parent.stem
with p.open(mode="r") as f:
cog_data = json.load(f)
for identifier, all_data in cog_data.items():
conf = Config.get_conf(None, int(identifier), cog_name=cog_name)
new_driver = red_mongo.Mongo(
cog_name=cog_name,
identifier=conf.driver.unique_cog_identifier,
**storage_details
)
curr_custom_data = custom_group_data.get(cog_name, {}).get(identifier, {})
exported_data = await conf.driver.export_data(curr_custom_data)
conversion_log.info(f"Converting {cog_name} with identifier {identifier}...")
await new_driver.import_data(exported_data, curr_custom_data)
conversion_log.info("Cog conversion complete.")
return storage_details
async def mongov2_to_json(instance):
instance_vals = instance_data[instance]
load_basic_configuration(instance)
core_path = core_data_path()
from redbot.core.drivers import red_json
core_conf = Config.get_core_conf()
new_driver = red_json.JSON(cog_name="Core", identifier="0", data_path_override=core_path)
core_conf.init_custom("CUSTOM_GROUPS", 2)
custom_group_data = await core_conf.custom("CUSTOM_GROUPS").all()
curr_custom_data = custom_group_data.get("Core", {}).get("0", {})
exported_data = await core_conf.driver.export_data(curr_custom_data)
conversion_log.info("Starting Core conversion...")
await new_driver.import_data(exported_data, curr_custom_data)
conversion_log.info("Core conversion complete.")
collection_names = await core_conf.driver.db.list_collection_names()
splitted_names = list(filter(
lambda elem: elem[1] != '' and elem[0] != "Core",
[n.split('.') for n in collection_names]
))
ident_map = {} # Cogname: idents list
for cog_name, category in splitted_names:
if cog_name not in ident_map:
ident_map[cog_name] = set()
idents = await core_conf.driver.db[cog_name][category].distinct("_id.RED_uuid")
ident_map[cog_name].update(set(idents))
for cog_name, idents in ident_map.items():
for identifier in idents:
curr_custom_data = custom_group_data.get(cog_name, {}).get(identifier, {})
conf = Config.get_conf(None, int(identifier), cog_name=cog_name)
exported_data = await conf.driver.export_data(curr_custom_data)
new_path = cog_data_path(raw_name=cog_name)
new_driver = red_json.JSON(cog_name, identifier, data_path_override=new_path)
conversion_log.info(f"Converting {cog_name} with identifier {identifier}...")
await new_driver.import_data(exported_data, curr_custom_data)
# cog_data_path(raw_name=cog_name)
conversion_log.info("Cog conversion complete.")
return {}
async def mongo_to_json(current_data_dir: Path, storage_details: dict):
from redbot.core.drivers.red_mongo import Mongo
m = Mongo("Core", "0", **storage_details)
db = m.db
collection_names = await db.list_collection_names()
for collection_name in collection_names:
if collection_name == "Core":
c_data_path = current_data_dir / "core"
else:
c_data_path = current_data_dir / "cogs" / collection_name
c_data_path.mkdir(parents=True, exist_ok=True)
# Every cog name has its own collection
collection = db[collection_name]
async for document in collection.find():
# Every cog has its own document.
# This means if two cogs have the same name but different identifiers, they will
# be two separate documents in the same collection
cog_id = document.pop("_id")
driver = JSON(collection_name, cog_id, data_path_override=c_data_path)
for key, value in document.items():
await driver.set(key, value=value)
async def edit_instance():
instance_list = load_existing_config()
if not instance_list:
print("No instances have been set up!")
return
print(
"You have chosen to edit an instance. The following "
"is a list of instances that currently exist:\n"
)
for instance in instance_list.keys():
print("{}\n".format(instance))
print("Please select one of the above by entering its name")
selected = input("> ")
if selected not in instance_list.keys():
print("That isn't a valid instance!")
return
instance_data = instance_list[selected]
default_dirs = deepcopy(basic_config_default)
current_data_dir = Path(instance_data["DATA_PATH"])
print("You have selected '{}' as the instance to modify.".format(selected))
if not confirm("Please confirm (y/n):"):
print("Ok, we will not continue then.")
return
print("Ok, we will continue on.")
print()
if confirm("Would you like to change the instance name? (y/n)"):
name = get_name()
else:
name = selected
if confirm("Would you like to change the data location? (y/n)"):
default_data_dir = get_data_dir()
default_dirs["DATA_PATH"] = str(default_data_dir.resolve())
else:
default_dirs["DATA_PATH"] = str(current_data_dir.resolve())
if name != selected:
save_config(selected, {}, remove=True)
save_config(name, default_dirs)
print("Your basic configuration has been edited")
async def create_backup(instance):
instance_vals = instance_data[instance]
if confirm("Would you like to make a backup of the data for this instance? (y/n)"):
load_basic_configuration(instance)
if instance_vals["STORAGE_TYPE"] == "MongoDB":
await mongo_to_json(instance_vals["DATA_PATH"], instance_vals["STORAGE_DETAILS"])
print("Backing up the instance's data...")
backup_filename = "redv3-{}-{}.tar.gz".format(
instance, dt.utcnow().strftime("%Y-%m-%d %H-%M-%S")
)
pth = Path(instance_vals["DATA_PATH"])
if pth.exists():
backup_pth = pth.home()
backup_file = backup_pth / backup_filename
to_backup = []
exclusions = [
"__pycache__",
"Lavalink.jar",
os.path.join("Downloader", "lib"),
os.path.join("CogManager", "cogs"),
os.path.join("RepoManager", "repos"),
]
from redbot.cogs.downloader.repo_manager import RepoManager
repo_mgr = RepoManager()
repo_output = []
for _, repo in repo_mgr._repos:
repo_output.append({"url": repo.url, "name": repo.name, "branch": repo.branch})
repo_filename = pth / "cogs" / "RepoManager" / "repos.json"
with open(str(repo_filename), "w") as f:
f.write(json.dumps(repo_output, indent=4))
instance_vals = {instance_name: basic_config}
instance_file = pth / "instance.json"
with open(str(instance_file), "w") as instance_out:
instance_out.write(json.dumps(instance_vals, indent=4))
for f in pth.glob("**/*"):
if not any(ex in str(f) for ex in exclusions):
to_backup.append(f)
with tarfile.open(str(backup_file), "w:gz") as tar:
for f in to_backup:
tar.add(str(f), recursive=False)
print("A backup of {} has been made. It is at {}".format(instance, backup_file))
async def remove_instance(instance):
await create_backup(instance)
instance_vals = instance_data[instance]
if instance_vals["STORAGE_TYPE"] == "MongoDB":
from redbot.core.drivers.red_mongo import Mongo
m = Mongo("Core", **instance_vals["STORAGE_DETAILS"])
db = m.db
collections = await db.collection_names(include_system_collections=False)
for name in collections:
collection = await db.get_collection(name)
await collection.drop()
else:
pth = Path(instance_data["DATA_PATH"])
safe_delete(pth)
save_config(instance, {}, remove=True)
print("The instance {} has been removed\n".format(instance))
async def remove_instance_interaction():
if not instance_list:
print("No instances have been set up!")
return
print(
"You have chosen to remove an instance. The following "
"is a list of instances that currently exist:\n"
)
for instance in instance_data.keys():
print("{}\n".format(instance))
print("Please select one of the above by entering its name")
selected = input("> ")
if selected not in instance_data.keys():
print("That isn't a valid instance!")
return
await create_backup(selected)
await remove_instance(selected)
@click.group(invoke_without_command=True)
@click.option("--debug", type=bool)
@click.pass_context
def cli(ctx, debug):
level = logging.DEBUG if debug else logging.INFO
redbot.logging.init_logging(
level=level, location=Path.cwd() / "red_setup_logs"
)
if ctx.invoked_subcommand is None:
basic_setup()
@cli.command()
@click.argument("instance", type=click.Choice(instance_list))
def delete(instance):
loop = asyncio.get_event_loop()
loop.run_until_complete(remove_instance(instance))
@cli.command()
@click.argument("instance", type=click.Choice(instance_list))
@click.argument("backend", type=click.Choice(["json", "mongo"]))
def convert(instance, backend):
current_backend = get_current_backend(instance)
target = get_target_backend(backend)
default_dirs = deepcopy(basic_config_default)
default_dirs["DATA_PATH"] = str(Path(instance_data[instance]["DATA_PATH"]))
loop = asyncio.get_event_loop()
if current_backend == BackendType.MONGOV1 and target == BackendType.MONGO:
raise RuntimeError("Please see conversion docs for updating to the latest mongo version.")
elif current_backend == BackendType.JSON:
if target == BackendType.MONGO:
storage_details = loop.run_until_complete(json_to_mongov2(instance))
default_dirs["STORAGE_TYPE"] = BackendType.MONGO.value
default_dirs["STORAGE_DETAILS"] = storage_details
save_config(instance, default_dirs)
elif current_backend == BackendType.MONGO:
if target == BackendType.JSON:
storage_details = loop.run_until_complete(mongov2_to_json(instance))
default_dirs["STORAGE_TYPE"] = BackendType.JSON.value
default_dirs["STORAGE_DETAILS"] = storage_details
save_config(instance, default_dirs)
if __name__ == "__main__":
try:
cli()
except KeyboardInterrupt:
print("Exiting...")
else:
print("Exiting...")