|
| 1 | +# Script Developer: Gabriel Mihai Sandu |
| 2 | +# GitHub Profile: https://github.com/Gabrieliam42 |
| 3 | + |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +def is_wsl(): |
| 9 | + try: |
| 10 | + with open("/proc/version", "r") as f: |
| 11 | + return "microsoft" in f.read().lower() |
| 12 | + except FileNotFoundError: |
| 13 | + return False |
| 14 | + |
| 15 | +def find_activate(): |
| 16 | + cwd = os.getcwd() |
| 17 | + for root, dirs, files in os.walk(cwd): |
| 18 | + if 'bin' in dirs: |
| 19 | + activate_path = os.path.join(root, 'bin', 'activate') |
| 20 | + if os.path.isfile(activate_path): |
| 21 | + return activate_path |
| 22 | + return None |
| 23 | + |
| 24 | +def ensure_virtualenv(): |
| 25 | + if sys.prefix != sys.base_prefix: |
| 26 | + return |
| 27 | + activate_path = find_activate() |
| 28 | + if activate_path: |
| 29 | + print(f"Found activation script at: {activate_path}") |
| 30 | + venv_python = os.path.join(os.path.dirname(activate_path), "python") |
| 31 | + if not os.path.exists(venv_python): |
| 32 | + print("Python interpreter not found in the virtual environment.") |
| 33 | + input("Press Enter to exit...") |
| 34 | + sys.exit(1) |
| 35 | + print("Re-launching the script with the virtual environment's interpreter...") |
| 36 | + subprocess.run([venv_python] + sys.argv) |
| 37 | + input("Press Enter to exit...") |
| 38 | + sys.exit(0) |
| 39 | + else: |
| 40 | + print("No virtual environment activation script found in the current directory or its subdirectories.") |
| 41 | + input("Press Enter to exit...") |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | +def select_and_run_py(): |
| 45 | + py_files = [f for f in os.listdir('.') if f.endswith('.py') and os.path.isfile(f)] |
| 46 | + if not py_files: |
| 47 | + print("No Python files found in the current directory.") |
| 48 | + return |
| 49 | + |
| 50 | + print("Python files in the current directory:") |
| 51 | + for i, filename in enumerate(py_files, start=1): |
| 52 | + print(f"{i}. {filename}") |
| 53 | + |
| 54 | + try: |
| 55 | + choice = int(input("Select the number of the .py file you want to run: ")) |
| 56 | + if choice < 1 or choice > len(py_files): |
| 57 | + print("Invalid selection.") |
| 58 | + return |
| 59 | + except ValueError: |
| 60 | + print("Please enter a valid number.") |
| 61 | + return |
| 62 | + |
| 63 | + selected_file = py_files[choice - 1] |
| 64 | + command = [sys.executable, selected_file] |
| 65 | + |
| 66 | + print(f"Executing command: {' '.join(command)}") |
| 67 | + subprocess.run(command) |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + if not is_wsl(): |
| 71 | + print("This script must be run inside WSL2. Exiting...") |
| 72 | + input("Press Enter to exit...") |
| 73 | + sys.exit(1) |
| 74 | + |
| 75 | + ensure_virtualenv() |
| 76 | + |
| 77 | + select_and_run_py() |
0 commit comments