|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import platform |
| 4 | +import winreg |
| 5 | +import ctypes |
| 6 | +import sys |
| 7 | +import re |
| 8 | + |
| 9 | +def is_admin(): |
| 10 | + try: |
| 11 | + return ctypes.windll.shell32.IsUserAnAdmin() |
| 12 | + except: |
| 13 | + return False |
| 14 | + |
| 15 | +def run_as_admin(): |
| 16 | + params = " ".join([f'"{arg}"' for arg in sys.argv]) |
| 17 | + ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, params, None, 1) |
| 18 | + |
| 19 | +def get_python_versions(base_path): |
| 20 | + python_versions = [] |
| 21 | + for folder in os.listdir(base_path): |
| 22 | + match = re.match(r'^Python(\d{3})$', folder) |
| 23 | + if match and match.group(1) != '310': |
| 24 | + python_versions.append(folder) |
| 25 | + return python_versions |
| 26 | + |
| 27 | +def update_environment_variable(variable_name, new_value, scope): |
| 28 | + try: |
| 29 | + with winreg.OpenKey(scope, r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 0, winreg.KEY_ALL_ACCESS) as key: |
| 30 | + value, regtype = winreg.QueryValueEx(key, variable_name) |
| 31 | + value_list = value.split(os.pathsep) |
| 32 | + # Remove any existing instances of the new value |
| 33 | + value_list = [v for v in value_list if v != new_value] |
| 34 | + # Add the new value to the top |
| 35 | + value_list.insert(0, new_value) |
| 36 | + new_value_str = os.pathsep.join(value_list) |
| 37 | + winreg.SetValueEx(key, variable_name, 0, regtype, new_value_str) |
| 38 | + print(f"Updated {variable_name} in system variables.") |
| 39 | + except FileNotFoundError: |
| 40 | + print(f"{variable_name} not found in system variables.") |
| 41 | + |
| 42 | +def create_virtual_environment(directory): |
| 43 | + print(f"Creating a virtual environment in {directory}...") |
| 44 | + subprocess.run(["python", "-m", "venv", ".venv310"], cwd=directory, shell=True) |
| 45 | + print("Virtual environment created.") |
| 46 | + |
| 47 | +def find_and_activate_venv(): |
| 48 | + cwd = os.getcwd() |
| 49 | + for root, dirs, files in os.walk(cwd): |
| 50 | + if 'Scripts' in dirs or 'bin' in dirs: |
| 51 | + scripts_path = os.path.join(root, 'Scripts' if platform.system() == 'Windows' else 'bin') |
| 52 | + required_files = {'activate.bat' if platform.system() == 'Windows' else 'activate'} |
| 53 | + if required_files.issubset(set(os.listdir(scripts_path))): |
| 54 | + print(f"Virtual environment found and activated at: {root}") |
| 55 | + if platform.system() == 'Windows': |
| 56 | + activate_command = f'cmd /k ""{os.path.join(scripts_path, "activate.bat")}" && python.exe -m pip install --upgrade pip"' |
| 57 | + else: |
| 58 | + activate_command = f'source "{os.path.join(scripts_path, "activate")}" && python3 -m pip install --upgrade pip' |
| 59 | + subprocess.run(activate_command, shell=True, executable='/bin/bash' if platform.system() != 'Windows' else None) |
| 60 | + return |
| 61 | + print("No virtual environment found.") |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + if not is_admin(): |
| 65 | + print("Requesting administrative privileges...") |
| 66 | + run_as_admin() |
| 67 | + sys.exit() |
| 68 | + |
| 69 | + base_path = r"C:\Program Files" |
| 70 | + new_path = r"C:\Program Files\Python310" |
| 71 | + new_path_scripts = os.path.join(new_path, 'Scripts') |
| 72 | + |
| 73 | + update_environment_variable('Path', new_path_scripts, winreg.HKEY_LOCAL_MACHINE) |
| 74 | + update_environment_variable('Path', new_path, winreg.HKEY_LOCAL_MACHINE) |
| 75 | + |
| 76 | + print("All changes to the System and User Variables have been made!") |
| 77 | + create_virtual_environment(os.getcwd()) |
| 78 | + find_and_activate_venv() |
| 79 | + |
| 80 | + if platform.system() == 'Windows': |
| 81 | + subprocess.run(['cmd', '/k', 'echo Virtual environment setup complete.'], shell=True) |
0 commit comments