chiark / gitweb /
systemd-analyze: use argparse instead of getopt
[elogind.git] / src / analyze / systemd-analyze
index 636fd74..88699d6 100755 (executable)
@@ -1,14 +1,16 @@
 #!/usr/bin/python
 
-import getopt, dbus, sys, os
+import sys, os
+import argparse
+from gi.repository import Gio
 try:
         import cairo
 except ImportError:
         cairo = None
 
 def acquire_time_data():
-
-        manager = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.systemd1.Manager')
+        manager = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
+                None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager', None)
         units = manager.ListUnits()
 
         l = []
@@ -17,23 +19,25 @@ def acquire_time_data():
                 if i[5] != "":
                         continue
 
-                properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', i[6]), 'org.freedesktop.DBus.Properties')
+                properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
+                        None, 'org.freedesktop.systemd1', i[6], 'org.freedesktop.DBus.Properties', None)
 
-                ixt = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveExitTimestampMonotonic'))
-                aet = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic'))
-                axt = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic'))
-                iet = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic'))
+                ixt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveExitTimestampMonotonic')
+                aet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic')
+                axt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic')
+                iet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic')
 
                 l.append((str(i[0]), ixt, aet, axt, iet))
 
         return l
 
 def acquire_start_time():
-        properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.DBus.Properties')
+        properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
+                None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.DBus.Properties', None)
 
-        initrd_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic'))
-        userspace_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'UserspaceTimestampMonotonic'))
-        finish_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic'))
+        initrd_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic')
+        userspace_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'UserspaceTimestampMonotonic')
+        finish_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic')
 
         if finish_time == 0:
                 sys.stderr.write("Bootup is not yet finished. Please try again later.\n")
@@ -72,20 +76,6 @@ def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5
 
         context.restore()
 
-def usage():
-        sys.stdout.write("""systemd-analyze [--user] time
-systemd-analyze [--user] blame
-systemd-analyze [--user] plot
-
-Process systemd profiling information
-
-  -h --help         Show this help
-""")
-
-def help():
-        usage()
-        sys.exit()
-
 def time():
 
         initrd_time, start_time, finish_time = acquire_start_time()
@@ -276,34 +266,29 @@ def plot():
 
         surface.finish()
 
-def unknown_verb():
-        sys.stderr.write("Unknown verb '%s'.\n" % args[0])
-        usage()
-        sys.exit(1)
+parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
+                                 description='Process systemd profiling information',
+                                 epilog='''\
+time - print time spent in the kernel before reaching userspace
+blame - print list of running units ordered by time to init
+plot - output SVG graphic showing service initialization
+''')
 
-bus = dbus.SystemBus()
+parser.add_argument('action', choices=('time', 'blame', 'plot'),
+                    default='time', nargs='?',
+                    help='action to perform (default: time)')
+parser.add_argument('--user', action='store_true',
+                    help='use the session bus')
 
-try:
-        opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "user"])
-except getopt.GetoptError as err:
-        sys.stdout.write(str(err) + "\n")
-        usage()
-        sys.exit(2)
-for o, a in opts:
-        if o in ("-h", "--help"):
-                help()
-        elif o == '--user':
-                bus = dbus.SessionBus()
-        else:
-                assert False, "unhandled option"
+args = parser.parse_args()
+
+if args.user:
+        bus = Gio.BusType.SESSION
+else:
+        bus = Gio.BusType.SYSTEM
 
 verb = {'time' : time,
        'blame': blame,
        'plot' : plot,
-       'help' : help,
        }
-
-if len(args) == 0:
-        time()
-else:
-        verb.get(args[0], unknown_verb)()
+verb.get(args.action)()