Skip to content

Commit 2ffc977

Browse files
authored
Add files via upload
1 parent 4844e9d commit 2ffc977

4 files changed

+143
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 create_virtualenv():
16+
cwd = os.getcwd()
17+
venv_path = os.path.join(cwd, ".venv")
18+
19+
command = f"python3.10 -m venv {venv_path}"
20+
21+
try:
22+
print(f"Creating virtual environment at: {venv_path}")
23+
subprocess.run(command, shell=True, check=True)
24+
print(f"Virtual environment '.venv' created successfully in {cwd}.")
25+
26+
print("\nThe virtual environment is activated!")
27+
28+
shell = os.environ.get("SHELL", "/bin/bash")
29+
subprocess.run(f"exec {shell} --rcfile {venv_path}/bin/activate", shell=True)
30+
31+
except subprocess.CalledProcessError as e:
32+
print(f"Error while creating virtual environment: {e}")
33+
sys.exit(1)
34+
35+
if __name__ == "__main__":
36+
if not is_wsl():
37+
print("This script must be run inside WSL2. Exiting...")
38+
sys.exit(1)
39+
40+
create_virtualenv()
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 create_virtualenv():
16+
cwd = os.getcwd()
17+
venv_path = os.path.join(cwd, ".venv")
18+
19+
command = f"python3.11 -m venv {venv_path}"
20+
21+
try:
22+
print(f"Creating virtual environment at: {venv_path}")
23+
subprocess.run(command, shell=True, check=True)
24+
print(f"Virtual environment '.venv' created successfully in {cwd}.")
25+
26+
print("\nThe virtual environment is activated!")
27+
28+
shell = os.environ.get("SHELL", "/bin/bash")
29+
subprocess.run(f"exec {shell} --rcfile {venv_path}/bin/activate", shell=True)
30+
31+
except subprocess.CalledProcessError as e:
32+
print(f"Error while creating virtual environment: {e}")
33+
sys.exit(1)
34+
35+
if __name__ == "__main__":
36+
if not is_wsl():
37+
print("This script must be run inside WSL2. Exiting...")
38+
sys.exit(1)
39+
40+
create_virtualenv()

Run_WSL_Python_Script.exe

10.6 MB
Binary file not shown.

Run_WSL_Python_Script.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Script Developer: Gabriel Mihai Sandu
2+
# GitHub Profile: https://github.com/Gabrieliam42
3+
4+
import os
5+
import sys
6+
import ctypes
7+
import tkinter as tk
8+
from tkinter import filedialog
9+
import subprocess
10+
11+
def get_current_directory():
12+
return os.getcwd()
13+
14+
def check_admin_privileges():
15+
try:
16+
return ctypes.windll.shell32.IsUserAnAdmin() != 0
17+
except:
18+
return False
19+
20+
def run_as_admin(script, params):
21+
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, f'"{script}" {params}', None, 1)
22+
23+
def select_python_file():
24+
root = tk.Tk()
25+
root.withdraw()
26+
file_path = filedialog.askopenfilename(filetypes=[("Python files", "*.py")])
27+
return file_path
28+
29+
def convert_path_to_wsl_format(path):
30+
drive, rest = os.path.splitdrive(path)
31+
if drive:
32+
drive_letter = drive[0].lower()
33+
rest = rest.replace('\\', '/')
34+
return f"/mnt/{drive_letter}{rest}"
35+
return path
36+
37+
def run_python_file(file_path):
38+
wsl_path = convert_path_to_wsl_format(file_path)
39+
subprocess.run(['wsl', 'python3', wsl_path], stderr=subprocess.STDOUT, stdout=sys.stdout)
40+
41+
def main():
42+
cwd = get_current_directory()
43+
print(f"Current working directory: {cwd}")
44+
if not check_admin_privileges():
45+
print("Script is not running with admin privileges. Restarting with admin privileges...")
46+
run_as_admin(__file__, "")
47+
return
48+
py_files = []
49+
for root, dirs, files in os.walk(cwd):
50+
for file in files:
51+
if file.endswith('.py'):
52+
py_files.append(os.path.join(root, file))
53+
if not py_files:
54+
print("No .py files found in the current directory or its subdirectories.")
55+
return
56+
selected_file = select_python_file()
57+
if not selected_file:
58+
print("No file selected.")
59+
return
60+
run_python_file(selected_file)
61+
62+
if __name__ == "__main__":
63+
main()

0 commit comments

Comments
 (0)