Skip to content

Commit

Permalink
First pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Dids committed Aug 24, 2023
1 parent 4353794 commit d19f751
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
Thumbs.db

.vscode/
39 changes: 39 additions & 0 deletions klipper-priority-fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import psutil
import os
import time
import sys

KLIPPY_PROCESS_NAME = "klippy.py"

def set_high_priority(pid, method="nice"):
if method == "chrt":
cmd = f"sudo chrt --verbose --all-tasks --pid 1 {pid}"
elif method == "nice":
cmd = f"sudo renice -n -20 -p {pid}"
else:
raise ValueError(f"Unsupported priority method: {method}")

os.system(cmd)

def main(method="nice"):
while True:
try:
# Check if Klippy is running
klippy_process = None
for process in psutil.process_iter(["pid", "name", "cmdline"]):
if KLIPPY_PROCESS_NAME in process.info["name"] or KLIPPY_PROCESS_NAME in ' '.join(process.info["cmdline"]):
klippy_process = process
break

if klippy_process:
set_high_priority(klippy_process.info["pid"], method=method)

except Exception as e:
print(f"Error: {e}")

time.sleep(5) # check every 5 seconds

if __name__ == "__main__":
allowed_methods = ["chrt", "nice"]
method = sys.argv[1] if len(sys.argv) > 1 and sys.argv[1] in allowed_methods else "nice"
main(method=method)
176 changes: 176 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#!/usr/bin/env bash

#
# Installation script for klipper-priority-fix for Klipper.
#
# Based loosely on the following files/projects:
# - https://github.com/gobackup/gobackup/blob/main/install
# - https://github.com/eliteSchwein/mooncord/blob/master/scripts/install.sh
#

# Enable error handling.
set -eo pipefail

# Enable script debugging.
# set -x

# Get the script path.
SCRIPT_PATH="$(cd "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"

# Define global variables.
KLIPPER_PRIORITY_FIX_ROOT_PATH="$(cd "${SCRIPT_PATH}/.." >/dev/null 2>&1; pwd -P)"
KLIPPER_PRIORITY_FIX_INSTALL_PATH="/usr/local/bin"
KLIPPER_PRIORITY_FIX_SERVICE_NAME="klipper-priority-fix"
KLIPPER_PRIORITY_FIX_SERVICE_VERSION="1"

source "${SCRIPT_PATH}/util.sh"

# Ensure Python is available and meets the minimum version requirement (e.g., 3.6+)
if ! command -v python3 &> /dev/null; then
echo "Python3 is not installed. Please install Python3 and try again."
exit 1
fi
python_version=$(python3 --version | awk '{print $2}')
if [[ "$python_version" < "3.6" ]]; then
echo "Python version 3.6 or higher is required. Current version: $python_version"
exit 1
fi
PYTHON_PATH="$(which python3)"

# Function for installing or updating the custom klipper-priority-fix binary.
function install_klipper-priority-fix_binary() {
local source_path="${KLIPPER_PRIORITY_FIX_ROOT_PATH}/klipper-priority-fix.py"
local target_path="${KLIPPER_PRIORITY_FIX_INSTALL_PATH}/klipper-priority-fix"

# Check if this is a fresh install
if test -e "${target_path}"; then
echo "klipper-priority-fix binary already installed, checking for updates ..."
local source_hash="$(shasum -a 256 "${source_path}" | awk '{print $1}')"
local target_hash="$(shasum -a 256 "${target_path}" | awk '{print $1}')"
if test "${source_hash}" = "${target_hash}"; then
echo "klipper-priority-fix binary already up-to-date"
return
else
echo "klipper-priority-fix binary is out-of-date, updating ..."
fi
else
echo "klipper-priority-fix binary is not installed, installing ..."
fi

# Check if running as root
if test $(id -u) -eq 0; then
cp -f "${source_path}" "${target_path}"
chmod +x "${target_path}"
else
# Check if sudo is available
if ! command -v sudo &> /dev/null; then
echo "This script must be run as root or with sudo."
exit 1
fi
sudo cp -f "${source_path}" "${target_path}"
sudo chmod +x "${target_path}"
fi

echo "klipper-priority-fix binary successfully installed"
}

# Function for installing or updating the custom systemd service for klipper-priority-fix.
function install_klipper-priority-fix_service() {
local source_path="/tmp/${KLIPPER_PRIORITY_FIX_SERVICE_NAME}.service"
local target_path="/etc/systemd/system/${KLIPPER_PRIORITY_FIX_SERVICE_NAME}.service"

local user="$(id -un)"
if test -z "${user}"; then
echo "ERROR: Could not determine current user name" >&2
exit 1
fi

local group="$(id -gn)"
if test -z "${group}"; then
echo "ERROR: Could not determine current group name" >&2
exit 1
fi

# Echo a systemd service file to the temporary directory.
rm -f "${source_path}" || true
cat <<EOT >> "${source_path}"
# KLIPPER_PRIORITY_FIX_SERVICE_VERSION=${KLIPPER_PRIORITY_FIX_SERVICE_VERSION}
[Unit]
Description=Klipper Priority Manager
Requires=klipper.service
After=klipper.service
[Service]
Type=simple
User=${user}
Group=${group}
ExecStart=${PYTHON_PATH} ${KLIPPER_PRIORITY_FIX_INSTALL_PATH}/klipper-priority-fix
WorkingDirectory=${HOME}
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOT

# Check if this is a fresh install
if test -e "${target_path}"; then
echo "klipper-priority-fix systemd service already installed, checking for updates ..."

# Check the KLIPPER_PRIORITY_FIX_SERVICE_VERSION of the service file to determine if we need to update it
local service_version="$(grep "^# KLIPPER_PRIORITY_FIX_SERVICE_VERSION=" "${target_path}" | awk -F= '{print $NF}')"
if test "${service_version}" = "${KLIPPER_PRIORITY_FIX_SERVICE_VERSION}"; then
echo "klipper-priority-fix systemd service already up-to-date"
return
else
echo "Updating klipper-priority-fix systemd service from version ${service_version} to ${KLIPPER_PRIORITY_FIX_SERVICE_VERSION} ..."
fi
else
echo "klipper-priority-fix systemd service is not installed, installing ..."
fi

# Check if running as root
if test $(id -u) -eq 0; then
cp -f "${source_path}" "${target_path}"
else
# Check if sudo is available
if ! command -v sudo &> /dev/null; then
echo "This script must be run as root or with sudo."
exit 1
fi
sudo cp -f "${source_path}" "${target_path}"
fi

# Remove the temporary systemd service file.
rm -f "${source_path}" || true

# Reload systemd and its services.
reload_systemd

# Ensure that the klipper-priority-fix systemd service is always enabled.
enable_systemd_service "${KLIPPER_PRIORITY_FIX_SERVICE_NAME}"
}

# Stop the klipper-priority-fix systemd service.
stop_systemd_service "${KLIPPER_PRIORITY_FIX_SERVICE_NAME}"

# Check if klipper service is running
if ! systemctl is-active --quiet klipper.service; then
echo "Klipper service is not running. Please start the Klipper service and try again."
exit 1
fi

# Ensure that the klipper-priority-fix binary is installed and up-to-date.
install_klipper-priority-fix_binary

# Ensure that the custom systemd service is installed and up-to-date.
install_klipper-priority-fix_service

# Start the klipper-priority-fix systemd service.
start_systemd_service "${KLIPPER_PRIORITY_FIX_SERVICE_NAME}"

# Verify that the klipper-priority-fix systemd service is running.
verify_systemd_service_running "${KLIPPER_PRIORITY_FIX_SERVICE_NAME}"

echo "klipper-priority-fix installation complete"
exit 0
49 changes: 49 additions & 0 deletions scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bash

#
# Uninstallation script for klipper-priority-fix for Klipper.
#

# Enable error handling
set -eo pipefail

# Enable script debugging
# set -x

# Get the script path.
SCRIPT_PATH="$(cd "$(dirname "$0")" >/dev/null 2>&1; pwd -P)"

# Load the utility functions.
source "${SCRIPT_PATH}/util.sh"

# Stop the klipper-priority-fix systemd service.
echo "Stopping klipper-priority-fix systemd service ..."
stop_systemd_service

# Disable the klipper-priority-fix systemd service.
echo "Disabling klipper-priority-fix systemd service ..."
disable_systemd_service

# Remove the klipper-priority-fix systemd service.
echo "Removing klipper-priority-fix systemd service ..."
if test $(id -u) -eq 0; then
rm -f /etc/systemd/system/klipper-priority-fix.service
else
sudo rm -f /etc/systemd/system/klipper-priority-fix.service
fi

# Reload the systemd daemon.
reload_systemd

# Remove the klipper-priority-fix binary.
if test $(id -u) -eq 0; then
rm -f /usr/local/bin/klipper-priority-fix
else
sudo rm -f /usr/local/bin/klipper-priority-fix
fi

# Remove the klipper-priority-fix source code.
# echo "Removing klipper-priority-fix source code ..."
# rm -fr ~/klipper-priority-fix

echo "Successfully uninstalled klipper-priority-fix"
Loading

0 comments on commit d19f751

Please sign in to comment.