Skip to content

Commit a7e279c

Browse files
committed
CP-41972: update ACK to python3 and fixed as reviewer's suggestions
Signed-off-by: alexhimmel <alex.ac@qq.com>
1 parent b93376d commit a7e279c

24 files changed

+135
-129
lines changed

acktools/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# /usr/bin/env python
1+
# /usr/bin/env python3
22

33
import subprocess
44

55

66
def make_local_call(call):
77
"""Function wrapper for making a simple call to shell"""
8-
process = subprocess.Popen(call, stdout=subprocess.PIPE) # NOSONAR
8+
process = subprocess.Popen(call, stdout=subprocess.PIPE, universal_newlines=True) # NOSONAR
99
stdout, stderr = process.communicate()
1010
if process.returncode == 0:
11-
return str(stdout).strip()
11+
return stdout.strip()
1212
else:
1313
raise Exception("Error: '%s' '%s'" % (stdout, stderr))

acktools/log.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22

33
# Copyright (c) 2005-2022 Citrix Systems Inc.
44
# Copyright (c) 2022-12-01 Cloud Software Group Holdings, Inc.
@@ -48,7 +48,7 @@ def configure_log(name, path, to_stdout=True):
4848
'%%(asctime)-8s %s: %%(levelname)-8s %%(filename)s:%%(lineno)-10d %%(message)s' % name)
4949
fileh.setFormatter(formatter)
5050
log.addHandler(fileh)
51-
except IOError, e:
51+
except IOError as e:
5252
print("Error writing to file handler. Ignoring.")
5353
print(str(e))
5454

@@ -57,7 +57,7 @@ def configure_log(name, path, to_stdout=True):
5757
sth = logging.StreamHandler(sys.__stdout__)
5858
sth.setLevel(logging.DEBUG)
5959
log.addHandler(sth)
60-
except IOError, e:
60+
except IOError as e:
6161
print("Error writing to stdout handler. Ignoring.")
6262
print(str(e))
6363

acktools/net/route.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22

33
# Copyright (c) 2005-2022 Citrix Systems Inc.
44
# Copyright (c) 2022-12-01 Cloud Software Group Holdings, Inc.
@@ -31,6 +31,8 @@
3131
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3232
# SUCH DAMAGE.
3333

34+
import sys
35+
sys.path.append("/opt/xensource/packages/files/auto-cert-kit/pypackages")
3436
import acktools
3537
from acktools import utils
3638

acktools/net/tests/route_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
import unittest
44
import mock

acktools/tests/acktools_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
import unittest
44
import acktools

acktools/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22

33
# Copyright (c) 2005-2022 Citrix Systems Inc.
44
# Copyright (c) 2022-12-01 Cloud Software Group Holdings, Inc.
@@ -44,7 +44,7 @@ def cli_table_to_recs(table_string):
4444
route_recs = []
4545
for line in lines:
4646
vals = line.split()
47-
rec = dict(zip(header_line, vals))
47+
rec = dict(list(zip(header_line, vals)))
4848
route_recs.append(rec)
4949

5050
return route_recs

autocertkit/ack_cli.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22

33
# Copyright (c) 2005-2022 Citrix Systems Inc.
44
# Copyright (c) 2022-12-01 Cloud Software Group Holdings, Inc.
@@ -38,7 +38,7 @@
3838
import utils
3939
import sys
4040
import os
41-
import ConfigParser
41+
import configparser
4242

4343
import testbase
4444
import inspect
@@ -51,7 +51,7 @@
5151
import test_report
5252
import test_runner
5353
from optparse import OptionParser
54-
from exceptions import *
54+
import builtins as exceptions
5555

5656
MIN_VLAN = 0
5757
MAX_VLAN = 4096
@@ -140,7 +140,7 @@ def parse_cmd_args():
140140

141141
if options.optionstr:
142142
kvp_rec = kvp_string_to_rec(options.optionstr)
143-
for k, v in kvp_rec.iteritems():
143+
for k, v in kvp_rec.items():
144144
config[k] = v
145145

146146
check_files(config)
@@ -155,7 +155,7 @@ def check_files(config):
155155
if opt in config.keys():
156156
assert_file_exists(os.path.join(INSTALL_DIR, config[opt]), label)
157157

158-
for key, value in config['netconf'].iteritems():
158+
for key, value in config['netconf'].items():
159159
if key.startswith('eth'):
160160
vf_driver_pkg = value['vf_driver_pkg']
161161
if vf_driver_pkg:
@@ -196,7 +196,7 @@ def parse_netconf_file(filename):
196196
"""
197197
utils.log.debug("Parse network config file: %s" % filename)
198198

199-
cp = ConfigParser.ConfigParser()
199+
cp = configparser.ConfigParser()
200200
cp.read(filename)
201201
rec = {}
202202
for section in cp.sections():
@@ -282,7 +282,7 @@ def parse_section_static_net(cp, rec, section):
282282
'should be in format of "static_<network_id>_<vlan_id>"')
283283
net = arr[1]
284284
vlan = arr[2]
285-
if not unicode(net.strip()).isdecimal() or not unicode(vlan.strip()).isdecimal():
285+
if not str(net.strip()).isdecimal() or not str(vlan.strip()).isdecimal():
286286
raise utils.InvalidArgument('static addressing section', section,
287287
'should be valid network and/or vlan to determine')
288288
rec[section] = parse_static_config(cp, section)
@@ -461,7 +461,7 @@ def pre_flight_checks(session, config):
461461
# Check that we have at least two network adaptors, on the same network
462462
recs = config['netconf']
463463
ifaces = {}
464-
for k, v in recs.iteritems():
464+
for k, v in recs.items():
465465
if k.startswith('eth'):
466466
ifaces[k] = v['network_id']
467467

@@ -479,8 +479,8 @@ def pre_flight_checks(session, config):
479479
len(ifaces.keys()), ifaces.keys()))
480480

481481
# If less than 2 interfaces share the same network, raise an exception
482-
for k, v in ifaces.iteritems():
483-
if ifaces.values().count(v) < 2:
482+
for k, v in ifaces.items():
483+
if list(ifaces.values()).count(v) < 2:
484484
raise Exception("Error: ethernet device %s on network %s is defined in the network config but does not have a matching partner. \
485485
Please review the nework configuration and minumum requirements of this kit." % (k, v))
486486

autocertkit/models.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def create_xml_node(self, dom):
8989
xml_node = dom.createElement(str(self.get_name()))
9090

9191
# Set the elements XML attributes
92-
for k, v in self.attr.iteritems():
92+
for k, v in self.attr.items():
9393
xml_node.setAttribute(str(k), str(v))
9494

9595
# Set the elements value
@@ -175,7 +175,7 @@ def is_done(self):
175175
def create_xml_node(self, dom):
176176
"""Write this test method out to an xml node"""
177177
xml_node = dom.createElement('test_method')
178-
for k, v in self.attr.iteritems():
178+
for k, v in self.attr.items():
179179
xml_node.setAttribute(str(k), str(v))
180180

181181
for elem in self.elems:
@@ -208,7 +208,7 @@ def update_elem(self, k, v):
208208

209209
def update(self, rec):
210210
"""Update elements in this method object from a provided record"""
211-
for k, v in rec.iteritems():
211+
for k, v in rec.items():
212212
self.update_elem(k, v)
213213

214214

@@ -416,7 +416,7 @@ def get_id(self):
416416
if self.tag == "OP":
417417
xs_id = "XenServer %s" % self.config['product_version']
418418
return xs_id
419-
except Exception, e:
419+
except Exception as e:
420420
log.error("Exception occurred getting ID: '%s'" % str(e))
421421
return "Unknown ID"
422422

@@ -443,7 +443,7 @@ def get_description(self):
443443
if self.tag == "OP":
444444
build_id = "build %s" % self.config['build_number']
445445
return build_id
446-
except Exception, e:
446+
except Exception as e:
447447
log.error("Exception occurred getting Description: '%s'" % str(e))
448448
return "Unknown Device"
449449

@@ -557,7 +557,7 @@ def print_report(self, stream):
557557
stream.write("%s\n" % method.get_name())
558558
else:
559559
stream.write("Capabilities:\n")
560-
for k, v in self.get_caps().iteritems():
560+
for k, v in self.get_caps().items():
561561
if v:
562562
reqval = "Supported"
563563
else:

autocertkit/network_tests.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def run(self):
219219
# Capture interface statistcs post test run
220220
bytes_transferred = int(iperf_data['transfer'])
221221
self.validate_stats(bytes_transferred)
222-
except Exception, e:
222+
except Exception as e:
223223
traceb = traceback.format_exc()
224224
log.warning(traceb)
225225
fail_data["failed_attempt_%d" % (attempt_count)] = str(e)
@@ -684,7 +684,7 @@ def _set_offload_params(self, session, pif):
684684
log.debug(self.OFFLOAD_CONFIG)
685685
device = session.xenapi.PIF.get_device(pif)
686686
log.debug("Device: %s" % device)
687-
for k, v in self.OFFLOAD_CONFIG.iteritems():
687+
for k, v in self.OFFLOAD_CONFIG.items():
688688
set_hw_offload(session, device, k, v)
689689

690690
def _verify_ethtool_offloads(self, session, device):
@@ -693,7 +693,7 @@ def _verify_ethtool_offloads(self, session, device):
693693

694694
hw_offloads = get_hw_offloads(session, device)
695695
log.debug("verify offloads...%s" % hw_offloads)
696-
for k, v in self.OFFLOAD_CONFIG.iteritems():
696+
for k, v in self.OFFLOAD_CONFIG.items():
697697
if k not in hw_offloads:
698698
raise Exception("Cannot determine status of %s." % k)
699699
log.debug("Device: %s (%s offload: %s)" %

autocertkit/ssh.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self,
6060
self.trans = None
6161
try:
6262
self.connect(ip, username, password, timeout)
63-
except Exception, e:
63+
except Exception as e:
6464
log.error(traceback.format_exc())
6565
desc = str(e)
6666
log.error("SSH retry %d exception %s" % (retry, desc))
@@ -110,7 +110,7 @@ def connect(self, ip, username, password, timeout):
110110
try:
111111
self.trans.start_client()
112112
goes = 0
113-
except Exception, e:
113+
except Exception as e:
114114
goes = goes - 1
115115
if goes > 0:
116116
self.log.debug("Retrying SSHSession connection %d" % goes)
@@ -185,7 +185,7 @@ def __init__(self, ip, command, username, password, opt):
185185
self.client.shutdown(1)
186186
self.hStdout = self.client.makefile()
187187
self.hStderr = None if combine_stderr else self.client.makefile_stderr()
188-
except Exception, e:
188+
except Exception as e:
189189
self.reply = "SSH command executed failed: %s" % str(e),
190190
self.toreply = 1
191191
self.close()

autocertkit/status.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22

33
# Copyright (c) 2005-2022 Citrix Systems Inc.
44
# Copyright (c) 2022-12-01 Cloud Software Group Holdings, Inc.
@@ -47,7 +47,7 @@
4747

4848
def get_process_strings():
4949
ps = subprocess.Popen(
50-
['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
50+
['ps', 'aux'], stdout=subprocess.PIPE, universal_newlines=True).communicate()[0]
5151
process_strings = []
5252
for line in ps.split('\n'):
5353
if 'ack_cli.py' in line or 'test_runner.py' in line:

autocertkit/test_generators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def append_xml_config(self, doc, xml_node):
141141

142142
device_config = self.get_device_config()
143143
if device_config:
144-
for k, v in device_config.iteritems():
144+
for k, v in device_config.items():
145145
device_node.setAttribute(k, v)
146146

147147
# Set the unique device id

autocertkit/test_report.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def count_by_result(class_recs, fn):
4343

4444
test_matches = []
4545

46-
for class_name, (method_list, test_class_caps) in class_recs.iteritems():
46+
for class_name, (method_list, test_class_caps) in class_recs.items():
4747
for method in method_list:
4848
if fn(method['result']):
4949
test_matches.append("%s.%s" %

autocertkit/test_runner.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ def remove_child_nodes(parent_node):
136136

137137

138138
def recurse_add_records_to_node(topnode, record):
139-
for k, v in record.iteritems():
139+
for k, v in record.items():
140140
node = dom.createElement(k)
141141
topnode.appendChild(node)
142142

143143
if type(v) == dict:
144144
# Set attributes for element
145-
for key, value in v.iteritems():
145+
for key, value in v.items():
146146
log.debug("Value = %s Type=%s" %
147147
(str(value), str(type(value))))
148148
if type(value) == dict:

autocertkit/testbase.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def run(self, debug=False, test_name=None):
103103
continue
104104

105105
# This assumes that we do not keep IPs across individual tests
106-
for vlan, sm in self.static_managers.iteritems():
106+
for vlan, sm in self.static_managers.items():
107107
sm.release_all()
108108

109109
# Release Alarm signal to prevent handled signal from previous test
@@ -169,7 +169,7 @@ def run_test(self, test, debug, rec):
169169
self.copy_field(rec, res, 'reason', False)
170170
self.copy_field(rec, res, 'warning', False)
171171

172-
except Exception, e:
172+
except Exception as e:
173173
traceb = traceback.format_exc()
174174
rec['status'] = 'done'
175175
rec['result'] = 'fail'
@@ -298,7 +298,7 @@ def generate_static_net_conf_common(self, netid_rec, res):
298298

299299
# Iterate through the network config structure to
300300
# see if we have any static managers to initialise.
301-
for k, v in self.get_netconf().iteritems():
301+
for k, v in self.get_netconf().items():
302302
# We only care about vlans on the physical network ID this test is
303303
# running on
304304

@@ -328,7 +328,7 @@ def generate_static_net_conf(self):
328328
netconf = self.get_netconf()
329329
log.debug("Netconf: %s" % netconf)
330330
netid_rec = {}
331-
for iface, rec in netconf.iteritems():
331+
for iface, rec in netconf.items():
332332
if iface.startswith('eth'):
333333
log.debug("iface: %s Rec: %s" % (iface, rec))
334334
nid = rec['network_id']
@@ -412,7 +412,7 @@ def get_pifs_to_use(self):
412412
equiv_devs = self.get_equivalent_devices()
413413
try:
414414
return filter_pif_devices(self.session, equiv_devs)
415-
except Exception, e:
415+
except Exception as e:
416416
log.error(
417417
"Caught Exception - may be OK if running in single NIC mode.")
418418
log.error("Exception Occurred: %s" % str(e))
@@ -497,7 +497,7 @@ def get_bondable_ifaces(self, iface):
497497
# Construct a list of interface names who have the same physical ID
498498
# as the provided interface.
499499

500-
blist = intersection([k for k, v in netconf.iteritems() if k.startswith('eth') and
500+
blist = intersection([k for k, v in netconf.items() if k.startswith('eth') and
501501
v['network_id'] == phy_id],
502502
netconf.keys())
503503

autocertkit/tests/frontend-test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22

33
import unittest
44
import sys
55
import os
66
import os.path
77
import random
8-
import exceptions
8+
import builtins as exceptions
99
import tempfile
1010
import shutil
1111

0 commit comments

Comments
 (0)