systemd-analyze: use argparse instead of getopt
Makes the output way nicer with shorter code. Also brings systemd-analyze behaviour more in line with other systemd-programs. Argparse is in Python since 2.6, and is available as a package for previous versions, if someone is stuck with very old Python.
This commit is contained in:
parent
c94f4b8b53
commit
0c0271841a
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import getopt, sys, os
|
import sys, os
|
||||||
|
import argparse
|
||||||
from gi.repository import Gio
|
from gi.repository import Gio
|
||||||
try:
|
try:
|
||||||
import cairo
|
import cairo
|
||||||
|
@ -75,20 +76,6 @@ def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5
|
||||||
|
|
||||||
context.restore()
|
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():
|
def time():
|
||||||
|
|
||||||
initrd_time, start_time, finish_time = acquire_start_time()
|
initrd_time, start_time, finish_time = acquire_start_time()
|
||||||
|
@ -279,34 +266,29 @@ def plot():
|
||||||
|
|
||||||
surface.finish()
|
surface.finish()
|
||||||
|
|
||||||
def unknown_verb():
|
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
sys.stderr.write("Unknown verb '%s'.\n" % args[0])
|
description='Process systemd profiling information',
|
||||||
usage()
|
epilog='''\
|
||||||
sys.exit(1)
|
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 = Gio.BusType.SYSTEM
|
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:
|
args = parser.parse_args()
|
||||||
opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "user"])
|
|
||||||
except getopt.GetoptError as err:
|
if args.user:
|
||||||
sys.stdout.write(str(err) + "\n")
|
bus = Gio.BusType.SESSION
|
||||||
usage()
|
else:
|
||||||
sys.exit(2)
|
bus = Gio.BusType.SYSTEM
|
||||||
for o, a in opts:
|
|
||||||
if o in ("-h", "--help"):
|
|
||||||
help()
|
|
||||||
elif o == '--user':
|
|
||||||
bus = Gio.BusType.SESSION
|
|
||||||
else:
|
|
||||||
assert False, "unhandled option"
|
|
||||||
|
|
||||||
verb = {'time' : time,
|
verb = {'time' : time,
|
||||||
'blame': blame,
|
'blame': blame,
|
||||||
'plot' : plot,
|
'plot' : plot,
|
||||||
'help' : help,
|
|
||||||
}
|
}
|
||||||
|
verb.get(args.action)()
|
||||||
if len(args) == 0:
|
|
||||||
time()
|
|
||||||
else:
|
|
||||||
verb.get(args[0], unknown_verb)()
|
|
||||||
|
|
Loading…
Reference in a new issue