️ perf(improve): optimize functions

This commit is contained in:
eshanized
2024-12-29 11:36:00 +05:30
parent 41db7f7d1b
commit ba03f7d320

View File

@@ -12,8 +12,7 @@ from queue import Queue
import ui.GUI as GUI import ui.GUI as GUI
from ui.MessageDialog import MessageDialogBootloader from ui.MessageDialog import MessageDialogBootloader
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
gi.require_version("Wnck", "3.0") from gi.repository import Gtk, GdkPixbuf, GLib, Gdk
from gi.repository import Gtk, GdkPixbuf, GLib, Gdk, Wnck
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__))) base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__)))
REMOTE_SERVER = "www.google.com" REMOTE_SERVER = "www.google.com"
@@ -372,17 +371,28 @@ class Main(Gtk.Window):
def on_buttonarandr_clicked(self, widget): def on_buttonarandr_clicked(self, widget):
"""
Handles the "Arandr" button click. Checks if Arandr is installed and installs it if needed,
then launches the application.
"""
# Command to launch Arandr
app_cmd = ["/usr/bin/arandr"] app_cmd = ["/usr/bin/arandr"]
# Command to install Arandr using pacman
pacman_cmd = [ pacman_cmd = [
"pkexec", "pkexec", # Elevate privileges
"pacman", "pacman", # Package manager command
"-Sy", "-Sy", # Synchronize package databases
"arandr", "arandr", # Install the Arandr package
"--noconfirm", "--noconfirm", # Skip confirmation prompts
"--needed", "--needed", # Install only if not already installed
] ]
# Check if Arandr is installed
if not self.check_package_installed("arandr"): if not self.check_package_installed("arandr"):
# If not installed, check for the pacman lockfile
if not os.path.exists(self.pacman_lockfile): if not os.path.exists(self.pacman_lockfile):
# Display a warning dialog to inform the user
md = Gtk.MessageDialog( md = Gtk.MessageDialog(
parent=self, parent=self,
flags=0, flags=0,
@@ -391,56 +401,72 @@ class Main(Gtk.Window):
text="%s was not found\n" % "arandr", text="%s was not found\n" % "arandr",
title="Warning", title="Warning",
) )
# Add "Yes" and "No" buttons for user interaction
md.add_buttons("Yes", 1) md.add_buttons("Yes", 1)
md.add_buttons("No", 0) md.add_buttons("No", 0)
md.format_secondary_markup("Let Snigdha OS - Welcome install it ?") md.format_secondary_markup("Let Snigdha OS - Welcome install it?")
# Capture user response
response = md.run() response = md.run()
md.destroy() md.destroy()
if response == 1:
if response == 1: # User agrees to install Arandr
# Start the package queue checker in a separate thread
threading.Thread( threading.Thread(
target=self.check_package_queue, daemon=True target=self.check_package_queue, daemon=True
).start() ).start()
# Start the package installation in a separate thread
threading.Thread( threading.Thread(
target=self.install_package, target=self.install_package,
args=( args=(
app_cmd, app_cmd, # Command to launch Arandr after installation
pacman_cmd, pacman_cmd, # Command to install Arandr
"arandr", "arandr", # Package name
), ),
daemon=True, daemon=True,
).start() ).start()
else: else:
# If the pacman lockfile exists, display an error message
print( print(
"[ERROR]: Pacman lockfile found %s, is another pacman process running ?" "[ERROR]: Pacman lockfile found %s, is another pacman process running?"
% self.pacman_lockfile % self.pacman_lockfile
) )
# Show a warning dialog to notify the user
md = Gtk.MessageDialog( md = Gtk.MessageDialog(
parent=self, parent=self,
flags=0, flags=0,
message_type=Gtk.MessageType.WARNING, message_type=Gtk.MessageType.WARNING,
buttons=Gtk.ButtonsType.OK, buttons=Gtk.ButtonsType.OK,
text="Pacman lockfile found %s, is another pacman process running ?" text="Pacman lockfile found %s, is another pacman process running?"
% self.pacman_lockfile, % self.pacman_lockfile,
title="Warning", title="Warning",
) )
md.run() md.run()
md.destroy() md.destroy()
else: else:
# If Arandr is already installed, launch it in a separate thread
threading.Thread(target=self.run_app, args=(app_cmd,), daemon=True).start() threading.Thread(target=self.run_app, args=(app_cmd,), daemon=True).start()
def remove_dev_package(self, pacman_cmd, package): def remove_dev_package(self, pacman_cmd, package):
"""
Removes a specified development package using a given pacman command.
Args:
pacman_cmd (list): Command to run pacman for removing the package.
package (str): Name of the package to be removed.
"""
try: try:
# Notify the user that package removal has started
self.label_notify.set_name("label_style") self.label_notify.set_name("label_style")
GLib.idle_add( GLib.idle_add(self.label_notify.show)
self.label_notify.show,
)
GLib.idle_add( GLib.idle_add(
self.label_notify.set_markup, self.label_notify.set_markup,
"<span foreground='orange'><b>Removing dev package %s</b></span>" "<span foreground='orange'><b>Removing dev package %s</b></span>" % package,
% package,
)
GLib.idle_add(
self.label_notify.hide,
) )
GLib.idle_add(self.label_notify.hide)
# Execute the pacman command as a subprocess
with subprocess.Popen( with subprocess.Popen(
pacman_cmd, pacman_cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
@@ -449,48 +475,44 @@ class Main(Gtk.Window):
universal_newlines=True, universal_newlines=True,
) as process: ) as process:
while True: while True:
# Check if the process has finished
if process.poll() is not None: if process.poll() is not None:
break break
# Output each line of the subprocess's stdout
for line in process.stdout: for line in process.stdout:
print(line.strip()) print(line.strip())
if not self.check_package_installed(package): # Check if the package was successfully uninstalled
print("[INFO]: Pacman %s uninstall completed" % package) if not self.check_package_installed(package):
GLib.idle_add( print("[INFO]: Pacman %s uninstall completed" % package)
self.label_notify.show, # Notify the user of successful removal
) GLib.idle_add(self.label_notify.show)
self.label_notify.set_name("label_style") self.label_notify.set_name("label_style")
GLib.idle_add( GLib.idle_add(
self.label_notify.set_markup, self.label_notify.set_markup,
"<span foreground='orange'><b>Dev package %s removed</b></span>" "<span foreground='orange'><b>Dev package %s removed</b></span>" % package,
% package, )
) GLib.idle_add(self.label_notify.hide)
GLib.idle_add( else:
self.label_notify.hide, # Notify the user of a failure to uninstall
) print("[ERROR]: Pacman %s uninstall failed" % package)
else: self.label_notify.set_name("label_style")
print("[ERROR]: Pacman %s uninstall failed" % package) GLib.idle_add(self.label_notify.show)
self.label_notify.set_name("label_style") GLib.idle_add(
GLib.idle_add( self.label_notify.set_markup,
self.label_notify.show, "<span foreground='red'><b>Failed to remove dev package %s</b></span>" % package,
) )
GLib.idle_add(
self.label_notify.set_markup,
"<span foreground='red'><b>Failed to remove dev package %s</b></span>"
% package,
)
except Exception as e: except Exception as e:
# Handle exceptions and notify the user
print("[ERROR]: Exception in remove_dev_package(): %s" % e) print("[ERROR]: Exception in remove_dev_package(): %s" % e)
self.label_notify.set_name("label_style") self.label_notify.set_name("label_style")
GLib.idle_add( GLib.idle_add(self.label_notify.show)
self.label_notify.show,
)
GLib.idle_add( GLib.idle_add(
self.label_notify.set_markup, self.label_notify.set_markup,
"<span foreground='red'><b>Failed to remove dev package %s</b></span>" "<span foreground='red'><b>Failed to remove dev package %s</b></span>" % package,
% package,
) )
def install_package(self, app_cmd, pacman_cmd, package): def install_package(self, app_cmd, pacman_cmd, package):
try: try:
self.label_notify.set_name("label_style") self.label_notify.set_name("label_style")