summaryrefslogtreecommitdiffstats
path: root/killusall.py
blob: 8b4192f8989294167a52aadeb5ef6f985f2abda2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import dbus
from os.path import basename
from xml.etree import ElementTree
IDLE_PROCESS = ["zsh", "bash", "fish"]
KONSOLE_PATH = 'org.kde.konsole'


def is_shell(pid):
    with open('/proc/{pid}/cmdline'.format(pid=pid)) as file:
        path = file.read()
        process_name = basename(path)[:-1]   # \x00 >:
        return(process_name in IDLE_PROCESS)


def get_sessions_ids(sessions_xml):
    root = ElementTree.fromstring(sessions_xml)
    return [x.attrib['name'] for x in root if x.tag == 'node']


if __name__ == "__main__":
    session_bus = dbus.SessionBus()

    id_list = get_sessions_ids(
        session_bus.get_object(KONSOLE_PATH, '/Sessions').Introspect()
    )

    for x in id_list:
        object_path = "/Sessions/{id}".format(id=x)
        session = session_bus.get_object(KONSOLE_PATH, object_path)
        foreground_id = session.foregroundProcessId()
        if is_shell(foreground_id):
            session.sendText("\4")