Merge pull request #20 from Yamakaky/systemd-service
Improve bluetooth.service handling
This commit is contained in:
commit
b48b030b5e
2 changed files with 25 additions and 27 deletions
|
|
@ -5,6 +5,7 @@ import time
|
||||||
import logging
|
import logging
|
||||||
from shutil import which
|
from shutil import which
|
||||||
import random
|
import random
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import dbus
|
import dbus
|
||||||
|
|
||||||
|
|
@ -109,31 +110,33 @@ def toggle_clean_bluez(toggle):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
service_path = "/lib/systemd/system/bluetooth.service"
|
service_path = "/lib/systemd/system/bluetooth.service"
|
||||||
service = None
|
override_dir = Path("/run/systemd/system/bluetooth.service.d")
|
||||||
with open(service_path, "r") as f:
|
override_path = override_dir / "nxbt.conf"
|
||||||
service = f.read()
|
|
||||||
|
|
||||||
# Find the bluetooth service execution line
|
if toggle:
|
||||||
lines = service.split("\n")
|
if override_path.is_file():
|
||||||
for i in range(0, len(lines)):
|
# Override exist, no need to restart bluetooth
|
||||||
line = lines[i]
|
return
|
||||||
if line.startswith("ExecStart="):
|
|
||||||
# If we want to ensure the plugin is enabled
|
with open(service_path) as f:
|
||||||
if not toggle:
|
for line in f:
|
||||||
# If input is already enabled
|
if line.startswith("ExecStart="):
|
||||||
if "--compat --noplugin=*" not in line:
|
exec_start = line.strip() + " --compat --noplugin=*"
|
||||||
return
|
break
|
||||||
lines[i] = re.sub(" --compat --noplugin=\*", "", line)
|
|
||||||
else:
|
else:
|
||||||
# If input is already disabled
|
raise Exception("systemd service file doesn't have a ExecStart line")
|
||||||
if "--compat --noplugin=*" in line:
|
|
||||||
return
|
|
||||||
# If not, add the flag
|
|
||||||
lines[i] = line + " --compat --noplugin=*"
|
|
||||||
|
|
||||||
service = "\n".join(lines)
|
override = f"[Service]\nExecStart=\n{exec_start}"
|
||||||
with open(service_path, "w") as f:
|
|
||||||
f.write(service)
|
override_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
with override_path.open("w") as f:
|
||||||
|
f.write(override)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
os.remove(override_path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
# Override doesn't exist, no need to restart bluetooth
|
||||||
|
return
|
||||||
|
|
||||||
# Reload units
|
# Reload units
|
||||||
_run_command(["systemctl", "daemon-reload"])
|
_run_command(["systemctl", "daemon-reload"])
|
||||||
|
|
|
||||||
|
|
@ -323,11 +323,6 @@ if __name__ == "__main__":
|
||||||
with open("messages.txt", "w") as f:
|
with open("messages.txt", "w") as f:
|
||||||
f.write("\n".join(message_buffer))
|
f.write("\n".join(message_buffer))
|
||||||
|
|
||||||
try:
|
|
||||||
sys.exit(1)
|
|
||||||
except SystemExit:
|
|
||||||
os._exit(1)
|
|
||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print("Closing sockets")
|
print("Closing sockets")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue