[systemd-devel] [PATCH] systemd-analyze: switch to python getopt for argument parsing

Patrick McCarty patrick.mccarty at linux.intel.com
Mon May 14 14:07:01 PDT 2012


On Thu, May 03, 2012 at 08:13:44PM -0700, Shawn Landden wrote:
> this uses gnu style getopt, so you can put the opts at the end: (e.g.)
> systemd-analyze blame --user
> 
> v2
> ---
>  src/analyze/systemd-analyze |   56 ++++++++++++++++++++++++++++++-------------
>  1 file changed, 39 insertions(+), 17 deletions(-)
> 
> diff --git a/src/analyze/systemd-analyze b/src/analyze/systemd-analyze
> index ad7bd9a..48d30ce 100755
> --- a/src/analyze/systemd-analyze
> +++ b/src/analyze/systemd-analyze
> @@ -1,6 +1,6 @@
>  #!/usr/bin/python
>  
> -import dbus, sys
> +import getopt, dbus, sys
>  
>  def acquire_time_data():
>  
> @@ -68,7 +68,7 @@ def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5
>  
>          context.restore()
>  
> -def help():
> +def usage():
>          sys.stdout.write("""systemd-analyze [--user] time
>  systemd-analyze [--user] blame
>  systemd-analyze [--user] plot
> @@ -78,16 +78,11 @@ Process systemd profiling information
>    -h --help         Show this help
>  """)
>  
> +def help():
> +        usage()
> +        sys.exit()
>  
> -bus = dbus.SystemBus()
> -command_index = 1
> -
> -if len(sys.argv) > 1 and sys.argv[1] == '--user':
> -        bus = dbus.SessionBus()
> -        command_index = 2
> -
> -
> -if len(sys.argv) <= command_index or sys.argv[command_index] == 'time':
> +def time():
>  
>          initrd_time, start_time, finish_time = acquire_start_time()
>  
> @@ -104,7 +99,7 @@ if len(sys.argv) <= command_index or sys.argv[command_index] == 'time':
>                          finish_time/1000)
>  
>  
> -elif sys.argv[command_index] == 'blame':
> +def blame():
>  
>          data = acquire_time_data()
>          s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
> @@ -119,7 +114,7 @@ elif sys.argv[command_index] == 'blame':
>  
>                  sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
>  
> -elif sys.argv[command_index] == 'plot':
> +def plot():

Defining plot() here breaks `systemd-analyze plot`: the cairo module is
no longer imported at top-level, which is required when using the cairo
module in draw_text().

Otherwise the patch looks OK to me.


-Patrick


More information about the systemd-devel mailing list