X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fnepi%2Futil%2Fsshfuncs.py;fp=src%2Fnepi%2Futil%2Fsshfuncs.py;h=34d19a243c45a2fb0d14e28c84acf2f9a488c7c6;hb=2cf4ac128c85ecdfd49d2142a6042bf11f9fbdb0;hp=048b818c57ee908188a34610eae92dbb77b7167c;hpb=feb18eb5ff86f1b1fbc9158ca6d5f89f948cb389;p=nepi.git diff --git a/src/nepi/util/sshfuncs.py b/src/nepi/util/sshfuncs.py index 048b818c..34d19a24 100644 --- a/src/nepi/util/sshfuncs.py +++ b/src/nepi/util/sshfuncs.py @@ -84,6 +84,7 @@ def resolve_hostname(host): shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines = True, ) stdout, stderr = p.communicate() m = _re_inet.findall(stdout) @@ -124,7 +125,8 @@ def openssh_has_persist(): ["ssh", "-v"], stdout = subprocess.PIPE, stderr = subprocess.STDOUT, - stdin = open("/dev/null"), + stdin = subprocess.DEVNULL, + universal_newlines = True, ) out,err = proc.communicate() proc.wait() @@ -708,6 +710,7 @@ def _retry_rexec(args, stdout = stdout, stdin = stdin, stderr = stderr, + universal_newlines = True, ) # attach tempfile object to the process, to make sure the file stays # alive until the process is finished with it @@ -722,7 +725,9 @@ def _retry_rexec(args, # The method communicate was re implemented for performance issues # when using python subprocess communicate method the ssh commands # last one minute each + #log("BEFORE communicate", level=logging.INFO); import time; beg=time.time() out, err = _communicate(proc, input=None) + #log("AFTER communicate - {}s".format(time.time()-beg), level=logging.INFO) elif stdout: out = proc.stdout.read() @@ -839,15 +844,25 @@ def _communicate(proc, input, timeout=None, err_on_timeout=True): write_set.remove(proc.stdin) if proc.stdout in rlist: - data = os.read(proc.stdout.fileno(), 1024) - if data == "": + # python2 version used to do this + # data = os.read(proc.stdout.fileno(), 1024) + # however this always returned bytes... + data = proc.stdout.read() + log('we have read {}'.format(data)) + # data should be str and not bytes because we use + # universal_lines = True, but to be clean + # instead of saying data != "" + if not data: + log('closing stdout') proc.stdout.close() read_set.remove(proc.stdout) stdout.append(data) if proc.stderr in rlist: - data = os.read(proc.stderr.fileno(), 1024) - if data == "": + # likewise (see above) + # data = os.read(proc.stderr.fileno(), 1024) + data = proc.stderr.read() + if not data: proc.stderr.close() read_set.remove(proc.stderr) stderr.append(data) @@ -858,15 +873,15 @@ def _communicate(proc, input, timeout=None, err_on_timeout=True): if stderr is not None: stderr = ''.join(stderr) - # Translate newlines, if requested. We cannot let the file - # object do the translation: It is based on stdio, which is - # impossible to combine with select (unless forcing no - # buffering). - if proc.universal_newlines and hasattr(file, 'newlines'): - if stdout: - stdout = proc._translate_newlines(stdout) - if stderr: - stderr = proc._translate_newlines(stderr) +# # Translate newlines, if requested. We cannot let the file +# # object do the translation: It is based on stdio, which is +# # impossible to combine with select (unless forcing no +# # buffering). +# if proc.universal_newlines and hasattr(file, 'newlines'): +# if stdout: +# stdout = proc._translate_newlines(stdout) +# if stderr: +# stderr = proc._translate_newlines(stderr) if killed and err_on_timeout: errcode = proc.poll()