Python2+3: Things import

pull/5848/head
Jimmy Brisson 2018-01-11 15:05:46 -06:00
parent 964e6e74fb
commit 68737f2762
9 changed files with 74 additions and 67 deletions

View File

@ -1,10 +1,16 @@
try:
from urllib2 import urlopen, URLError from urllib2 import urlopen, URLError
except ImportError:
from urllib.request import urlopen, URLError
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from os.path import join, dirname, basename from os.path import join, dirname, basename
from os import makedirs from os import makedirs
from errno import EEXIST from errno import EEXIST
from threading import Thread from threading import Thread
try:
from Queue import Queue from Queue import Queue
except ImportError:
from queue import Queue
from re import compile, sub from re import compile, sub
from sys import stderr, stdout from sys import stderr, stdout
from itertools import takewhile from itertools import takewhile

View File

@ -14,12 +14,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from __future__ import print_function
import re import re
import tempfile import tempfile
import datetime import datetime
import uuid import uuid
from types import ListType
from shutil import rmtree from shutil import rmtree
from os.path import join, exists, dirname, basename, abspath, normpath, splitext from os.path import join, exists, dirname, basename, abspath, normpath, splitext
from os.path import relpath from os.path import relpath
@ -124,7 +124,7 @@ def get_config(src_paths, target, toolchain_name):
toolchain_name - the string that identifies the build tools toolchain_name - the string that identifies the build tools
""" """
# Convert src_paths to a list if needed # Convert src_paths to a list if needed
if type(src_paths) != ListType: if not isinstance(src_paths, list):
src_paths = [src_paths] src_paths = [src_paths]
# Pass all params to the unified prepare_resources() # Pass all params to the unified prepare_resources()
@ -399,7 +399,7 @@ def scan_resources(src_paths, toolchain, dependencies_paths=None,
# Add additional include directories if passed # Add additional include directories if passed
if inc_dirs: if inc_dirs:
if type(inc_dirs) == ListType: if isinstance(inc_dirs, list):
resources.inc_dirs.extend(inc_dirs) resources.inc_dirs.extend(inc_dirs)
else: else:
resources.inc_dirs.append(inc_dirs) resources.inc_dirs.append(inc_dirs)
@ -457,7 +457,7 @@ def build_project(src_paths, build_path, target, toolchain_name,
""" """
# Convert src_path to a list if needed # Convert src_path to a list if needed
if type(src_paths) != ListType: if not isinstance(src_paths, list):
src_paths = [src_paths] src_paths = [src_paths]
# Extend src_paths wiht libraries_paths # Extend src_paths wiht libraries_paths
if libraries_paths is not None: if libraries_paths is not None:
@ -527,9 +527,9 @@ def build_project(src_paths, build_path, target, toolchain_name,
memap_bars = memap_instance.generate_output('bars', memap_bars = memap_instance.generate_output('bars',
real_stats_depth, None, real_stats_depth, None,
getattr(toolchain.target, 'device_name', None)) getattr(toolchain.target, 'device_name', None))
print memap_bars print(memap_bars)
else: else:
print memap_table print(memap_table)
# Write output to file in JSON format # Write output to file in JSON format
map_out = join(build_path, name + "_map.json") map_out = join(build_path, name + "_map.json")
@ -613,7 +613,7 @@ def build_library(src_paths, build_path, target, toolchain_name,
""" """
# Convert src_path to a list if needed # Convert src_path to a list if needed
if type(src_paths) != ListType: if not isinstance(src_paths, list):
src_paths = [src_paths] src_paths = [src_paths]
# Build path # Build path
@ -782,7 +782,7 @@ def build_lib(lib_id, target, toolchain_name, verbose=False,
inc_dirs = lib.inc_dirs inc_dirs = lib.inc_dirs
inc_dirs_ext = lib.inc_dirs_ext inc_dirs_ext = lib.inc_dirs_ext
if type(src_paths) != ListType: if not isinstance(src_paths, list):
src_paths = [src_paths] src_paths = [src_paths]
# The first path will give the name to the library # The first path will give the name to the library

View File

@ -18,6 +18,7 @@ limitations under the License.
TEST BUILD & RUN TEST BUILD & RUN
""" """
from __future__ import print_function
import sys import sys
import json import json
from time import sleep from time import sleep
@ -186,20 +187,20 @@ if __name__ == '__main__':
# Only prints matrix of supported toolchains # Only prints matrix of supported toolchains
if options.supported_toolchains: if options.supported_toolchains:
if options.supported_toolchains == "matrix": if options.supported_toolchains == "matrix":
print mcu_toolchain_matrix(platform_filter=options.general_filter_regex) print(mcu_toolchain_matrix(platform_filter=options.general_filter_regex))
elif options.supported_toolchains == "toolchains": elif options.supported_toolchains == "toolchains":
toolchain_list = mcu_toolchain_list() toolchain_list = mcu_toolchain_list()
# Only print the lines that matter # Only print the lines that matter
for line in toolchain_list.split("\n"): for line in toolchain_list.split("\n"):
if not "mbed" in line: if not "mbed" in line:
print line print(line)
elif options.supported_toolchains == "targets": elif options.supported_toolchains == "targets":
print mcu_target_list() print(mcu_target_list())
exit(0) exit(0)
# Print available tests in order and exit # Print available tests in order and exit
if options.list_tests is True: if options.list_tests is True:
print '\n'.join(map(str, sorted(TEST_MAP.values()))) print('\n'.join(map(str, sorted(TEST_MAP.values()))))
sys.exit() sys.exit()
# force program to "0" if a source dir is specified # force program to "0" if a source dir is specified
@ -259,7 +260,7 @@ if __name__ == '__main__':
if options.extra is not None: test.extra_files = options.extra if options.extra is not None: test.extra_files = options.extra
if not test.is_supported(mcu, toolchain): if not test.is_supported(mcu, toolchain):
print 'The selected test is not supported on target %s with toolchain %s' % (mcu, toolchain) print('The selected test is not supported on target %s with toolchain %s' % (mcu, toolchain))
sys.exit() sys.exit()
# Linking with extra libraries # Linking with extra libraries
@ -294,7 +295,7 @@ if __name__ == '__main__':
options, options,
toolchain), toolchain),
stats_depth=options.stats_depth) stats_depth=options.stats_depth)
print 'Image: %s'% bin_file print('Image: %s'% bin_file)
if options.disk: if options.disk:
# Simple copy to the mbed disk # Simple copy to the mbed disk
@ -328,16 +329,16 @@ if __name__ == '__main__':
sys.stdout.write(c) sys.stdout.write(c)
sys.stdout.flush() sys.stdout.flush()
except KeyboardInterrupt, e: except KeyboardInterrupt as e:
print "\n[CTRL+c] exit" print("\n[CTRL+c] exit")
except NotSupportedException as e: except NotSupportedException as e:
print "\nCould not compile for %s: %s" % (mcu, str(e)) print("\nCould not compile for %s: %s" % (mcu, str(e)))
except Exception,e: except Exception as e:
if options.verbose: if options.verbose:
import traceback import traceback
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
else: else:
print "[ERROR] %s" % str(e) print("[ERROR] %s" % str(e))
sys.exit(1) sys.exit(1)
if options.build_data: if options.build_data:

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
"""Memory Map File Analyser for ARM mbed""" """Memory Map File Analyser for ARM mbed"""
from __future__ import print_function
from abc import abstractmethod, ABCMeta from abc import abstractmethod, ABCMeta
from sys import stdout, exit, argv from sys import stdout, exit, argv
@ -132,7 +133,7 @@ class _GccParser(_Parser):
return join('[lib]', test_re_obj_name.group(2), return join('[lib]', test_re_obj_name.group(2),
test_re_obj_name.group(3)) test_re_obj_name.group(3))
else: else:
print "Unknown object name found in GCC map file: %s" % line print("Unknown object name found in GCC map file: %s" % line)
return '[misc]' return '[misc]'
def parse_section(self, line): def parse_section(self, line):
@ -217,7 +218,7 @@ class _ArmccParser(_Parser):
if is_obj: if is_obj:
return join('[lib]', basename(is_obj.group(1)), is_obj.group(3)) return join('[lib]', basename(is_obj.group(1)), is_obj.group(3))
else: else:
print "Malformed input found when parsing ARMCC map: %s" % line print("Malformed input found when parsing ARMCC map: %s" % line)
return '[misc]' return '[misc]'
def parse_section(self, line): def parse_section(self, line):
@ -246,8 +247,8 @@ class _ArmccParser(_Parser):
elif test_re.group(3) == 'Code': elif test_re.group(3) == 'Code':
section = '.text' section = '.text'
else: else:
print "Malformed input found when parsing armcc map: %s, %r" %\ print("Malformed input found when parsing armcc map: %s, %r"
(line, test_re.groups()) % (line, test_re.groups()))
return ["", 0, ""] return ["", 0, ""]
@ -352,7 +353,7 @@ class _IarParser(_Parser):
elif test_re.group(2) == 'inited': elif test_re.group(2) == 'inited':
section = '.data' section = '.data'
else: else:
print "Malformed input found when parsing IAR map: %s" % line print("Malformed input found when parsing IAR map: %s" % line)
return ["", 0, ""] return ["", 0, ""]
# lookup object in dictionary and return module name # lookup object in dictionary and return module name
@ -528,7 +529,7 @@ class MemapParser(object):
else: else:
file_desc = stdout file_desc = stdout
except IOError as error: except IOError as error:
print "I/O error({0}): {1}".format(error.errno, error.strerror) print("I/O error({0}): {1}".format(error.errno, error.strerror))
return False return False
to_call = {'json': self.generate_json, to_call = {'json': self.generate_json,
@ -736,7 +737,7 @@ class MemapParser(object):
return True return True
except IOError as error: except IOError as error:
print "I/O error({0}): {1}".format(error.errno, error.strerror) print("I/O error({0}): {1}".format(error.errno, error.strerror))
return False return False
def main(): def main():
@ -803,7 +804,7 @@ def main():
returned_string = memap.generate_output(args.export, depth) returned_string = memap.generate_output(args.export, depth)
if args.export == 'table' and returned_string: if args.export == 'table' and returned_string:
print returned_string print(returned_string)
exit(0) exit(0)

View File

@ -14,7 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from __future__ import print_function
from os import getenv from os import getenv
from os.path import join, abspath, dirname, exists from os.path import join, abspath, dirname, exists
import logging import logging
@ -81,7 +81,8 @@ for _n in _ENV_PATHS:
if exists(getenv('MBED_'+_n)): if exists(getenv('MBED_'+_n)):
globals()[_n] = getenv('MBED_'+_n) globals()[_n] = getenv('MBED_'+_n)
else: else:
print "WARNING: MBED_%s set as environment variable but doesn't exist" % _n print("WARNING: MBED_%s set as environment variable but doesn't"
" exist" % _n)
############################################################################## ##############################################################################

View File

@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from __future__ import print_function
import os import os
import binascii import binascii
@ -65,7 +66,7 @@ def cached(func):
""" """
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
"""The wrapped function itself""" """The wrapped function itself"""
if not CACHES.has_key((func.__name__, args)): if (func.__name__, args) not in CACHES:
CACHES[(func.__name__, args)] = func(*args, **kwargs) CACHES[(func.__name__, args)] = func(*args, **kwargs)
return CACHES[(func.__name__, args)] return CACHES[(func.__name__, args)]
return wrapper return wrapper
@ -143,7 +144,8 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
for extra_target in Target.__extra_target_json_files: for extra_target in Target.__extra_target_json_files:
for k, v in json_file_to_dict(extra_target).iteritems(): for k, v in json_file_to_dict(extra_target).iteritems():
if k in targets: if k in targets:
print 'WARNING: Custom target "%s" cannot replace existing target.' % k print('WARNING: Custom target "%s" cannot replace existing '
'target.' % k)
else: else:
targets[k] = v targets[k] = v
@ -258,19 +260,14 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
starting_value = None starting_value = None
for tgt in self.resolution_order: for tgt in self.resolution_order:
data = tdata[tgt[0]] data = tdata[tgt[0]]
if data.has_key(attrname): try:
starting_value = data[attrname] return data[attrname]
break except KeyError:
pass
else: # Attribute not found else: # Attribute not found
raise AttributeError( raise AttributeError(
"Attribute '%s' not found in target '%s'" "Attribute '%s' not found in target '%s'"
% (attrname, self.name)) % (attrname, self.name))
# 'progen' needs the full path to the template (the path in JSON is
# relative to tools/export)
if attrname == "progen":
return self.__add_paths_to_progen(starting_value)
else:
return starting_value
def __getattr__(self, attrname): def __getattr__(self, attrname):
""" Return the value of an attribute. This function only computes the """ Return the value of an attribute. This function only computes the
@ -427,7 +424,7 @@ class MTSCode(object):
loader = os.path.join(TOOLS_BOOTLOADERS, target_name, "bootloader.bin") loader = os.path.join(TOOLS_BOOTLOADERS, target_name, "bootloader.bin")
target = binf + ".tmp" target = binf + ".tmp"
if not os.path.exists(loader): if not os.path.exists(loader):
print "Can't find bootloader binary: " + loader print("Can't find bootloader binary: " + loader)
return return
outbin = open(target, 'w+b') outbin = open(target, 'w+b')
part = open(loader, 'rb') part = open(loader, 'rb')

View File

@ -15,15 +15,10 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from tools.paths import * from tools.paths import *
from tools.data.support import * from tools.data.support import DEFAULT_SUPPORT, CORTEX_ARM_SUPPORT
from argparse import ArgumentTypeError from argparse import ArgumentTypeError
from utils import columnate from utils import columnate
try:
import tools.private_settings as ps
except:
ps = object()
TEST_CMSIS_LIB = join(TEST_DIR, "cmsis", "lib") TEST_CMSIS_LIB = join(TEST_DIR, "cmsis", "lib")
TEST_MBED_LIB = join(TEST_DIR, "mbed", "env") TEST_MBED_LIB = join(TEST_DIR, "mbed", "env")
@ -917,8 +912,7 @@ def test_known(string):
def test_name_known(string): def test_name_known(string):
if string not in TEST_MAP.keys() and \ if string not in TEST_MAP.keys() and \
(getattr(ps, "test_alias", None) is None or \ (getattr(ps, "test_alias", None) is None):
ps.test_alias.get(string, "") not in TEST_MAP.keys()):
raise ArgumentTypeError("Program with name '{0}' not found. Supported tests are: \n{1}".format(string, columnate([t['id'] for t in TESTS]))) raise ArgumentTypeError("Program with name '{0}' not found. Supported tests are: \n{1}".format(string, columnate([t['id'] for t in TESTS])))
return TEST_MAP[string].n return TEST_MAP[string].n

View File

@ -14,13 +14,13 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from __future__ import print_function
import re import re
import sys import sys
from os import stat, walk, getcwd, sep, remove from os import stat, walk, getcwd, sep, remove
from copy import copy from copy import copy
from time import time, sleep from time import time, sleep
from types import ListType
from shutil import copyfile from shutil import copyfile
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir, normcase from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir, normcase
from itertools import chain from itertools import chain
@ -472,7 +472,7 @@ class mbedToolchain:
if msg: if msg:
if not silent: if not silent:
print msg print(msg)
self.output += msg + "\n" self.output += msg + "\n"
def print_notify_verbose(self, event, silent=False): def print_notify_verbose(self, event, silent=False):
@ -489,7 +489,7 @@ class mbedToolchain:
event['toolchain_name'] = event['toolchain_name'].upper() if event['toolchain_name'] else "Unknown" event['toolchain_name'] = event['toolchain_name'].upper() if event['toolchain_name'] else "Unknown"
msg = '[%(severity)s] %(target_name)s::%(toolchain_name)s::%(file)s@%(line)s: %(message)s' % event msg = '[%(severity)s] %(target_name)s::%(toolchain_name)s::%(file)s@%(line)s: %(message)s' % event
if not silent: if not silent:
print msg print(msg)
self.output += msg + "\n" self.output += msg + "\n"
elif event['type'] == 'progress': elif event['type'] == 'progress':
@ -793,7 +793,8 @@ class mbedToolchain:
def copy_files(self, files_paths, trg_path, resources=None, rel_path=None): def copy_files(self, files_paths, trg_path, resources=None, rel_path=None):
# Handle a single file # Handle a single file
if type(files_paths) != ListType: files_paths = [files_paths] if isinstance(files_paths, list):
files_paths = [files_paths]
for source in files_paths: for source in files_paths:
if source is None: if source is None:
@ -982,7 +983,7 @@ class mbedToolchain:
res['command'] res['command']
]) ])
objects.append(result['object']) objects.append(result['object'])
except ToolException, err: except ToolException as err:
if p._taskqueue.queue: if p._taskqueue.queue:
p._taskqueue.queue.clear() p._taskqueue.queue.clear()
sleep(0.5) sleep(0.5)
@ -1010,7 +1011,7 @@ class mbedToolchain:
dep_path = base + '.d' dep_path = base + '.d'
try: try:
deps = self.parse_dependencies(dep_path) if (exists(dep_path)) else [] deps = self.parse_dependencies(dep_path) if (exists(dep_path)) else []
except IOError, IndexError: except (IOError, IndexError):
deps = [] deps = []
config_file = ([self.config.app_config_location] config_file = ([self.config.app_config_location]
if self.config.app_config_location else []) if self.config.app_config_location else [])
@ -1177,7 +1178,7 @@ class mbedToolchain:
# ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY # ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
def debug(self, message): def debug(self, message):
if self.VERBOSE: if self.VERBOSE:
if type(message) is ListType: if isinstance(message, list):
message = ' '.join(message) message = ' '.join(message)
message = "[DEBUG] " + message message = "[DEBUG] " + message
self.notify({'type': 'debug', 'message': message}) self.notify({'type': 'debug', 'message': message})

View File

@ -14,6 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from __future__ import print_function
import sys import sys
import inspect import inspect
import os import os
@ -30,6 +31,11 @@ from collections import OrderedDict
import logging import logging
from intelhex import IntelHex from intelhex import IntelHex
try:
unicode
except NameError:
unicode = str
def remove_if_in(lst, thing): def remove_if_in(lst, thing):
if thing in lst: if thing in lst:
lst.remove(thing) lst.remove(thing)
@ -66,14 +72,14 @@ def cmd(command, check=True, verbose=False, shell=False, cwd=None):
"""A wrapper to run a command as a blocking job""" """A wrapper to run a command as a blocking job"""
text = command if shell else ' '.join(command) text = command if shell else ' '.join(command)
if verbose: if verbose:
print text print(text)
return_code = call(command, shell=shell, cwd=cwd) return_code = call(command, shell=shell, cwd=cwd)
if check and return_code != 0: if check and return_code != 0:
raise Exception('ERROR %d: "%s"' % (return_code, text)) raise Exception('ERROR %d: "%s"' % (return_code, text))
def run_cmd(command, work_dir=None, chroot=None, redirect=False): def run_cmd(command, work_dir=None, chroot=None, redirect=False):
"""Run a command in the forground """Run a command in the foreground
Positional arguments: Positional arguments:
command - the command to run command - the command to run
@ -100,7 +106,7 @@ def run_cmd(command, work_dir=None, chroot=None, redirect=False):
stderr=STDOUT if redirect else PIPE, cwd=work_dir) stderr=STDOUT if redirect else PIPE, cwd=work_dir)
_stdout, _stderr = process.communicate() _stdout, _stderr = process.communicate()
except OSError: except OSError:
print "[OS ERROR] Command: "+(' '.join(command)) print("[OS ERROR] Command: "+(' '.join(command)))
raise raise
return _stdout, _stderr, process.returncode return _stdout, _stderr, process.returncode
@ -318,13 +324,13 @@ def check_required_modules(required_modules, verbose=True):
except ImportError as exc: except ImportError as exc:
not_installed_modules.append(module_name) not_installed_modules.append(module_name)
if verbose: if verbose:
print "Error: %s" % exc print("Error: %s" % exc)
if verbose: if verbose:
if not_installed_modules: if not_installed_modules:
print ("Warning: Module(s) %s not installed. Please install " + \ print("Warning: Module(s) %s not installed. Please install "
"required module(s) before using this script.")\ "required module(s) before using this script."
% (', '.join(not_installed_modules)) % (', '.join(not_installed_modules)))
if not_installed_modules: if not_installed_modules:
return False return False
@ -342,11 +348,11 @@ def dict_to_ascii(dictionary):
""" """
if isinstance(dictionary, dict): if isinstance(dictionary, dict):
return OrderedDict([(dict_to_ascii(key), dict_to_ascii(value)) return OrderedDict([(dict_to_ascii(key), dict_to_ascii(value))
for key, value in dictionary.iteritems()]) for key, value in dictionary.items()])
elif isinstance(dictionary, list): elif isinstance(dictionary, list):
return [dict_to_ascii(element) for element in dictionary] return [dict_to_ascii(element) for element in dictionary]
elif isinstance(dictionary, unicode): elif isinstance(dictionary, unicode):
return dictionary.encode('ascii') return dictionary.encode('ascii').decode()
else: else:
return dictionary return dictionary