-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
95 lines (68 loc) · 2.27 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
import datetime
import shutil
from pathlib import Path
from flask_migrate import MigrateCommand
from flask_script import Manager
from CTFd import create_app
from CTFd.utils import get_config as get_config_util
from CTFd.utils import set_config as set_config_util
from CTFd.utils.config import ctf_name
from CTFd.utils.exports import export_ctf as export_ctf_util
from CTFd.utils.exports import import_ctf as import_ctf_util
from CTFd.utils.exports import (
set_import_end_time,
set_import_error,
)
app = create_app()
manager = Manager(app)
manager.add_command("db", MigrateCommand)
def jsenums():
from CTFd.constants import JS_ENUMS
import json
import os
path = os.path.join(app.root_path, "themes/core/assets/js/constants.js")
with open(path, "w+") as f:
for k, v in JS_ENUMS.items():
f.write("const {} = Object.freeze({});".format(k, json.dumps(v)))
BUILD_COMMANDS = {"jsenums": jsenums}
@manager.command
def get_config(key):
with app.app_context():
print(get_config_util(key))
@manager.command
def set_config(key, value):
with app.app_context():
print(set_config_util(key, value).value)
@manager.command
def build(cmd):
with app.app_context():
cmd = BUILD_COMMANDS.get(cmd)
cmd()
@manager.command
def export_ctf(path=None):
with app.app_context():
backup = export_ctf_util()
if path:
with open(path, "wb") as target:
shutil.copyfileobj(backup, target)
else:
name = ctf_name()
day = datetime.datetime.now().strftime("%Y-%m-%d_%T")
full_name = f"{name}.{day}.zip"
with open(full_name, "wb") as target:
shutil.copyfileobj(backup, target)
print(f"Exported {full_name}")
@manager.command
def import_ctf(path, delete_import_on_finish=False):
with app.app_context():
try:
import_ctf_util(path)
except Exception as e:
from CTFd.utils.dates import unix_time
set_import_error(f"Import Failure: " + str(e))
set_import_end_time(value=unix_time(datetime.datetime.utcnow()))
if delete_import_on_finish:
print(f"Deleting {path}")
Path(path).unlink()
if __name__ == "__main__":
manager.run()