2020-08-27 22:53:04 +00:00
|
|
|
import sys
|
|
|
|
import cmd
|
|
|
|
import time
|
2021-11-20 00:14:32 +00:00
|
|
|
import threading
|
2020-08-27 22:53:04 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from app.classes.shared.console import console
|
|
|
|
from app.classes.shared.helpers import helper
|
2022-03-19 02:55:39 +00:00
|
|
|
from app.classes.shared.import3 import import3
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.web.websocket_helper import websocket_helper
|
2020-08-27 22:53:04 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-03-08 04:40:44 +00:00
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class MainPrompt(cmd.Cmd):
|
2021-08-18 15:11:53 +00:00
|
|
|
def __init__(self, tasks_manager, migration_manager):
|
2021-03-22 04:02:18 +00:00
|
|
|
super().__init__()
|
|
|
|
self.tasks_manager = tasks_manager
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager = migration_manager
|
2020-08-27 22:53:04 +00:00
|
|
|
|
|
|
|
# overrides the default Prompt
|
2022-01-26 01:45:30 +00:00
|
|
|
prompt = f"Crafty Controller v{helper.get_version_string()} > "
|
2020-08-27 22:53:04 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def emptyline():
|
|
|
|
pass
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
# pylint: disable=unused-argument
|
2020-08-27 22:53:04 +00:00
|
|
|
def do_exit(self, line):
|
2021-12-09 23:35:00 +00:00
|
|
|
self.tasks_manager._main_graceful_exit()
|
2021-04-17 21:24:54 +00:00
|
|
|
self.universal_exit()
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2021-08-18 15:11:53 +00:00
|
|
|
def do_migrations(self, line):
|
2022-03-23 02:50:12 +00:00
|
|
|
if line == "up":
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.up()
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "down":
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.down()
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "done":
|
2021-08-18 15:11:53 +00:00
|
|
|
console.info(self.migration_manager.done)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "todo":
|
2021-08-18 15:11:53 +00:00
|
|
|
console.info(self.migration_manager.todo)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "diff":
|
2021-08-18 15:11:53 +00:00
|
|
|
console.info(self.migration_manager.diff)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "info":
|
|
|
|
console.info(f"Done: {self.migration_manager.done}")
|
|
|
|
console.info(f"FS: {self.migration_manager.todo}")
|
|
|
|
console.info(f"Todo: {self.migration_manager.diff}")
|
|
|
|
elif line.startswith("add "):
|
|
|
|
migration_name = line[len("add ") :]
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.create(migration_name, False)
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
console.info("Unknown migration command")
|
2022-01-15 00:23:50 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def do_threads(_line):
|
2021-11-20 00:14:32 +00:00
|
|
|
for thread in threading.enumerate():
|
2022-01-15 00:23:50 +00:00
|
|
|
if sys.version_info >= (3, 8):
|
2022-03-23 02:50:12 +00:00
|
|
|
print(
|
2022-03-23 06:06:13 +00:00
|
|
|
f"Name: {thread.name} Identifier: "
|
|
|
|
f"{thread.ident} TID/PID: {thread.native_id}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
print(f"Name: {thread.name} Identifier: {thread.ident}")
|
2021-11-20 00:14:32 +00:00
|
|
|
|
2022-03-19 03:59:10 +00:00
|
|
|
def do_import3(self, _line):
|
2022-03-19 02:55:39 +00:00
|
|
|
import3.start_import()
|
|
|
|
|
2021-04-17 21:24:54 +00:00
|
|
|
def universal_exit(self):
|
2020-08-27 22:53:04 +00:00
|
|
|
logger.info("Stopping all server daemons / threads")
|
2022-03-23 02:50:12 +00:00
|
|
|
console.info(
|
|
|
|
"Stopping all server daemons / threads - This may take a few seconds"
|
|
|
|
)
|
2021-02-26 15:39:35 +00:00
|
|
|
websocket_helper.disconnect_all()
|
2022-03-23 02:50:12 +00:00
|
|
|
console.info("Waiting for main thread to stop")
|
2020-08-27 22:53:04 +00:00
|
|
|
while True:
|
2021-03-06 20:48:02 +00:00
|
|
|
if self.tasks_manager.get_main_thread_run_status():
|
2020-08-27 22:53:04 +00:00
|
|
|
sys.exit(0)
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def help_exit():
|
|
|
|
console.help("Stops the server if running, Exits the program")
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2021-08-18 15:11:53 +00:00
|
|
|
@staticmethod
|
|
|
|
def help_migrations():
|
|
|
|
console.help("Only for advanced users. Use with caution")
|
2022-03-20 17:14:51 +00:00
|
|
|
|
2022-03-19 02:55:39 +00:00
|
|
|
@staticmethod
|
|
|
|
def help_import3():
|
2022-03-19 03:59:10 +00:00
|
|
|
console.help("Import users and servers from Crafty 3")
|