Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
commit 49da34dde6e67f365d8498468d828338d7bb2a1d Author: Michal Minar miminar@redhat.com Date: Wed Aug 21 09:20:33 2013 +0200
properly handle redirected std(out|err) when prompting
Detect, which of stdout and stderr is attached to terminal and use it for prompt when asking for credentials. Stdout is preferred.
When stdout is redirected, readline won't have any history records. It needs to be checked before trying to modify it.
cli/lmi/shell/LMIConnection.py | 24 ++++++++++++++++++++---- 1 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/cli/lmi/shell/LMIConnection.py b/cli/lmi/shell/LMIConnection.py index 10211dd..5dfa5fe 100644 --- a/cli/lmi/shell/LMIConnection.py +++ b/cli/lmi/shell/LMIConnection.py @@ -44,14 +44,28 @@ def __lmi_raw_input(prompt, use_echo=True): prompt -- string with a prompt before the input begins use_echo -- bool flag, which indicates, if to echo the input on the command line """ + if not sys.stdout.isatty() and sys.stderr.isatty(): + # read the input with prompt printed to stderr + def get_input(prompt): + sys.stderr.write(prompt) + return raw_input() + stream = sys.stderr + else: + # read the input with prompt printed to stdout + # NOTE: raw_input uses stdout only if the readline module is imported + get_input = raw_input + stream = sys.stdout + if not sys.stderr.isatty() and not sys.stdout.isatty(): + LOG.warn('both stdout and stderr are detached from terminal,' + ' using stdout for prompt') if not use_echo: os.system("stty -echo") try: - result = raw_input(prompt) + result = get_input(prompt) except EOFError, e: if not use_echo: os.system("stty echo") - sys.stdout.write("\n") + stream.write("\n") return None except KeyboardInterrupt, e: if not use_echo: @@ -59,10 +73,12 @@ def __lmi_raw_input(prompt, use_echo=True): raise e if not use_echo: os.system("stty echo") - sys.stdout.write("\n") + stream.write("\n") if result: cur_hist_len = readline.get_current_history_length() - readline.remove_history_item(cur_hist_len - 1) + if cur_hist_len > 1: + readline.remove_history_item(cur_hist_len - 1) + return result
def connect(hostname, username="", password="", interactive=False, use_cache=True,
cura-tools-devel@lists.fedorahosted.org