forked from tpd94/CDRM-Project
refactor Python checks for improved validation
This commit is contained in:
parent
1328805fa5
commit
5f217f2995
@ -1,33 +1,26 @@
|
|||||||
|
"""Module to check for the Python version and environment."""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import venv
|
import venv
|
||||||
|
import importlib.util
|
||||||
|
|
||||||
|
|
||||||
def version_check():
|
def version_check():
|
||||||
major_version = sys.version_info.major
|
"""Check for the Python version."""
|
||||||
minor_version = sys.version_info.minor
|
if sys.version_info < (3, 12):
|
||||||
|
sys.exit("Python version 3.12 or higher is required")
|
||||||
if major_version >= 3:
|
|
||||||
if minor_version >= 12:
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
exit("Python version 3.12 or higher is required")
|
|
||||||
else:
|
|
||||||
exit("Python 2 detected, Python version 3.12 or higher is required")
|
|
||||||
|
|
||||||
|
|
||||||
def pip_check():
|
def pip_check():
|
||||||
try:
|
"""Check for the pip installation."""
|
||||||
import pip
|
if importlib.util.find_spec("pip") is None:
|
||||||
|
sys.exit("Pip is not installed")
|
||||||
return
|
|
||||||
except ImportError:
|
|
||||||
exit("Pip is not installed")
|
|
||||||
|
|
||||||
|
|
||||||
def venv_check():
|
def venv_check():
|
||||||
# Check if we're already inside a virtual environment
|
"""Check for the virtual environment."""
|
||||||
if hasattr(sys, "real_prefix") or (
|
if hasattr(sys, "real_prefix") or (
|
||||||
hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix
|
hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix
|
||||||
):
|
):
|
||||||
@ -40,20 +33,20 @@ def venv_check():
|
|||||||
else os.path.join(venv_path, "Scripts", "python.exe")
|
else os.path.join(venv_path, "Scripts", "python.exe")
|
||||||
)
|
)
|
||||||
|
|
||||||
# If venv already exists, restart script using its Python
|
|
||||||
if os.path.exists(venv_path):
|
if os.path.exists(venv_path):
|
||||||
subprocess.call([venv_python] + sys.argv)
|
subprocess.call([venv_python] + sys.argv)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
# Ask user for permission to create a virtual environment
|
answer = (
|
||||||
answer = ""
|
input(
|
||||||
while not answer or answer[0].upper() not in {"Y", "N"}:
|
"Program is not running from a virtual environment. To maintain "
|
||||||
answer = input(
|
"compatibility, this program must be run from one.\n"
|
||||||
"Program is not running from a venv. To maintain compatibility and dependencies, this program must be run from one.\n"
|
"Would you like to create one? (Y/N): "
|
||||||
"Would you like me to create one for you? (Y/N): "
|
|
||||||
)
|
)
|
||||||
|
.strip()
|
||||||
if answer[0].upper() == "Y":
|
.upper()
|
||||||
|
)
|
||||||
|
if answer.startswith("Y"):
|
||||||
print("Creating virtual environment...")
|
print("Creating virtual environment...")
|
||||||
venv.create(venv_path, with_pip=True)
|
venv.create(venv_path, with_pip=True)
|
||||||
subprocess.call([venv_python] + sys.argv)
|
subprocess.call([venv_python] + sys.argv)
|
||||||
@ -64,37 +57,54 @@ def venv_check():
|
|||||||
|
|
||||||
|
|
||||||
def requirements_check():
|
def requirements_check():
|
||||||
try:
|
"""Check for the requirements."""
|
||||||
import pywidevine
|
required_packages = [
|
||||||
import pyplayready
|
"pywidevine",
|
||||||
import flask
|
"pyplayready",
|
||||||
import flask_cors
|
"flask",
|
||||||
import yaml
|
"flask_cors",
|
||||||
import mysql.connector
|
"yaml",
|
||||||
|
"mysql.connector",
|
||||||
|
]
|
||||||
|
missing = []
|
||||||
|
for pkg in required_packages:
|
||||||
|
if "." in pkg:
|
||||||
|
parent, _ = pkg.split(".", 1)
|
||||||
|
if (
|
||||||
|
importlib.util.find_spec(parent) is None
|
||||||
|
or importlib.util.find_spec(pkg) is None
|
||||||
|
):
|
||||||
|
missing.append(pkg)
|
||||||
|
else:
|
||||||
|
if importlib.util.find_spec(pkg) is None:
|
||||||
|
missing.append(pkg)
|
||||||
|
if not missing:
|
||||||
return
|
return
|
||||||
except ImportError:
|
|
||||||
while True:
|
while True:
|
||||||
user_input = (
|
user_input = (
|
||||||
input("Missing packages. Do you want to install them? (Y/N): ")
|
input(
|
||||||
.strip()
|
f"Missing packages: {', '.join(missing)}. Do you want to install them? (Y/N): "
|
||||||
.upper()
|
|
||||||
)
|
)
|
||||||
if user_input == "Y":
|
.strip()
|
||||||
print("Installing packages from requirements.txt...")
|
.upper()
|
||||||
subprocess.check_call(
|
)
|
||||||
[sys.executable, "-m", "pip", "install", "-r", "requirements.txt"]
|
if user_input == "Y":
|
||||||
)
|
print("Installing packages from requirements.txt...")
|
||||||
print("Installation complete.")
|
subprocess.check_call(
|
||||||
break
|
[sys.executable, "-m", "pip", "install", "-r", "requirements.txt"]
|
||||||
elif user_input == "N":
|
)
|
||||||
print("Dependencies required, please install them and run again.")
|
print("Installation complete.")
|
||||||
sys.exit()
|
break
|
||||||
else:
|
if user_input == "N":
|
||||||
print("Invalid input. Please enter 'Y' to install or 'N' to exit.")
|
print("Dependencies required, please install them and run again.")
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
|
print("Invalid input. Please enter 'Y' to install or 'N' to exit.")
|
||||||
|
|
||||||
|
|
||||||
def run_python_checks():
|
def run_python_checks():
|
||||||
|
"""Run the Python checks."""
|
||||||
if getattr(sys, "frozen", False): # Check if running from PyInstaller
|
if getattr(sys, "frozen", False): # Check if running from PyInstaller
|
||||||
return
|
return
|
||||||
version_check()
|
version_check()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user