From bf3392236a56bce5cb660cb1129fe8bb11500569 Mon Sep 17 00:00:00 2001 From: Brosef Date: Sun, 22 Jun 2025 17:12:54 +0100 Subject: [PATCH] Created setup.py --- setup.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 setup.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..265d631 --- /dev/null +++ b/setup.py @@ -0,0 +1,68 @@ +""" +Initalises the launchers virtual environment, +and installs all dependancies. +""" + +import subprocess as sp +import platform +import shutil +import sys +import os + +def getVenvExec(): + """ + Gets the path of the virtual environment + based on the current opperating system. + """ + plat = platform.system() + if plat == 'Linux': + return f'./.venv/bin/python3' + elif plat == 'Windows': + return f'./.venv/Scripts/python.exe' + else: + print(f'WARNING: Playform type "{plat}" is not recognised. Exiting...') + exit(1) + +def inputYN(prompt: str, default=None): + """ + A simply yes/no prompt. + """ + while True: + i = input(prompt) + + if i.lower() in ['y', 'yes', '1', 'true']: return True + elif i.lower() in ['n', 'no', '0', 'false']: return False + elif i == '' and default != None: return default + +def installRequirements(requirementsPath: str): + """ + Installs a requirements.txt file to the current venv. + """ + print(f'---------- Installing {requirementsPath} ----------') + sp.run([getVenvExec(), '-m', 'pip', 'install', '-r', requirementsPath]) + +# Check for a pre-existing venv +if os.path.isdir('./.venv/'): + # If one exists, ask the user if they want to delete it + replace = inputYN(f'Virtual environment already exists, would you like to replace it? (y/n) > ') + if replace: + # If so, remove the .venv directory + print(f'---------- Removing old virtual environment ----------') + shutil.rmtree('./.venv/') + +# Initailise a venv, creating one if it doesn't exist +print(f'---------- Initalising virtual environment ----------') +sp.run([sys.executable, '-m', 'venv', './.venv/']) + +# Upgrade pip +print(f'---------- Upgrading pip ----------') +sp.run([getVenvExec(), '-m', 'pip', 'install', '-U', 'pip']) + +# Install all requirements.txt files +for path in os.listdir('../'): + requirementsPath = os.path.join('../', path, 'requirements.txt') + if not os.path.isfile(requirementsPath): + print(f'{path} does not have a requirements.txt, skipping...') + continue + + installRequirements(requirementsPath) \ No newline at end of file