From 66ff9c0b91a92c0fbb24de30f690dd11d0dbe603 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Mon, 16 Nov 2020 20:24:42 -0500 Subject: [PATCH] Add import export commands to manage.py (#1723) * Add `import_ctf` and `export_ctf` commands to `manage.py` * Deprecate `import.py` and `export.py` * Works on #1629 --- export.py | 3 +++ import.py | 3 +++ manage.py | 40 +++++++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/export.py b/export.py index 3bca659a..9eca3fdc 100644 --- a/export.py +++ b/export.py @@ -9,6 +9,9 @@ import shutil app = create_app() with app.app_context(): + print( + "This file will be deleted in CTFd v4.0. Switch to using `python manage.py export_ctf`" + ) backup = export_ctf() if len(sys.argv) > 1: diff --git a/import.py b/import.py index fa59abb6..c0f0ba8b 100644 --- a/import.py +++ b/import.py @@ -8,4 +8,7 @@ import sys app = create_app() with app.app_context(): + print( + "This file will be deleted in CTFd v4.0. Switch to using `python manage.py import_ctf`" + ) import_ctf(sys.argv[1]) diff --git a/manage.py b/manage.py index 0e8e8cd5..e061d9cd 100644 --- a/manage.py +++ b/manage.py @@ -1,10 +1,15 @@ -from flask import Flask -from flask_sqlalchemy import SQLAlchemy +import datetime +import shutil + +from flask_migrate import MigrateCommand from flask_script import Manager -from flask_migrate import Migrate, MigrateCommand + from CTFd import create_app -from CTFd.utils import get_config as get_config_util, set_config as set_config_util -from CTFd.models import * +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 app = create_app() @@ -46,5 +51,30 @@ def build(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") + 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): + with app.app_context(): + import_ctf_util(path) + + if __name__ == "__main__": manager.run()