Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global variables #734

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions auto_cpufreq/bin/auto_cpufreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from auto_cpufreq.power_helper import *
from auto_cpufreq.battery_scripts.battery import *
from auto_cpufreq.utils.config import config as conf, find_config_file
from auto_cpufreq.globals import GITHUB, IS_INSTALLED_WITH_AUR, IS_INSTALLED_WITH_SNAP
# cli
@click.command()
@click.option("--monitor", is_flag=True, help="Monitor and see suggestions for CPU optimizations")
Expand Down Expand Up @@ -62,10 +63,10 @@ def config_info_dialog():
config_info_dialog()
root_check()
file_stats()
if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
if IS_INSTALLED_WITH_SNAP and dcheck == "enabled":
gnome_power_detect_snap()
tlp_service_detect_snap()
elif os.getenv("PKG_MARKER") != "SNAP":
elif not IS_INSTALLED_WITH_SNAP:
gnome_power_detect()
tlp_service_detect()
battery_setup()
Expand All @@ -88,7 +89,7 @@ def config_info_dialog():
battery_setup()
battery_get_thresholds()
conf.notifier.start()
if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
gnome_power_detect_snap()
tlp_service_detect_snap()
else:
Expand All @@ -115,7 +116,7 @@ def config_info_dialog():
battery_setup()
battery_get_thresholds()
conf.notifier.start()
if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
gnome_power_detect_snap()
tlp_service_detect_snap()
else:
Expand All @@ -141,7 +142,7 @@ def config_info_dialog():
not_running_daemon_check()
config_info_dialog()
print('\nNote: You can quit stats mode by pressing "ctrl+c"')
if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
gnome_power_detect_snap()
tlp_service_detect_snap()
else:
Expand Down Expand Up @@ -185,11 +186,11 @@ def config_info_dialog():
footer()
print("If auto-cpufreq helped you out and you find it useful ...\n")
print("Show your appreciation by donating!")
print("https://github.com/AdnanHodzic/auto-cpufreq/#donate")
print(GITHUB+"#donate")
footer()
elif install:
root_check()
if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
running_daemon_check()
gnome_power_detect_snap()
tlp_service_detect_snap()
Expand All @@ -204,7 +205,7 @@ def config_info_dialog():
deploy_complete_msg()
elif remove:
root_check()
if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
run("snapctl set daemon=disabled", shell=True)
run("snapctl stop --disable auto-cpufreq", shell=True)
if auto_cpufreq_stats_path.exists():
Expand All @@ -231,15 +232,14 @@ def config_info_dialog():
sys.argv.remove("--update")
if len(sys.argv) == 2: custom_dir = sys.argv[1]

if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
print("Detected auto-cpufreq was installed using snap")
# refresh snap directly using this command
# path wont work in this case

print("Please update using snap package manager, i.e: `sudo snap refresh auto-cpufreq`.")
#check for AUR
elif subprocess.run(["bash", "-c", "command -v pacman >/dev/null 2>&1"]).returncode == 0 and subprocess.run(["bash", "-c", "pacman -Q auto-cpufreq >/dev/null 2>&1"]).returncode == 0:
print("Arch-based distribution with AUR support detected. Please refresh auto-cpufreq using your AUR helper.")
elif IS_INSTALLED_WITH_AUR: print("Arch-based distribution with AUR support detected. Please refresh auto-cpufreq using your AUR helper.")
else:
is_new_update = check_for_update()
if not is_new_update: return
Expand Down
40 changes: 14 additions & 26 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
sys.path.append("../")
from auto_cpufreq.power_helper import *
from auto_cpufreq.utils.config import config
from auto_cpufreq.globals import GITHUB, IS_INSTALLED_WITH_AUR, IS_INSTALLED_WITH_SNAP

warnings.filterwarnings("ignore")

Expand Down Expand Up @@ -65,15 +66,12 @@
auto_cpufreq_stats_file = None

# track governor override
if os.getenv("PKG_MARKER") == "SNAP":
governor_override_state = Path("/var/snap/auto-cpufreq/current/override.pickle")
else:
governor_override_state = Path("/opt/auto-cpufreq/override.pickle")

if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
auto_cpufreq_stats_path = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.stats")
governor_override_state = Path("/var/snap/auto-cpufreq/current/override.pickle")
else:
auto_cpufreq_stats_path = Path("/var/run/auto-cpufreq.stats")
governor_override_state = Path("/opt/auto-cpufreq/override.pickle")

# daemon check
dcheck = getoutput("snapctl get daemon")
Expand Down Expand Up @@ -114,7 +112,7 @@ def set_override(override):
print("[!] Warning: Cannot get distro name")
if os.path.exists("/etc/pop-os/os-release"):
# Check if using a Snap
if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
print("[!] Snap install on PopOS detected, you must manually run the following"
" commands in another terminal:\n")
print("[!] Backup the /etc/os-release file:")
Expand All @@ -140,15 +138,9 @@ def app_version():
print("auto-cpufreq version: ", end="")

# snap package
if os.getenv("PKG_MARKER") == "SNAP":
print(getoutput(r"echo \(Snap\) $SNAP_VERSION"))
if IS_INSTALLED_WITH_SNAP: print(getoutput(r"echo \(Snap\) $SNAP_VERSION"))
# aur package
elif os.path.exists("/etc/arch-release"):
aur_pkg_check = call("pacman -Qs auto-cpufreq > /dev/null", shell=True)
if aur_pkg_check == 1:
print(get_formatted_version())
else:
print(getoutput("pacman -Qi auto-cpufreq | grep Version"))
elif IS_INSTALLED_WITH_AUR: print(getoutput("pacman -Qi auto-cpufreq | grep Version"))
else:
# source code (auto-cpufreq-installer)
try:
Expand All @@ -163,7 +155,7 @@ def check_for_update():
# Specify the repository and package name
# IT IS IMPORTANT TO THAT IF THE REPOSITORY STRUCTURE IS CHANGED, THE FOLLOWING FUNCTION NEEDS TO BE UPDATED ACCORDINGLY
# Fetch the latest release information from GitHub API
latest_release_url = f"https://api.github.com/repos/AdnanHodzic/auto-cpufreq/releases/latest"
latest_release_url = GITHUB.replace("github.com", "api.github.com/repos") + "/releases/latest"
try:
response = requests.get(latest_release_url)
if response.status_code == 200:
Expand Down Expand Up @@ -211,7 +203,7 @@ def check_for_update():
def new_update(custom_dir):
os.chdir(custom_dir)
print(f"Cloning the latest release to {custom_dir}")
run(["git", "clone", "https://github.com/AdnanHodzic/auto-cpufreq.git"])
run(["git", "clone", GITHUB+".git"])
os.chdir("auto-cpufreq")
print(f"package cloned to directory {custom_dir}")
run(['./auto-cpufreq-installer'], input='i\n', encoding='utf-8')
Expand Down Expand Up @@ -390,9 +382,7 @@ def cpufreqctl():
"""

# detect if running on a SNAP
if os.getenv("PKG_MARKER") == "SNAP":
pass
else:
if not IS_INSTALLED_WITH_SNAP:
# deploy cpufreqctl.auto-cpufreq script
if not os.path.isfile("/usr/local/bin/cpufreqctl.auto-cpufreq"):
shutil.copy(SCRIPTS_DIR / "cpufreqctl.sh", "/usr/local/bin/cpufreqctl.auto-cpufreq")
Expand All @@ -403,9 +393,7 @@ def cpufreqctl_restore():
remove cpufreqctl.auto-cpufreq script
"""
# detect if running on a SNAP
if os.getenv("PKG_MARKER") == "SNAP":
pass
else:
if not IS_INSTALLED_WITH_SNAP:
if os.path.isfile("/usr/local/bin/cpufreqctl.auto-cpufreq"):
os.remove("/usr/local/bin/cpufreqctl.auto-cpufreq")

Expand Down Expand Up @@ -1213,7 +1201,7 @@ def distro_info():
version = "UNKNOWN version"

# get distro information in snap env.
if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
try:
with open("/var/lib/snapd/hostfs/etc/os-release", "r") as searchfile:
for line in searchfile:
Expand Down Expand Up @@ -1388,7 +1376,7 @@ def running_daemon_check():
if is_running("auto-cpufreq", "--daemon"):
daemon_running_msg()
exit(1)
elif os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
elif IS_INSTALLED_WITH_SNAP and dcheck == "enabled":
daemon_running_msg()
exit(1)

Expand All @@ -1397,6 +1385,6 @@ def not_running_daemon_check():
if not is_running("auto-cpufreq", "--daemon"):
daemon_not_running_msg()
exit(1)
elif os.getenv("PKG_MARKER") == "SNAP" and dcheck == "disabled":
elif IS_INSTALLED_WITH_SNAP and dcheck == "disabled":
daemon_not_running_msg()
exit(1)
6 changes: 6 additions & 0 deletions auto_cpufreq/globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from os import getenv, path
from subprocess import getoutput

GITHUB = "https://github.com/AdnanHodzic/auto-cpufreq"
IS_INSTALLED_WITH_AUR = path.isfile("/etc/arch-release") and bool(getoutput("pacman -Qs auto-cpufreq"))
IS_INSTALLED_WITH_SNAP = getenv("PKG_MARKER") == "SNAP"
7 changes: 4 additions & 3 deletions auto_cpufreq/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
sys.path.append("../")
from auto_cpufreq.core import is_running, check_for_update, remove_daemon, new_update
from auto_cpufreq.gui.objects import RadioButtonView, SystemStatsLabel, CPUFreqStatsLabel, CurrentGovernorBox, DropDownMenu, DaemonNotRunningView, UpdateDialog
from auto_cpufreq.globals import GITHUB, IS_INSTALLED_WITH_SNAP

if os.getenv("PKG_MARKER") == "SNAP":
if IS_INSTALLED_WITH_SNAP:
ICON_FILE = "/snap/auto-cpufreq/current/icon.png"
CSS_FILE = "/snap/auto-cpufreq/current/style.css"
else:
Expand Down Expand Up @@ -67,7 +68,7 @@ def snap(self):
label = Gtk.Label(label="GUI not available due to Snap package confinement limitations.\nPlease install auto-cpufreq using auto-cpufreq-installer\nVisit the GitHub repo for more info")
label.set_justify(Gtk.Justification.CENTER)
button = Gtk.LinkButton.new_with_label(
uri="https://github.com/AdnanHodzic/auto-cpufreq",
uri=GITHUB,
label="GitHub Repo"
)

Expand Down Expand Up @@ -102,7 +103,7 @@ def daemon_not_running(self):
self.add(self.box)

def build(self):
if os.getenv("PKG_MARKER") == "SNAP": self.snap()
if IS_INSTALLED_WITH_SNAP: self.snap()
elif is_running("auto-cpufreq", "--daemon"): self.main()
else: self.daemon_not_running()

Expand Down
13 changes: 6 additions & 7 deletions auto_cpufreq/gui/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

from io import StringIO

from auto_cpufreq.globals import GITHUB, IS_INSTALLED_WITH_AUR, IS_INSTALLED_WITH_SNAP

PKEXEC_ERROR = "Error executing command as another user: Not authorized\n\nThis incident has been reported.\n"

auto_cpufreq_stats_path = ("/var/snap/auto-cpufreq/current" if os.getenv("PKG_MARKER") == "SNAP" else "/var/run") + "/auto-cpufreq.stats"
auto_cpufreq_stats_path = ("/var/snap/auto-cpufreq/current" if IS_INSTALLED_WITH_SNAP else "/var/run") + "/auto-cpufreq.stats"

def get_stats():
if os.path.isfile(auto_cpufreq_stats_path):
Expand All @@ -26,12 +28,9 @@ def get_stats():

def get_version():
# snap package
if os.getenv("PKG_MARKER") == "SNAP": return getoutput(r"echo \(Snap\) $SNAP_VERSION")
if IS_INSTALLED_WITH_SNAP: return getoutput(r"echo \(Snap\) $SNAP_VERSION")
# aur package
elif dist_name in ["arch", "manjaro", "garuda"]:
aur_pkg_check = run("pacman -Qs auto-cpufreq > /dev/null", shell=True)
if aur_pkg_check == 1: return get_formatted_version()
else: return getoutput("pacman -Qi auto-cpufreq | grep Version")
elif IS_INSTALLED_WITH_AUR: return getoutput("pacman -Qi auto-cpufreq | grep Version")
else:
# source code (auto-cpufreq-installer)
try: return get_formatted_version()
Expand Down Expand Up @@ -208,7 +207,7 @@ def __init__(self, parent):
self.title = Gtk.Label(label="auto-cpufreq", name="bold")
self.version = Gtk.Label(label=app_version)
self.python = Gtk.Label(label=f"Python {pl.python_version()}")
self.github = Gtk.Label(label="https://github.com/AdnanHodzic/auto-cpufreq")
self.github = Gtk.Label(label=GITHUB)
self.license = Gtk.Label(label="Licensed under LGPL3", name="small")
self.love = Gtk.Label(label="Made with <3", name="small")

Expand Down
29 changes: 15 additions & 14 deletions auto_cpufreq/power_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
sys.path.append("../")
from auto_cpufreq.core import *
from auto_cpufreq.tlp_stat_parser import TLPStatusParser
from auto_cpufreq.globals import GITHUB, IS_INSTALLED_WITH_SNAP

# app_name var
app_name = "python3 power_helper.py" if sys.argv[0] == "power_helper.py" else "auto-cpufreq"
Expand All @@ -27,13 +28,13 @@ def does_command_exists(cmd): return which(cmd) is not None
powerprofilesctl_exists = does_command_exists("powerprofilesctl")

# detect if gnome power profile service is running
if os.getenv("PKG_MARKER") != "SNAP":
if not IS_INSTALLED_WITH_SNAP:
if systemctl_exists:
try: gnome_power_status = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"])
except:
print("\nUnable to determine init system")
print("If this causes any problems, please submit an issue:")
print("https://github.com/AdnanHodzic/auto-cpufreq/issues")
print(GITHUB+"/issues")

# alert in case TLP service is running
def tlp_service_detect():
Expand All @@ -60,10 +61,10 @@ def gnome_power_detect():
print("Detected running GNOME Power Profiles daemon service!")
print("This daemon might interfere with auto-cpufreq and should be disabled.")
print("\nSteps to perform this action using auto-cpufreq: power_helper script:")
print("git clone https://github.com/AdnanHodzic/auto-cpufreq.git")
print(f"git clone {GITHUB}.git")
print("cd auto-cpufreq/auto_cpufreq")
print("python3 power_helper.py --gnome_power_disable")
print("\nReference: https://github.com/AdnanHodzic/auto-cpufreq#configuring-auto-cpufreq")
print(f"\nReference: {GITHUB}#configuring-auto-cpufreq")

# automatically disable gnome power profile service in case it's running during install
def gnome_power_detect_install():
Expand All @@ -79,15 +80,15 @@ def gnome_power_detect_install():
def gnome_power_detect_snap():
warning()
print("Due to Snap package confinement limitations please consider installing auto-cpufreq using")
print("auto-cpufreq-installer: https://github.com/AdnanHodzic/auto-cpufreq/#auto-cpufreq-installer")
print(f"auto-cpufreq-installer: {GITHUB}#auto-cpufreq-installer")
print()
print("Unable to detect state of GNOME Power Profiles daemon service!")
print("This daemon might interfere with auto-cpufreq and should be disabled.")
print("\nSteps to perform this action using auto-cpufreq: power_helper script:")
print("git clone https://github.com/AdnanHodzic/auto-cpufreq.git")
print(f"git clone {GITHUB}.git")
print("cd auto-cpufreq/auto_cpufreq")
print("python3 power_helper.py --gnome_power_disable")
print("\nReference: https://github.com/AdnanHodzic/auto-cpufreq#configuring-auto-cpufreq")
print(f"\nReference: {GITHUB}#configuring-auto-cpufreq")

# stops gnome >= 40 power profiles (live)
def gnome_power_stop_live():
Expand All @@ -111,7 +112,7 @@ def gnome_power_svc_enable():
except:
print("\nUnable to enable GNOME power profiles")
print("If this causes any problems, please submit an issue:")
print("https://github.com/AdnanHodzic/auto-cpufreq/issues")
print(GITHUB+"/issues")

# gnome power profiles current status
def gnome_power_svc_status():
Expand All @@ -122,11 +123,11 @@ def gnome_power_svc_status():
except:
print("\nUnable to see GNOME power profiles status")
print("If this causes any problems, please submit an issue:")
print("https://github.com/AdnanHodzic/auto-cpufreq/issues")
print(GITHUB+"/issues")

# disable bluetooth on boot
def bluetooth_disable():
if os.getenv("PKG_MARKER") == "SNAP": bluetooth_notif_snap()
if IS_INSTALLED_WITH_SNAP: bluetooth_notif_snap()
elif bluetoothctl_exists:
print("* Turn off bluetooth on boot")
btconf = Path("/etc/bluetooth/main.conf")
Expand All @@ -143,7 +144,7 @@ def bluetooth_disable():

# enable bluetooth on boot
def bluetooth_enable():
if os.getenv("PKG_MARKER") == "SNAP": bluetooth_on_notif_snap()
if IS_INSTALLED_WITH_SNAP: bluetooth_on_notif_snap()
if bluetoothctl_exists:
print("* Turn on bluetooth on boot")
btconf = "/etc/bluetooth/main.conf"
Expand Down Expand Up @@ -183,10 +184,10 @@ def gnome_power_rm_reminder_snap():
print("Unable to detect state of GNOME Power Profiles daemon service!")
print("Now it's recommended to enable this service.")
print("\nSteps to perform this action using auto-cpufreq: power_helper script:")
print("git clone https://github.com/AdnanHodzic/auto-cpufreq.git")
print(f"git clone {GITHUB}.git")
print("cd auto-cpufreq/auto_cpufreq")
print("python3 power_helper.py --gnome_power_enable")
print("\nReference: https://github.com/AdnanHodzic/auto-cpufreq#configuring-auto-cpufreq")
print(f"\nReference: {GITHUB}#configuring-auto-cpufreq")

def valid_options():
print("--gnome_power_enable\t\tEnable GNOME Power Profiles daemon")
Expand All @@ -203,7 +204,7 @@ def disable_power_profiles_daemon():
except:
print("\nUnable to disable GNOME power profiles")
print("If this causes any problems, please submit an issue:")
print("https://github.com/AdnanHodzic/auto-cpufreq/issues")
print(GITHUB+"/issues")

# default gnome_power_svc_disable func (balanced)
def gnome_power_svc_disable():
Expand Down
Loading