Fix py3 issues

- Update popen to use universal_newlines=True to enable newlines
  for calls that we parse or consume output for. Without universal_newlines=True,
  the output is treated as bytes under python3
  which leads to issues later where we are using it as
  strings.
- The division operator in Python 3 results in a float, not an integer
  like in Python 2. Use the truncation division operator instead.
- Use python builtins since the behavior of round has changed between
  python2 and python3.
  (https://docs.python.org/3/whatsnew/3.0.html#builtins)

Story: 2006796
Task: 42695

Signed-off-by: Charles Short <charles.short@windriver.com>
Change-Id: I9c9a4fdfd8a65cf6a3034a2c80c3a620ab138d22
(cherry picked from commit 9ced50cf38)
This commit is contained in:
Charles Short 2021-06-25 11:46:31 -04:00 committed by Chuck Short
parent 2ff8374037
commit 277d10417b
1 changed files with 3 additions and 2 deletions

View File

@ -29,6 +29,7 @@ import textwrap
import keyring import keyring
import subprocess import subprocess
import math import math
from builtins import round
from prettytable import PrettyTable from prettytable import PrettyTable
from cgtsclient.common import utils from cgtsclient.common import utils
from cgtsclient import client as cgts_client from cgtsclient import client as cgts_client
@ -145,7 +146,7 @@ def get_system_creds():
proc = subprocess.Popen(['bash', '-c', proc = subprocess.Popen(['bash', '-c',
'source /etc/platform/openrc && env'], 'source /etc/platform/openrc && env'],
stdout=subprocess.PIPE) stdout=subprocess.PIPE, universal_newlines=True)
for line in proc.stdout: for line in proc.stdout:
key, _, value = line.partition("=") key, _, value = line.partition("=")
@ -180,7 +181,7 @@ def convert_to_readable_size(size, orig_unit='B'):
if unitIndex > 5: if unitIndex > 5:
unitIndex = 5 unitIndex = 5
sizer = math.pow(1024, unitIndex) sizer = math.pow(1024, unitIndex)
newsize = round(size / sizer, 2) newsize = round(size // sizer, 2)
return "%s %s" % (newsize, units[unitIndex]) return "%s %s" % (newsize, units[unitIndex])