2024-08-20 16:56:43 +00:00
|
|
|
import datetime
|
|
|
|
import json
|
2024-06-25 04:55:49 +00:00
|
|
|
import logging
|
2024-06-14 21:33:55 +00:00
|
|
|
import os
|
|
|
|
import re
|
2024-09-11 02:15:40 +00:00
|
|
|
import shutil
|
2024-06-14 21:33:55 +00:00
|
|
|
|
2024-06-15 04:20:33 +00:00
|
|
|
supported_versions = ["bukkit", "paper", "spigot"]
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-06-15 04:20:33 +00:00
|
|
|
def get_all_versions(folder_path="servers/paper"):
|
|
|
|
return [name for name in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, name))]
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
|
|
|
def version_exists(version, framework="paper") -> bool:
|
2024-06-15 04:20:33 +00:00
|
|
|
if framework not in supported_versions:
|
|
|
|
return False
|
2024-06-20 16:57:04 +00:00
|
|
|
folder_names = get_all_versions("servers/" + framework)
|
2024-06-15 04:20:33 +00:00
|
|
|
return any(version in name for name in folder_names)
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-06-14 21:33:55 +00:00
|
|
|
def create_folder(path):
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-06-14 21:33:55 +00:00
|
|
|
def check_if_exists(path):
|
|
|
|
return os.path.exists(path)
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-06-14 21:33:55 +00:00
|
|
|
def delete_non_empty_folder(path):
|
|
|
|
shutil.rmtree(path)
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-06-14 21:33:55 +00:00
|
|
|
def copy_folder_contents(source_dir, destination_dir):
|
|
|
|
if not os.path.exists(destination_dir):
|
|
|
|
os.makedirs(destination_dir)
|
|
|
|
for item_name in os.listdir(source_dir):
|
|
|
|
source_path = os.path.join(source_dir, item_name)
|
|
|
|
destination_path = os.path.join(destination_dir, item_name)
|
|
|
|
if os.path.isfile(source_path):
|
|
|
|
shutil.copy2(source_path, destination_path)
|
|
|
|
elif os.path.isdir(source_path):
|
|
|
|
if os.path.exists(destination_path):
|
|
|
|
for filename in os.listdir(source_path):
|
|
|
|
file_source_path = os.path.join(source_path, filename)
|
2024-06-20 16:57:04 +00:00
|
|
|
file_destination_path = os.path.join(destination_path, filename)
|
2024-06-14 21:33:55 +00:00
|
|
|
if not os.path.exists(file_destination_path):
|
|
|
|
shutil.copy2(file_source_path, file_destination_path)
|
|
|
|
else:
|
|
|
|
shutil.copytree(source_path, destination_path)
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-06-14 21:33:55 +00:00
|
|
|
def update_server_property(file_path, property_name, new_value):
|
|
|
|
pattern = rf'^{property_name}=.*$'
|
|
|
|
with open(file_path, 'r') as file:
|
|
|
|
content = file.readlines()
|
|
|
|
for i, line in enumerate(content):
|
|
|
|
if re.match(pattern, line):
|
|
|
|
content[i] = f"{property_name}={new_value}\n"
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Property '{property_name}' not found in the file.")
|
|
|
|
with open(file_path, 'w') as file:
|
|
|
|
file.writelines(content)
|
|
|
|
|
2024-06-20 16:57:04 +00:00
|
|
|
|
2024-08-15 08:49:41 +00:00
|
|
|
async def log_error(error_type: str, error_message: str):
|
2024-06-25 04:55:49 +00:00
|
|
|
logging.basicConfig(filename='logs.txt', level=logging.INFO,
|
|
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
datefmt='%Y-%m-%d %H:%M:%S')
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.error(f'{error_type}: {error_message}')
|
|
|
|
|
|
|
|
|
2024-08-20 16:56:43 +00:00
|
|
|
def log_action(user_id: str, name: str, action: str, details: str = None):
|
|
|
|
log_file = f"users/{user_id}/{name}/history.log"
|
|
|
|
event_log = {
|
|
|
|
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
"type": action,
|
|
|
|
"details": details,
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
with open(log_file, "a+") as log_file:
|
|
|
|
log_file.write(json.dumps(event_log) + "\n")
|
|
|
|
except Exception as e:
|
2024-08-20 17:35:36 +00:00
|
|
|
log_error(type(e).__name__, str(e) + " error trying to access history file on not existing server.")
|
2024-08-20 16:56:43 +00:00
|
|
|
|
|
|
|
|
2024-09-09 23:05:15 +00:00
|
|
|
def get_path_from_extension(filename) -> str or None:
|
|
|
|
allowed_extensions = {
|
|
|
|
'zip': "world/datapacks",
|
|
|
|
'jar': "plugins"
|
|
|
|
}
|
|
|
|
filename_lower = filename.strip().lower()
|
|
|
|
if '.' not in filename_lower:
|
|
|
|
return None
|
|
|
|
|
|
|
|
file_extension = filename_lower.split('.')[-1]
|
|
|
|
if file_extension in allowed_extensions:
|
|
|
|
return allowed_extensions[file_extension]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2024-09-11 02:15:40 +00:00
|
|
|
def filter_directory_contents(desired_extension: str, directory_contents: list[str]):
|
|
|
|
filtered_list = []
|
|
|
|
for item in directory_contents:
|
|
|
|
_, ext = os.path.splitext(item)
|
|
|
|
if ext.lower() == desired_extension:
|
|
|
|
filtered_list.append(item)
|
|
|
|
return filtered_list
|
|
|
|
|
|
|
|
|
2024-07-09 17:29:38 +00:00
|
|
|
def kebab_to_camel_case(s: str) -> str:
|
|
|
|
parts = s.split('-')
|
|
|
|
return parts[0] + ''.join(part.title() for part in parts[1:])
|
|
|
|
|
|
|
|
|
2024-06-14 21:33:55 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|