Skip to content

Commit 57f9a1e

Browse files
authored
Merge pull request #11921 from madchutney/tools/py3-fixes
Updates to tools for Python 3 compatibility
2 parents 9974d83 + 23b12f4 commit 57f9a1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+144
-123
lines changed

tools/device_management.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ def inner(options):
115115
# Get the currently in-use API key (may come from environment or
116116
# configuration files, which is handled by the cloud SDK)
117117
api_key_value = accounts.config.get("api_key")
118-
api_key = accounts.list_api_keys(
118+
api_key = next(accounts.list_api_keys(
119119
filter={
120120
"key": api_key_value
121121
}
122-
).next()
122+
))
123123
certificates_owned = list(certs.list_certificates())
124124
dev_cert_info = None
125125
for certif in certificates_owned:

tools/export/exporters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from tools.targets import TARGET_MAP
2929
from tools.utils import mkdir
3030
from tools.resources import FileType, FileRef
31+
from future.utils import with_metaclass
3132

3233
"""Just a template for subclassing"""
3334

@@ -57,14 +58,13 @@ def __init__(*args, **kwargs):
5758
CLS.NAME = "%s (DEPRECATED)" % old_name
5859
return CLS
5960

60-
class Exporter(object):
61+
class Exporter(with_metaclass(ABCMeta, object)):
6162
"""Exporter base class
6263
6364
This class is meant to be extended by individual exporters, and provides a
6465
few helper methods for implementing an exporter with either jinja2 or
6566
progen.
6667
"""
67-
__metaclass__ = ABCMeta
6868
TEMPLATE_DIR = dirname(__file__)
6969
DOT_IN_RELATIVE_PATH = False
7070
NAME = None

tools/flash_algo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def format_algo_data(self, spaces, group_size, fmt):
134134
blob = self.algo_data[:]
135135
pad_size = 0 if len(blob) % 4 == 0 else 4 - len(blob) % 4
136136
blob = blob + "\x00" * pad_size
137-
integer_list = struct.unpack("<" + "L" * (len(blob) / 4), blob)
137+
integer_list = struct.unpack("<" + "L" * (len(blob) // 4), blob)
138138
line_list = []
139139
for pos in range(0, len(integer_list), group_size):
140140
group = ["0x%08x" % value for value in

tools/host_tests/default_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from sys import stdout
2020

21-
class DefaultAuto():
21+
class DefaultAuto(object):
2222
""" Simple, basic host test's test runner waiting for serial port
2323
output from MUT, no supervision over test running in MUT is executed.
2424
"""

tools/host_tests/detect_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import re
1919

20-
class DetectPlatformTest():
20+
class DetectPlatformTest(object):
2121
PATTERN_MICRO_NAME = "Target '(\w+)'"
2222
re_detect_micro_name = re.compile(PATTERN_MICRO_NAME)
2323

tools/host_tests/dev_null_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
limitations under the License.
1616
"""
1717

18-
class DevNullTest():
18+
class DevNullTest(object):
1919

2020
def check_readline(self, selftest, text):
2121
""" Reads line from serial port and checks if text was part of read string

tools/host_tests/echo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import uuid
2020
from sys import stdout
2121

22-
class EchoTest():
22+
class EchoTest(object):
2323

2424
# Test parameters
2525
TEST_SERIAL_BAUDRATE = 115200

tools/host_tests/echo_flow_control.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from host_test import Test
17+
from .host_test import Test
1818

1919

2020
class EchoTest(Test):

tools/host_tests/hello_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
limitations under the License.
1616
"""
1717

18-
class HelloTest():
18+
class HelloTest(object):
1919
HELLO_WORLD = "Hello World"
2020

2121
def test(self, selftest):

tools/host_tests/host_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
limitations under the License.
1616
"""
1717

18-
class HostRegistry:
18+
class HostRegistry(object):
1919
""" Class stores registry with host tests and objects representing them
2020
"""
2121
HOST_TESTS = {} # host_test_name -> host_test_ojbect

tools/host_tests/host_test.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# Check if 'serial' module is installed
1919
try:
2020
from serial import Serial
21-
except ImportError, e:
22-
print "Error: Can't import 'serial' module: %s"% e
21+
except ImportError as e:
22+
print("Error: Can't import 'serial' module: %s"% e)
2323
exit(-1)
2424

2525
import os
@@ -29,7 +29,7 @@
2929
from time import sleep, time
3030
from optparse import OptionParser
3131

32-
import host_tests_plugins
32+
from . import host_tests_plugins
3333

3434
# This is a little tricky. We need to add upper directory to path so
3535
# we can find packages we want from the same level as other files do
@@ -39,7 +39,7 @@
3939
from tools.test_api import get_module_avail
4040

4141

42-
class Mbed:
42+
class Mbed(object):
4343
""" Base class for a host driven test
4444
"""
4545
def __init__(self):
@@ -117,7 +117,7 @@ def __init__(self):
117117
self.serial_timeout = 1
118118

119119
self.timeout = self.DEFAULT_TOUT if self.options.timeout is None else self.options.timeout
120-
print 'MBED: Instrumentation: "%s" and disk: "%s"' % (self.port, self.disk)
120+
print('MBED: Instrumentation: "%s" and disk: "%s"' % (self.port, self.disk))
121121

122122
def init_serial_params(self, serial_baud=9600, serial_timeout=1):
123123
""" Initialize port parameters.
@@ -183,11 +183,11 @@ def pool_for_serial_init(self, serial_baud, serial_timeout, pooling_loops=40, in
183183
stdout.write('.')
184184
stdout.flush()
185185
else:
186-
print "...port ready!"
186+
print("...port ready!")
187187
result = True
188188
break
189189
if not result and last_error:
190-
print last_error
190+
print(last_error)
191191
return result
192192

193193
def set_serial_timeout(self, timeout):
@@ -221,7 +221,7 @@ def serial_readline(self, timeout=5):
221221
c = self.serial.read(1)
222222
result += c
223223
except Exception as e:
224-
print "MBED: %s"% str(e)
224+
print("MBED: %s"% str(e))
225225
result = None
226226
break
227227
if c == '\n':
@@ -298,7 +298,7 @@ def flush(self):
298298
return result
299299

300300

301-
class HostTestResults:
301+
class HostTestResults(object):
302302
""" Test results set by host tests
303303
"""
304304
def __init__(self):
@@ -389,8 +389,8 @@ def run(self):
389389
self.print_result(result)
390390
else:
391391
self.notify("HOST: Passive mode...")
392-
except Exception, e:
393-
print str(e)
392+
except Exception as e:
393+
print(str(e))
394394
self.print_result(self.RESULT_ERROR)
395395

396396
def setup(self):
@@ -406,7 +406,7 @@ def setup(self):
406406
def notify(self, message):
407407
""" On screen notification function
408408
"""
409-
print message
409+
print(message)
410410
stdout.flush()
411411

412412
def print_result(self, result):

tools/host_tests/mbedrpc.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
# >myled.write(1)
2828
# >
2929

30-
import serial, urllib2, time
30+
from future import standard_library
31+
standard_library.install_aliases()
32+
import serial, urllib.request, time
3133

3234
# mbed super class
33-
class mbed:
35+
class mbed(object):
3436
def __init__(self):
3537
print("This will work as a demo but no transport mechanism has been selected")
3638

@@ -48,7 +50,7 @@ def rpc(self, name, method, args):
4850
# creates the command to be sent serially - /name/method arg1 arg2 arg3 ... argN
4951
str = "/" + name + "/" + method + " " + " ".join(args) + "\n"
5052
# prints the command being executed
51-
print str
53+
print(str)
5254
# writes the command to serial
5355
self.ser.write(str)
5456
# strips trailing characters from the line just written
@@ -61,12 +63,12 @@ def __init__(self, ip):
6163
self.host = "http://" + ip
6264

6365
def rpc(self, name, method, args):
64-
response = urllib2.urlopen(self.host + "/rpc/" + name + "/" + method + "%20" + "%20".join(args))
66+
response = urllib.request.urlopen(self.host + "/rpc/" + name + "/" + method + "%20" + "%20".join(args))
6567
return response.read().strip()
6668

6769

6870
# generic mbed interface super class
69-
class mbed_interface():
71+
class mbed_interface(object):
7072
# initialize an mbed interface with a transport mechanism and pin name
7173
def __init__(self, this_mbed, mpin):
7274
self.mbed = this_mbed
@@ -198,7 +200,7 @@ def read_us(self):
198200
return float(re.search('\d+\.*\d*', r).group(0))
199201

200202
# Serial
201-
class Serial():
203+
class Serial(object):
202204
def __init__(self, this_mbed, tx, rx=""):
203205
self.mbed = this_mbed
204206
if isinstance(tx, str):

tools/host_tests/net_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from host_test import Test, Simple
17+
from .host_test import Test, Simple
1818
from sys import stdout
1919

2020
class NETTest(Simple):

tools/host_tests/rpc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
from host_test import Test
18-
from mbedrpc import SerialRPC, DigitalOut, DigitalIn, pin
17+
from .host_test import Test
18+
from .mbedrpc import SerialRPC, DigitalOut, DigitalIn, pin
1919

2020

2121
class RpcTest(Test):
@@ -30,7 +30,7 @@ def test(self):
3030

3131
if hasattr(self.mbed.options, 'micro'):
3232
if self.mbed.options.micro == 'M0+':
33-
print "Freedom Board: PTA12 <-> PTC4"
33+
print("Freedom Board: PTA12 <-> PTC4")
3434
p_out = pin("PTA12")
3535
p_in = pin("PTC4")
3636

tools/host_tests/rtc_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import re
1919
from time import time, strftime, gmtime
2020

21-
class RTCTest():
21+
class RTCTest(object):
2222
PATTERN_RTC_VALUE = "\[(\d+)\] \[(\d+-\d+-\d+ \d+:\d+:\d+ [AaPpMm]{2})\]"
2323
re_detect_rtc_value = re.compile(PATTERN_RTC_VALUE)
2424

tools/host_tests/serial_complete_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import string
2222
from sys import stdout
2323

24-
class SerialCompleteTest():
24+
class SerialCompleteTest(object):
2525

2626
def test(self, selftest):
2727
strip_chars = string.whitespace + "\0"

tools/host_tests/serial_nc_rx_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import string
2222
from sys import stdout
2323

24-
class SerialNCRXTest():
24+
class SerialNCRXTest(object):
2525

2626
def test(self, selftest):
2727
selftest.mbed.flush();

tools/host_tests/serial_nc_tx_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import string
2222
from sys import stdout
2323

24-
class SerialNCTXTest():
24+
class SerialNCTXTest(object):
2525

2626
def test(self, selftest):
2727
selftest.mbed.flush();

tools/host_tests/stdio_auto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import random
2020
from time import time
2121

22-
class StdioTest():
22+
class StdioTest(object):
2323
PATTERN_INT_VALUE = "Your value was: (-?\d+)"
2424
re_detect_int_value = re.compile(PATTERN_INT_VALUE)
2525

tools/host_tests/tcpecho_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
N_PACKETS = 5000
2727
TOT_BITS = float(LEN_PACKET * N_PACKETS * 8) * 2
2828
MEGA = float(1024 * 1024)
29-
UPDATE_STEP = (N_PACKETS/10)
29+
UPDATE_STEP = (N_PACKETS // 10)
3030

31-
class TCP_EchoClient:
31+
class TCP_EchoClient(object):
3232
def __init__(self, host):
3333
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3434
self.s.connect((host, ECHO_PORT))
@@ -44,10 +44,10 @@ def __packet(self):
4444
def test(self):
4545
start = time()
4646
for i in range(N_PACKETS):
47-
if (i % UPDATE_STEP) == 0: print '%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.)
47+
if (i % UPDATE_STEP) == 0: print('%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.))
4848
self.__packet()
4949
t = time() - start
50-
print 'Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA)
50+
print('Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA))
5151

5252
def __del__(self):
5353
self.s.close()

tools/host_tests/tcpecho_client_auto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ def handle(self):
3535
if not data: break
3636
self.request.sendall(data)
3737
if '{{end}}' in str(data):
38-
print
38+
print()
3939
print(str(data))
4040
else:
4141
if not count % 10:
4242
sys.stdout.write('.')
4343
count += 1
4444
stdout.flush()
4545

46-
class TCPEchoClientTest():
46+
class TCPEchoClientTest(object):
4747
def send_server_ip_port(self, selftest, ip_address, port_no):
4848
""" Set up network host. Reset target and and send server IP via serial to Mbed
4949
"""

0 commit comments

Comments
 (0)