Skip to content

Commit

Permalink
I hate root (please dont cancel me @linux)
Browse files Browse the repository at this point in the history
- Fixed an issue where the `root` variable was not correctly passed to functions, causing a `NameError`  when  executing admin commands

- removed unecessary argument "show_window"  in run_admin_command

- platform is not importet twice anymore
  • Loading branch information
truelockmc committed Dec 4, 2024
1 parent 8c77198 commit 7bfccb8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
13 changes: 6 additions & 7 deletions Features/command_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import platform
from Features.logging_func import log_command, log_error
import chardet
import platform
import tkinter as tk
from tkinter import messagebox
from Features.admin_elevate import is_admin, elevate
import os

def run_command(command, success_message):
try:
Expand All @@ -28,12 +28,12 @@ def run_command(command, success_message):
messagebox.showerror("Error", "An error occurred. Please check the log file for details.")

# Funktion, um einen Befehl als Administrator auszuführen und das Fenster zu verbergen
def run_admin_command(command, success_message, show_window=False):
def run_admin_command(command, success_message, root):
try:
if platform.system() == "Windows":
if is_admin():
# Befehl direkt ausführen, da der Benutzer Adminrechte hat
creationflags = subprocess.CREATE_NO_WINDOW if not show_window else 0
creationflags = subprocess.CREATE_NO_WINDOW
result = subprocess.run(command, shell=True, creationflags=creationflags)
if result.returncode == 0 or result.returncode == 3: # Ignoriere exit code 3
messagebox.showinfo("Success", success_message)
Expand All @@ -57,8 +57,7 @@ def run_admin_command(command, success_message, show_window=False):
log_error(f"Error running admin command: {command}", e)
messagebox.showerror("Error", f"An error occurred: {str(e)}")

def run_window_admin_command(command, success_message):
global root # Zugriff auf die globale Variable root
def run_window_admin_command(command, success_message, root):
try:
if platform.system() == "Windows":
if is_admin():
Expand All @@ -85,5 +84,5 @@ def run_window_admin_command(command, success_message):
messagebox.showerror("Error", f"Command failed with return code {result.returncode}")
except Exception as e:
# Fehlerprotokollierung und Anzeige
log_error(f"Error running admin command: {command}", e)
messagebox.showerror("Error", f"An error occurred: {str(e)}")
log_error(f"Error running admin command in window mode: {command}", e)
messagebox.showerror("Error", f"An error occurred: {str(e)}")
10 changes: 5 additions & 5 deletions UI/Buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def create_buttons(root, show_main_menu, show_info_menu, show_clean_menu, show_u

# Alle Buttons erstellen
buttons = {
"repair_file_system_button": tk.Button(root, text="Repair Filesystem", command=lambda: run_admin_command("echo J | chkdsk /f", "Successfully initiated chkdsk, your File System will be checked on the next startup."), **button_style),
"repair_file_system_button": tk.Button(root, text="Repair Filesystem", command=lambda: run_admin_command("echo J | chkdsk /f", "Successfully initiated chkdsk, your File System will be checked on the next startup.", root), **button_style),
"clean_vs_button": tk.Button(root, text="Clean Virtual Storage", command=clean_vs, **button_style),
"rm_dupe_button": tk.Button(root, text="Remove File Duplicates", command=lambda: rm_dupe(root), **button_style),
"info_button": tk.Button(root, text="Info", command=show_info_menu, **button_style),
Expand All @@ -39,21 +39,21 @@ def create_buttons(root, show_main_menu, show_info_menu, show_clean_menu, show_u
"resource_monitoring_button": tk.Button(root, text="Resource Monitoring", command=lambda: run_command("perfmon.exe /res", "Successfully ran Windows Resource Monitoring."), **button_style),
"speedtest_button": tk.Button(root, text="Internet Speed", command=lambda: open_speedtest_window(root), **button_style),
"connection_button": tk.Button(root, text="Connection", command=lambda: show_connection_info(root), **button_style),
"health_scan_button": tk.Button(root, text="Repair System Files", command=lambda: run_window_admin_command("title Health Scan && cls && sfc /scannow && DISM /Online /Cleanup-Image /RestoreHealth && pause", "Successfully started Scanning for Errors."), **button_style),
"health_scan_button": tk.Button(root, text="Repair System Files", command=lambda: run_window_admin_command("title Health Scan && cls && sfc /scannow && DISM /Online /Cleanup-Image /RestoreHealth && pause", "Successfully started Scanning for Errors.", root), **button_style),
"clean_button": tk.Button(root, text="Clean", command=show_clean_menu, **button_style),
"clean_mngr_button": tk.Button(root, text="Clean Manager", command=lambda: run_command("cleanmgr", "Disk cleanup completed successfully."), **button_style),
"wsreset_button": tk.Button(root, text="WSReset", command=lambda: run_command("wsreset.exe", "Microsoft Store reset completed successfully."), **button_style),
"disk_cleanup_button": tk.Button(root, text="Disk Cleanup", command=lambda: run_command("cleanmgr /sagerun:1", "Advanced Disk Cleanup completed successfully."), **button_style),
"temp_cleanup_button": tk.Button(root, text="Temp Cleanup", command=lambda: run_command(r"del /q/f/s %TEMP%\*", "Successfully deleted Temporary Files."), **button_style),
"prefetch_clean_button": tk.Button(root, text="Prefetch Cleanup", command=lambda: run_command(r"del /q/s C:\Windows\prefetch\*", "Successfully cleaned up Prefetch Files."), **button_style),
"clean_invis_button": tk.Button(root, text="Clean Invis", command=lambda: run_window_admin_command(r"cipher /w:c && msg * Successfully Cleaned invisible Space Consuming Files && exit", "Successfully ran Windows Cipher."), **button_style),
"clean_invis_button": tk.Button(root, text="Clean Invis", command=lambda: run_window_admin_command(r"cipher /w:c && msg * Successfully Cleaned invisible Space Consuming Files && exit", "Successfully ran Windows Cipher.", root), **button_style),
"Empty_RecycleBin_button": tk.Button(root, text="Empty Recycle Bin", command=clean_recycle_bin, **button_style),
"defragment_button": tk.Button(root, text="Defragment", command=defragment, **button_style),
"storage_diagonistics_button": tk.Button(root, text="Storage Diagnostics", command=lambda: run_command("MdSched.exe", "Successfully ran Windows Storage Diagnostics."), **button_style),
"update_button": tk.Button(root, text="Update", command=show_update_menu, **button_style),
"update_apps_button": tk.Button(root, text="Update Apps", command=lambda: run_window_admin_command("title Updater && cls && winget upgrade --all", "Winget initiated successfully."), **button_style),
"update_apps_button": tk.Button(root, text="Update Apps", command=lambda: run_window_admin_command("title Updater && cls && winget upgrade --all", "Winget initiated successfully.", root), **button_style),
"windows_update_button": tk.Button(root, text="Windows Update", command=lambda: run_command("wuauclt /detectnow /updatenow", "Windows update initiated successfully."), **button_style),
"repair_connection_button": tk.Button(root, text="Repair Connection", command=lambda: run_admin_command("ipconfig /flushdns && netsh winsock reset && ipconfig /release && ipconfig /renew", "Successfully reset Network and tried to fix Connection Problems."), **button_style),
"repair_connection_button": tk.Button(root, text="Repair Connection", command=lambda: run_admin_command("ipconfig /flushdns && netsh winsock reset && ipconfig /release && ipconfig /renew", "Successfully reset Network and tried to fix Connection Problems.", root), **button_style),
"driver_update_button": tk.Button(root, text="Driver Update", command=lambda: run_command("wuauclt /detectnow /updatenow", "Drivers updated successfully."), **button_style),
"back_button": tk.Button(root, text="Back", command=show_main_menu, **button_style),
"help_button": tk.Button(root, text="Help", command=lambda: print("Show Help (Placeholder)"), **button_style),
Expand Down

0 comments on commit 7bfccb8

Please sign in to comment.