Created
July 25, 2013 12:07
-
-
Save Stebalien/6079034 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# encoding: utf-8 | |
import dbus, gobject, re | |
from dbus.mainloop.glib import DBusGMainLoop | |
INACTIVE_STATES = ("inactive", "failed") | |
UNIT_INTERFACE = 'org.freedesktop.systemd1.Unit' | |
unit_regex = re.compile('[^A-Za-z0-9]') | |
DBusGMainLoop(set_as_default=True) | |
event_loop = gobject.MainLoop() | |
bus = dbus.SessionBus() | |
manager_int = dbus.Interface( | |
bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), | |
dbus_interface="org.freedesktop.systemd1.Manager" | |
) | |
def wait(unit): | |
path = "/org/freedesktop/systemd1/unit/" + unit_regex.sub( | |
lambda x: "_" + x.group(0).encode("hex"), | |
unit | |
) | |
unit_int = dbus.Interface( | |
bus.get_object('org.freedesktop.systemd1', | |
path), | |
dbus_interface=dbus.PROPERTIES_IFACE | |
) | |
def handler(interface, changed, invalidated): | |
if interface == UNIT_INTERFACE \ | |
and "ActiveState" in invalidated \ | |
and unit_int.Get(interface, "ActiveState") in INACTIVE_STATES: | |
event_loop.quit() | |
unit_int.connect_to_signal('PropertiesChanged', handler, utf8_strings=True) | |
manager_int.Subscribe() | |
event_loop.run() | |
manager_int.Unsubscribe() | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) != 2: | |
print "Usage: %s UNIT" % sys.argv[0] | |
exit(1) | |
wait(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment