Skip to content

Commit

Permalink
Merge pull request avocado-framework#3801 from nanliu-r/update_to_pyt…
Browse files Browse the repository at this point in the history
…hon3_format

virtio_console_guest:update code format to keep compatibility
  • Loading branch information
luckyh authored Nov 30, 2023
2 parents 065fa0a + 2773289 commit 1fb23f1
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions virttest/shared/scripts/virtio_console_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,32 @@
import signal
import time
import platform
try:
import six
except ImportError:
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "six"])
from six.moves import xrange
from six.moves import input
from distutils.version import LooseVersion # pylint: disable=W0611,E0611

# For Python 2 and 3 compatibility
# xrange
try:
range = xrange
except NameError:
pass
# raw_input
try:
input = raw_input
except NameError:
pass
# iteritems, itervalues
if sys.version_info[0] == 3:
def itervalues(d, **kw):
return iter(d.values(**kw))

def iteritems(d, **kw):
return iter(d.items(**kw))
else:
def itervalues(d, **kw):
return d.itervalues(**kw)

def iteritems(d, **kw):
return d.iteritems(**kw)

if os.name == "posix": # Linux
os_linux = True
Expand Down Expand Up @@ -276,7 +293,7 @@ def _get_port_status(self, in_files=None):
line_list.append(line)
try:
for line in line_list:
m = re.match("(\S+): (\S+)", line)
m = re.match(r"(\S+): (\S+)", line)
port[m.group(1)] = m.group(2)

if port['is_console'] == "yes":
Expand Down Expand Up @@ -428,7 +445,7 @@ def _reconnect_none_mode(self):
# when bz796048 is resolved.
while not self.exit_thread.isSet():
data = b""
for i in xrange(len(self.in_files)):
for i in range(len(self.in_files)):
if self.exit_thread.isSet():
break
desc = self.in_files[i]
Expand All @@ -447,11 +464,11 @@ def _reconnect_none_mode(self):
sys.stdout.write("Missing device, readerr %s\n"
% inst)
_desc = desc
for item in six.iteritems(virt.files):
for item in iteritems(virt.files):
if item[1] == desc:
path = item[0]
break
for item in six.iteritems(virt.ports):
for item in iteritems(virt.ports):
if item[1]['path'] == path:
name = item[0]
break
Expand All @@ -466,7 +483,7 @@ def _reconnect_none_mode(self):
pass
self.in_files[self.in_files.index(_desc)] = desc
if data != b"":
for i in xrange(len(self.out_files)):
for i in range(len(self.out_files)):
if self.exit_thread.isSet():
break
desc = self.out_files[i]
Expand All @@ -492,11 +509,11 @@ def _reconnect_none_mode(self):
sys.stdout.write("Missing device, writeerr"
" %s\n" % inst)
_desc = desc
for item in six.iteritems(virt.files):
for item in iteritems(virt.files):
if item[1] == desc:
path = item[0]
break
for item in six.iteritems(virt.ports):
for item in iteritems(virt.ports):
if item[1]['path'] == path:
name = item[0]
break
Expand Down Expand Up @@ -796,7 +813,7 @@ def open(self, in_file, attempts=1):
:return: Array of descriptors.
"""
opened = False
for i in xrange(attempts):
for i in range(attempts):
try:
name = self.ports[in_file]["path"]
self.files[name] = os.open(name, os.O_RDWR)
Expand Down Expand Up @@ -848,7 +865,7 @@ def exit_threads(self):
self.exit_thread.clear()

del self.threads[:]
for desc in six.itervalues(self.files):
for desc in itervalues(self.files):
os.close(desc)
self.files.clear()
print("PASS: All threads finished")
Expand Down Expand Up @@ -986,7 +1003,7 @@ def init(self, in_files):

# Check if all ports really exists
remove = []
for item in six.iteritems(self.ports):
for item in iteritems(self.ports):
port = item[1]
try:
hFile = win32file.CreateFile(port['path'], 0, 0, None,
Expand Down Expand Up @@ -1095,7 +1112,7 @@ def exit_threads(self):
self.exit_thread.clear()

del self.threads[:]
for desc in six.itervalues(self.files):
for desc in itervalues(self.files):
win32file.CloseHandle(desc)
self.files.clear()
print("PASS: All threads finished")
Expand Down

0 comments on commit 1fb23f1

Please sign in to comment.