1
0
Fork 0

Pass universal_newlines = True to Popen()

Use universal_newlines = True in calls to Popen(), in order to get
the output of the executed program as a string in Python3.
This commit is contained in:
Ondřej Lysoněk 2018-01-14 15:13:29 +01:00
parent e3ed58e160
commit be9440fb07
6 changed files with 32 additions and 10 deletions

View file

@ -182,7 +182,8 @@ class PowertopProfile:
self.output = output
def currentActiveProfile(self):
proc = Popen(["tuned-adm", "active"], stdout=PIPE)
proc = Popen(["tuned-adm", "active"], stdout=PIPE, \
universal_newlines = True)
output = proc.communicate()[0]
if output and output.find("Current active profile: ") == 0:
return output[len("Current active profile: "):output.find("\n")]
@ -200,7 +201,11 @@ class PowertopProfile:
environment = os.environ.copy()
environment["LC_ALL"] = "C"
try:
proc = Popen(["/usr/sbin/powertop", "--html=/tmp/powertop", "--time=1"], stdout=PIPE, stderr=PIPE, env=environment)
proc = Popen(["/usr/sbin/powertop", \
"--html=/tmp/powertop", "--time=1"], \
stdout=PIPE, stderr=PIPE, \
env=environment, \
universal_newlines = True)
output = proc.communicate()[1]
except (OSError, IOError):
print('Unable to execute PowerTOP, is PowerTOP installed?', file=sys.stderr)

View file

@ -223,8 +223,10 @@ class Plugin(object):
log.info("calling script '%s' with arguments '%s'" % (script, str(arguments)))
log.debug("using environment '%s'" % str(list(environ.items())))
try:
proc = Popen([script] + arguments, stdout=PIPE, stderr=PIPE, close_fds=True, env=environ, \
cwd = dir_name)
proc = Popen([script] + arguments, \
stdout=PIPE, stderr=PIPE, \
close_fds=True, env=environ, \
cwd = dir_name, universal_newlines = True)
out, err = proc.communicate()
if proc.returncode:
log.error("script '%s' error: %d, '%s'" % (script, proc.returncode, err[:-1]))

View file

@ -23,7 +23,10 @@ class MountsPlugin(base.Plugin):
mountpoint_topology = {}
current_disk = None
stdout, stderr = Popen(["lsblk", "-rno", "TYPE,RM,KNAME,FSTYPE,MOUNTPOINT"], stdout=PIPE, stderr=PIPE, close_fds=True).communicate()
stdout, stderr = Popen(["lsblk", "-rno", \
"TYPE,RM,KNAME,FSTYPE,MOUNTPOINT"], \
stdout=PIPE, stderr=PIPE, close_fds=True, \
universal_newlines = True).communicate()
for columns in [line.split() for line in stdout.splitlines()]:
if len(columns) < 3:
continue

View file

@ -37,8 +37,11 @@ class ScriptPlugin(base.Plugin):
log.info("calling script '%s' with arguments '%s'" % (script, str(arguments)))
log.debug("using environment '%s'" % str(list(environ.items())))
try:
proc = Popen([script] + arguments, stdout=PIPE, stderr=PIPE, close_fds=True, env=environ, \
cwd = os.path.dirname(script))
proc = Popen([script] + arguments, \
stdout=PIPE, stderr=PIPE, \
close_fds=True, env=environ, \
universal_newlines = True, \
cwd = os.path.dirname(script))
out, err = proc.communicate()
if proc.returncode:
log.error("script '%s' error: %d, '%s'" % (script, proc.returncode, err[:-1]))

View file

@ -211,7 +211,11 @@ class commands:
out = ""
err_msg = None
try:
proc = Popen(args, stdout = PIPE, stderr = PIPE, env = self._environment, shell = shell, cwd = cwd, close_fds = True)
proc = Popen(args, stdout = PIPE, stderr = PIPE, \
env = self._environment, \
shell = shell, cwd = cwd, \
close_fds = True, \
universal_newlines = True)
out, err = proc.communicate()
retcode = proc.returncode

View file

@ -113,8 +113,13 @@ class Nettool:
# run ethtool and preprocess output
p_ethtool = Popen(["ethtool", self._interface], stdout=PIPE, stderr=PIPE, close_fds=True)
p_filter = Popen(["sed", "s/^\s*//;s/:\s*/:\\n/g"], stdin=p_ethtool.stdout, stdout=PIPE, close_fds=True)
p_ethtool = Popen(["ethtool", self._interface], \
stdout=PIPE, stderr=PIPE, close_fds=True, \
universal_newlines = True)
p_filter = Popen(["sed", "s/^\s*//;s/:\s*/:\\n/g"], \
stdin=p_ethtool.stdout, stdout=PIPE, \
universal_newlines = True, \
close_fds=True)
output = p_filter.communicate()[0]
errors = p_ethtool.communicate()[1]