Don’t even try to refactor it.

master
daz 2013-10-18 21:00:23 +02:00
commit e9a6137281
1 changed files with 37 additions and 0 deletions

37
killusall.py Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import dbus
from os.path import basename
from xml.etree import ElementTree
#pid = 17821
IDLE_PROCESS = ["zsh", "bash"]
KONSOLE_PATH = 'org.kde.konsole'
def is_shell(foreground_process_pid):
with open('/proc/{pid}/cmdline'.format(pid=foreground_process_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('org.kde.konsole', '/Sessions').Introspect()
)
for x in id_list:
object_path = "/Sessions/{id}".format(id=x)
session = session_bus.get_object('org.kde.konsole', object_path)
foreground_id = session.foregroundProcessId()
if is_shell(foreground_id):
session.runCommand("exit")