Juan Hernandez has uploaded a new change for review. Change subject: cli: Don't fail when there is no tty ......................................................................
cli: Don't fail when there is no tty Currently we always try to open the /dev/tty device, but this will fail in environments where there isn't a terminal available, like when running from a cron script. This patch changes the CLI so that it won't crash in these situations. Change-Id: Ie68152e171248b5b18296773b2cd5acaf98d34f3 Bug-Url: https://bugzilla.redhat.com/1119412 Signed-off-by: Juan Hernandez <[email protected]> --- M src/cli/platform/posix/terminal.py 1 file changed, 26 insertions(+), 14 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/38/30238/1 diff --git a/src/cli/platform/posix/terminal.py b/src/cli/platform/posix/terminal.py index 7ea3b95..8130313 100644 --- a/src/cli/platform/posix/terminal.py +++ b/src/cli/platform/posix/terminal.py @@ -33,34 +33,46 @@ def __init__(self, *args): super(PosixTerminal, self).__init__(*args) - self._tty = os.open('/dev/tty', os.O_RDWR) - curses.setupterm() + try: + self._tty = os.open('/dev/tty', os.O_RDWR) + curses.setupterm() + except OSError: + self._tty = None def _get_width(self): - packed = fcntl.ioctl(self._tty, termios.TIOCGWINSZ, 'xxxx') - width = struct.unpack('@HH', packed)[1] + if self._tty: + packed = fcntl.ioctl(self._tty, termios.TIOCGWINSZ, 'xxxx') + width = struct.unpack('@HH', packed)[1] + else: + width = 80 return width width = property(_get_width) def _get_height(self): - packed = fcntl.ioctl(self._tty, termios.TIOCGWINSZ, 'xxxx') - height = struct.unpack('@HH', packed)[0] + if self._tty: + packed = fcntl.ioctl(self._tty, termios.TIOCGWINSZ, 'xxxx') + height = struct.unpack('@HH', packed)[0] + else: + height = 25 return height def clear(self): - os.system("clear") + if self._tty: + os.system("clear") def set_echo(self, echo): - attrs = termios.tcgetattr(self._tty) - if echo: - attrs[3] |= termios.ECHO - else: - attrs[3] &= ~termios.ECHO - termios.tcsetattr(self._tty, termios.TCSANOW, attrs) + if self._tty: + attrs = termios.tcgetattr(self._tty) + if echo: + attrs[3] |= termios.ECHO + else: + attrs[3] &= ~termios.ECHO + termios.tcsetattr(self._tty, termios.TCSANOW, attrs) def close(self): - os.close(self._tty) + if self._tty: + os.close(self._tty) def readline(self, prompt): line = raw_input(prompt) -- To view, visit http://gerrit.ovirt.org/30238 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie68152e171248b5b18296773b2cd5acaf98d34f3 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-cli Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
