74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
import logging
|
|
import shutil
|
|
import sys
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
from pathlib import Path
|
|
import antidrift.client as client
|
|
from antidrift.config import Config
|
|
from antidrift.daemon import AntiDriftDaemon
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
DBusGMainLoop(set_as_default=True)
|
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
|
|
|
def init_logging(log_file: Path):
|
|
class DuplicateFilter(logging.Filter):
|
|
def filter(self, record) -> bool:
|
|
current_log = (record.module, record.levelno, record.msg)
|
|
if current_log != getattr(self, "last_log", None):
|
|
self.last_log = current_log
|
|
return True
|
|
return False
|
|
format_str = '[bold pale_green3]%(asctime)s[/bold pale_green3] | ' \
|
|
'[light_steel_blue]%(levelname)-8s[/light_steel_blue] | ' \
|
|
'%(message)s'
|
|
logging.basicConfig(filename=log_file,
|
|
format=format_str,
|
|
datefmt='%a %H:%M:%S',
|
|
encoding='utf-8',
|
|
level=logging.DEBUG)
|
|
logger = logging.getLogger()
|
|
logger.addFilter(DuplicateFilter())
|
|
|
|
|
|
def check_for_xdotool():
|
|
""" Check if xdotool is in path and exit if not """
|
|
result = shutil.which("xdotool")
|
|
if not result:
|
|
logging.critical("Please install xdotool")
|
|
sys.exit(1)
|
|
|
|
|
|
def main_daemon(config):
|
|
init_logging(config.daemon_log_file)
|
|
add = AntiDriftDaemon(config)
|
|
add.run()
|
|
|
|
|
|
def main() -> None:
|
|
""" Main routine that dispatches to client or daemon """
|
|
config = Config.load(os.path.expanduser("~/.config/antidrift.yaml"))
|
|
check_for_xdotool()
|
|
|
|
if client.antidrift_daemon_is_running():
|
|
init_logging(config.client_log_file)
|
|
client.client_mode(config)
|
|
elif len(sys.argv) == 1:
|
|
if os.geteuid() == 0:
|
|
newpid = os.fork()
|
|
if newpid == 0:
|
|
main_daemon(config)
|
|
else:
|
|
cmd = ["sudo", "/home/felixm/dot/bin/antidrift"]
|
|
subprocess.Popen(cmd)
|
|
elif len(sys.argv) == 2 and sys.argv[1] == '--daemon_user':
|
|
main_daemon(config)
|
|
else:
|
|
print("ad not running")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|