partitioncloud-server/scripts/hooks/utils.py

70 lines
1.7 KiB
Python
Raw Normal View History

2024-01-16 21:32:00 +01:00
import os
2024-01-26 19:30:41 +01:00
import sys
import random
import string
2023-11-19 15:52:48 +01:00
import sqlite3
2024-01-26 19:30:41 +01:00
from colorama import Fore, Style
2023-11-19 15:52:48 +01:00
2024-01-16 21:00:14 +01:00
from . import config
2023-12-15 13:38:32 +01:00
def run_sqlite_command(*args):
2023-11-19 15:52:48 +01:00
"""Run a command against the database"""
2024-01-16 21:00:14 +01:00
con = sqlite3.connect(os.path.join(
config.instance,
"partitioncloud.sqlite"
))
2023-11-19 15:52:48 +01:00
cur = con.cursor()
cur.execute(*args)
2023-11-19 15:52:48 +01:00
con.commit()
con.close()
2023-12-15 13:38:32 +01:00
def get_sqlite_data(*args):
"""Get data from the db"""
2024-01-16 21:00:14 +01:00
con = sqlite3.connect(os.path.join(
config.instance,
"partitioncloud.sqlite"
))
cur = con.cursor()
data = cur.execute(*args)
2023-12-15 11:36:34 +01:00
new_data = list(data)
con.close()
return new_data
2023-12-15 13:38:32 +01:00
def new_uuid():
2023-12-15 13:38:32 +01:00
return "".join(
[random.choice(string.ascii_uppercase + string.digits) for _ in range(6)]
)
def format_uuid(uuid):
"""Format old uuid4 format"""
2023-12-15 11:36:34 +01:00
return uuid.upper()[:6]
2024-01-26 19:30:41 +01:00
def install_package(package):
print(f"\nThe following python package needs to be installed: {Style.BRIGHT}{Fore.YELLOW}{package}{Style.RESET_ALL}")
print(f"1. Install with {Style.BRIGHT}pip{Style.RESET_ALL} (automatic)")
print(f"2. Install manually (other package manager)")
option = input("Select an option: ")
try:
choice = int(option)
if choice == 1:
return_value = os.system(f"pip install {package} -qq")
if return_value == 0:
return
print(f"{Fore.RED}Installation with pip failed{Style.RESET_ALL}")
sys.exit(return_value)
elif choice == 2:
input("Install via you preferred option, and hit [Enter] when done")
return
except ValueError:
pass
print(f"{Fore.RED}Please enter a valid option{Style.RESET_ALL}")
return install_package(package)