shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
+ universal_newlines = True,
)
stdout, stderr = p.communicate()
m = _re_inet.findall(stdout)
["ssh", "-v"],
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
- stdin = open("/dev/null"),
+ stdin = subprocess.DEVNULL,
+ universal_newlines = True,
)
out,err = proc.communicate()
proc.wait()
if out:
try:
- return list(map(int,out.strip().split(' ',1)))
+ return [ int(x) for x in out.strip().split(' ',1)) ]
except:
# Ignore, many ways to fail that don't matter that much
return None
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
# 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()
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)
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()