Commit 9dd12283 authored by Matthieu Gonnet's avatar Matthieu Gonnet Committed by Seblu
Browse files

Fix blink in watch command

The watch command uses a buffer instead of the standard output. Using this the watch
command can execute commands and wait the end of the execution to clear and print the buffer.
The watch command also catch cliException and KeyboardInterrupt in order to proceed to clean exit.
parent 34e1cf38
Loading
Loading
Loading
Loading
+22 −7
Original line number Diff line number Diff line
@@ -5,13 +5,14 @@
CloudControl watch command
'''

import time as systime
import sys
import time

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

from cStringIO import StringIO

class Command_watch(OptionCommand):
    '''Show output of a repeated command'''
@@ -28,9 +29,23 @@ class Command_watch(OptionCommand):
        if len(self.args) == 0:
            raise cmdBadArgument()
        while True:
            # Substitute aliases
            self.args = self.cli.aliases.substitute(self.args)
            # Put the fd of a buffer in sys.stdout and sys.err instead of the tty fd
            sys.stderr = sys.stdout = StringIO()
            try:
                # Clear tty
            self.cli.commands(["clear"])
            # Execute command
                self.printer.clear()
                # Execute command and write output in the buffer
                self.cli.commands(self.args)
            systime.sleep(self.options.seconds)
                # Print the buffer
                self.printer.out(sys.stdout.getvalue(), fd=sys.__stdout__)
                # Wait
                time.sleep(self.options.seconds)
            except KeyboardInterrupt:
                raise
            except cliException:
                raise
            finally:
                sys.stdout = sys.__stdout__
                sys.stderr = sys.__stderr__