Skip to content
Snippets Groups Projects
time.py 1001 B
Newer Older
Matthieu Gonnet's avatar
Matthieu Gonnet committed
#!/usr/bin/env python
#coding=utf8

'''
CloudControl time command
'''

import time as systime
import datetime

from cccli.exception import *
from sjrpc.core.exceptions import *
from cccli.printer import Printer, color
from cccli.command import Command


class Command_time(Command):
    '''Show execution time'''

    def __init__(self, cli, argv0):
        Command.__init__(self, cli, argv0)

    def __call__(self, argv):
        if len(argv) < 2:
            raise cmdBadArgument()
        # Substitute aliases
        self.args = self.cli.aliases.substitute(argv[1:])
        # Init time
        t0 = systime.time()
        try:
Matthieu Gonnet's avatar
Matthieu Gonnet committed
            # Execute command
            self.cli.commands(self.args)
        except KeyboardInterrupt:
            raise
        except cliException:
            raise
        finally:
Matthieu Gonnet's avatar
Matthieu Gonnet committed
            t1 = systime.time()
            # Compute elapsed time
Matthieu Gonnet's avatar
Matthieu Gonnet committed
            dt = int(t1 - t0)
            self.printer.out("Execution time: %s" % datetime.timedelta(seconds=dt))