Skip to content

Commit

Permalink
Merge pull request #11921 from madchutney/tools/py3-fixes
Browse files Browse the repository at this point in the history
Updates to tools for Python 3 compatibility
  • Loading branch information
0xc0170 authored Nov 27, 2019
2 parents 9974d83 + 23b12f4 commit 57f9a1e
Show file tree
Hide file tree
Showing 44 changed files with 144 additions and 123 deletions.
4 changes: 2 additions & 2 deletions tools/device_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ def inner(options):
# Get the currently in-use API key (may come from environment or
# configuration files, which is handled by the cloud SDK)
api_key_value = accounts.config.get("api_key")
api_key = accounts.list_api_keys(
api_key = next(accounts.list_api_keys(
filter={
"key": api_key_value
}
).next()
))
certificates_owned = list(certs.list_certificates())
dev_cert_info = None
for certif in certificates_owned:
Expand Down
4 changes: 2 additions & 2 deletions tools/export/exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from tools.targets import TARGET_MAP
from tools.utils import mkdir
from tools.resources import FileType, FileRef
from future.utils import with_metaclass

"""Just a template for subclassing"""

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

class Exporter(object):
class Exporter(with_metaclass(ABCMeta, object)):
"""Exporter base class
This class is meant to be extended by individual exporters, and provides a
few helper methods for implementing an exporter with either jinja2 or
progen.
"""
__metaclass__ = ABCMeta
TEMPLATE_DIR = dirname(__file__)
DOT_IN_RELATIVE_PATH = False
NAME = None
Expand Down
2 changes: 1 addition & 1 deletion tools/flash_algo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def format_algo_data(self, spaces, group_size, fmt):
blob = self.algo_data[:]
pad_size = 0 if len(blob) % 4 == 0 else 4 - len(blob) % 4
blob = blob + "\x00" * pad_size
integer_list = struct.unpack("<" + "L" * (len(blob) / 4), blob)
integer_list = struct.unpack("<" + "L" * (len(blob) // 4), blob)
line_list = []
for pos in range(0, len(integer_list), group_size):
group = ["0x%08x" % value for value in
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/default_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from sys import stdout

class DefaultAuto():
class DefaultAuto(object):
""" Simple, basic host test's test runner waiting for serial port
output from MUT, no supervision over test running in MUT is executed.
"""
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/detect_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import re

class DetectPlatformTest():
class DetectPlatformTest(object):
PATTERN_MICRO_NAME = "Target '(\w+)'"
re_detect_micro_name = re.compile(PATTERN_MICRO_NAME)

Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/dev_null_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
limitations under the License.
"""

class DevNullTest():
class DevNullTest(object):

def check_readline(self, selftest, text):
""" Reads line from serial port and checks if text was part of read string
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import uuid
from sys import stdout

class EchoTest():
class EchoTest(object):

# Test parameters
TEST_SERIAL_BAUDRATE = 115200
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/echo_flow_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from host_test import Test
from .host_test import Test


class EchoTest(Test):
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/hello_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
limitations under the License.
"""

class HelloTest():
class HelloTest(object):
HELLO_WORLD = "Hello World"

def test(self, selftest):
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/host_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
limitations under the License.
"""

class HostRegistry:
class HostRegistry(object):
""" Class stores registry with host tests and objects representing them
"""
HOST_TESTS = {} # host_test_name -> host_test_ojbect
Expand Down
24 changes: 12 additions & 12 deletions tools/host_tests/host_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# Check if 'serial' module is installed
try:
from serial import Serial
except ImportError, e:
print "Error: Can't import 'serial' module: %s"% e
except ImportError as e:
print("Error: Can't import 'serial' module: %s"% e)
exit(-1)

import os
Expand All @@ -29,7 +29,7 @@
from time import sleep, time
from optparse import OptionParser

import host_tests_plugins
from . import host_tests_plugins

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


class Mbed:
class Mbed(object):
""" Base class for a host driven test
"""
def __init__(self):
Expand Down Expand Up @@ -117,7 +117,7 @@ def __init__(self):
self.serial_timeout = 1

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

def init_serial_params(self, serial_baud=9600, serial_timeout=1):
""" Initialize port parameters.
Expand Down Expand Up @@ -183,11 +183,11 @@ def pool_for_serial_init(self, serial_baud, serial_timeout, pooling_loops=40, in
stdout.write('.')
stdout.flush()
else:
print "...port ready!"
print("...port ready!")
result = True
break
if not result and last_error:
print last_error
print(last_error)
return result

def set_serial_timeout(self, timeout):
Expand Down Expand Up @@ -221,7 +221,7 @@ def serial_readline(self, timeout=5):
c = self.serial.read(1)
result += c
except Exception as e:
print "MBED: %s"% str(e)
print("MBED: %s"% str(e))
result = None
break
if c == '\n':
Expand Down Expand Up @@ -298,7 +298,7 @@ def flush(self):
return result


class HostTestResults:
class HostTestResults(object):
""" Test results set by host tests
"""
def __init__(self):
Expand Down Expand Up @@ -389,8 +389,8 @@ def run(self):
self.print_result(result)
else:
self.notify("HOST: Passive mode...")
except Exception, e:
print str(e)
except Exception as e:
print(str(e))
self.print_result(self.RESULT_ERROR)

def setup(self):
Expand All @@ -406,7 +406,7 @@ def setup(self):
def notify(self, message):
""" On screen notification function
"""
print message
print(message)
stdout.flush()

def print_result(self, result):
Expand Down
14 changes: 8 additions & 6 deletions tools/host_tests/mbedrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
# >myled.write(1)
# >

import serial, urllib2, time
from future import standard_library
standard_library.install_aliases()
import serial, urllib.request, time

# mbed super class
class mbed:
class mbed(object):
def __init__(self):
print("This will work as a demo but no transport mechanism has been selected")

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

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


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

# Serial
class Serial():
class Serial(object):
def __init__(self, this_mbed, tx, rx=""):
self.mbed = this_mbed
if isinstance(tx, str):
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/net_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from host_test import Test, Simple
from .host_test import Test, Simple
from sys import stdout

class NETTest(Simple):
Expand Down
6 changes: 3 additions & 3 deletions tools/host_tests/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from host_test import Test
from mbedrpc import SerialRPC, DigitalOut, DigitalIn, pin
from .host_test import Test
from .mbedrpc import SerialRPC, DigitalOut, DigitalIn, pin


class RpcTest(Test):
Expand All @@ -30,7 +30,7 @@ def test(self):

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

Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/rtc_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import re
from time import time, strftime, gmtime

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

Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/serial_complete_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import string
from sys import stdout

class SerialCompleteTest():
class SerialCompleteTest(object):

def test(self, selftest):
strip_chars = string.whitespace + "\0"
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/serial_nc_rx_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import string
from sys import stdout

class SerialNCRXTest():
class SerialNCRXTest(object):

def test(self, selftest):
selftest.mbed.flush();
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/serial_nc_tx_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import string
from sys import stdout

class SerialNCTXTest():
class SerialNCTXTest(object):

def test(self, selftest):
selftest.mbed.flush();
Expand Down
2 changes: 1 addition & 1 deletion tools/host_tests/stdio_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import random
from time import time

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

Expand Down
8 changes: 4 additions & 4 deletions tools/host_tests/tcpecho_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
N_PACKETS = 5000
TOT_BITS = float(LEN_PACKET * N_PACKETS * 8) * 2
MEGA = float(1024 * 1024)
UPDATE_STEP = (N_PACKETS/10)
UPDATE_STEP = (N_PACKETS // 10)

class TCP_EchoClient:
class TCP_EchoClient(object):
def __init__(self, host):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((host, ECHO_PORT))
Expand All @@ -44,10 +44,10 @@ def __packet(self):
def test(self):
start = time()
for i in range(N_PACKETS):
if (i % UPDATE_STEP) == 0: print '%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.)
if (i % UPDATE_STEP) == 0: print('%.2f%%' % ((float(i)/float(N_PACKETS)) * 100.))
self.__packet()
t = time() - start
print 'Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA)
print('Throughput: (%.2f)Mbits/s' % ((TOT_BITS / t)/MEGA))

def __del__(self):
self.s.close()
Expand Down
4 changes: 2 additions & 2 deletions tools/host_tests/tcpecho_client_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def handle(self):
if not data: break
self.request.sendall(data)
if '{{end}}' in str(data):
print
print()
print(str(data))
else:
if not count % 10:
sys.stdout.write('.')
count += 1
stdout.flush()

class TCPEchoClientTest():
class TCPEchoClientTest(object):
def send_server_ip_port(self, selftest, ip_address, port_no):
""" Set up network host. Reset target and and send server IP via serial to Mbed
"""
Expand Down
Loading

0 comments on commit 57f9a1e

Please sign in to comment.