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 @@
from urllib2 import urlopen, URLError
try:
from urllib2 import urlopen, URLError
except ImportError:
from urllib.request import urlopen, URLError
from bs4 import BeautifulSoup
from os.path import join, dirname, basename
from os import makedirs
from errno import EEXIST
from threading import Thread
from Queue import Queue
try:
from Queue import Queue
except ImportError:
from queue import Queue
from re import compile, sub
from sys import stderr, stdout
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
limitations under the License.
"""
from __future__ import print_function
import re
import tempfile
import datetime
import uuid
from types import ListType
from shutil import rmtree
from os.path import join, exists, dirname, basename, abspath, normpath, splitext
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
"""
# Convert src_paths to a list if needed
if type(src_paths) != ListType:
if not isinstance(src_paths, list):
src_paths = [src_paths]
# 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
if inc_dirs:
if type(inc_dirs) == ListType:
if isinstance(inc_dirs, list):
resources.inc_dirs.extend(inc_dirs)
else:
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
if type(src_paths) != ListType:
if not isinstance(src_paths, list):
src_paths = [src_paths]
# Extend src_paths wiht libraries_paths
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',
real_stats_depth, None,
getattr(toolchain.target, 'device_name', None))
print memap_bars
print(memap_bars)
else:
print memap_table
print(memap_table)
# Write output to file in JSON format
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
if type(src_paths) != ListType:
if not isinstance(src_paths, list):
src_paths = [src_paths]
# Build path
@ -782,7 +782,7 @@ def build_lib(lib_id, target, toolchain_name, verbose=False,
inc_dirs = lib.inc_dirs
inc_dirs_ext = lib.inc_dirs_ext
if type(src_paths) != ListType:
if not isinstance(src_paths, list):
src_paths = [src_paths]
# The first path will give the name to the library

View File

@ -18,6 +18,7 @@ limitations under the License.
TEST BUILD & RUN
"""
from __future__ import print_function
import sys
import json
from time import sleep
@ -186,20 +187,20 @@ if __name__ == '__main__':
# Only prints matrix of supported toolchains
if options.supported_toolchains:
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":
toolchain_list = mcu_toolchain_list()
# Only print the lines that matter
for line in toolchain_list.split("\n"):
if not "mbed" in line:
print line
print(line)
elif options.supported_toolchains == "targets":
print mcu_target_list()
print(mcu_target_list())
exit(0)
# Print available tests in order and exit
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()
# 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 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()
# Linking with extra libraries
@ -294,7 +295,7 @@ if __name__ == '__main__':
options,
toolchain),
stats_depth=options.stats_depth)
print 'Image: %s'% bin_file
print('Image: %s'% bin_file)
if options.disk:
# Simple copy to the mbed disk
@ -328,16 +329,16 @@ if __name__ == '__main__':
sys.stdout.write(c)
sys.stdout.flush()
except KeyboardInterrupt, e:
print "\n[CTRL+c] exit"
except KeyboardInterrupt as e:
print("\n[CTRL+c] exit")
except NotSupportedException as e:
print "\nCould not compile for %s: %s" % (mcu, str(e))
except Exception,e:
print("\nCould not compile for %s: %s" % (mcu, str(e)))
except Exception as e:
if options.verbose:
import traceback
traceback.print_exc(file=sys.stdout)
else:
print "[ERROR] %s" % str(e)
print("[ERROR] %s" % str(e))
sys.exit(1)
if options.build_data:

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python
"""Memory Map File Analyser for ARM mbed"""
from __future__ import print_function
from abc import abstractmethod, ABCMeta
from sys import stdout, exit, argv
@ -132,7 +133,7 @@ class _GccParser(_Parser):
return join('[lib]', test_re_obj_name.group(2),
test_re_obj_name.group(3))
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]'
def parse_section(self, line):
@ -217,7 +218,7 @@ class _ArmccParser(_Parser):
if is_obj:
return join('[lib]', basename(is_obj.group(1)), is_obj.group(3))
else:
print "Malformed input found when parsing ARMCC map: %s" % line
print("Malformed input found when parsing ARMCC map: %s" % line)
return '[misc]'
def parse_section(self, line):
@ -246,8 +247,8 @@ class _ArmccParser(_Parser):
elif test_re.group(3) == 'Code':
section = '.text'
else:
print "Malformed input found when parsing armcc map: %s, %r" %\
(line, test_re.groups())
print("Malformed input found when parsing armcc map: %s, %r"
% (line, test_re.groups()))
return ["", 0, ""]
@ -352,7 +353,7 @@ class _IarParser(_Parser):
elif test_re.group(2) == 'inited':
section = '.data'
else:
print "Malformed input found when parsing IAR map: %s" % line
print("Malformed input found when parsing IAR map: %s" % line)
return ["", 0, ""]
# lookup object in dictionary and return module name
@ -528,7 +529,7 @@ class MemapParser(object):
else:
file_desc = stdout
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
to_call = {'json': self.generate_json,
@ -736,7 +737,7 @@ class MemapParser(object):
return True
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
def main():
@ -803,7 +804,7 @@ def main():
returned_string = memap.generate_output(args.export, depth)
if args.export == 'table' and returned_string:
print returned_string
print(returned_string)
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
limitations under the License.
"""
from __future__ import print_function
from os import getenv
from os.path import join, abspath, dirname, exists
import logging
@ -81,7 +81,8 @@ for _n in _ENV_PATHS:
if exists(getenv('MBED_'+_n)):
globals()[_n] = getenv('MBED_'+_n)
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
limitations under the License.
"""
from __future__ import print_function
import os
import binascii
@ -65,7 +66,7 @@ def cached(func):
"""
def wrapper(*args, **kwargs):
"""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)
return CACHES[(func.__name__, args)]
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 k, v in json_file_to_dict(extra_target).iteritems():
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:
targets[k] = v
@ -258,19 +260,14 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
starting_value = None
for tgt in self.resolution_order:
data = tdata[tgt[0]]
if data.has_key(attrname):
starting_value = data[attrname]
break
try:
return data[attrname]
except KeyError:
pass
else: # Attribute not found
raise AttributeError(
"Attribute '%s' not found in target '%s'"
% (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):
""" 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")
target = binf + ".tmp"
if not os.path.exists(loader):
print "Can't find bootloader binary: " + loader
print("Can't find bootloader binary: " + loader)
return
outbin = open(target, 'w+b')
part = open(loader, 'rb')

View File

@ -15,15 +15,10 @@ See the License for the specific language governing permissions and
limitations under the License.
"""
from tools.paths import *
from tools.data.support import *
from tools.data.support import DEFAULT_SUPPORT, CORTEX_ARM_SUPPORT
from argparse import ArgumentTypeError
from utils import columnate
try:
import tools.private_settings as ps
except:
ps = object()
TEST_CMSIS_LIB = join(TEST_DIR, "cmsis", "lib")
TEST_MBED_LIB = join(TEST_DIR, "mbed", "env")
@ -917,8 +912,7 @@ def test_known(string):
def test_name_known(string):
if string not in TEST_MAP.keys() and \
(getattr(ps, "test_alias", None) is None or \
ps.test_alias.get(string, "") not in TEST_MAP.keys()):
(getattr(ps, "test_alias", None) is None):
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

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
limitations under the License.
"""
from __future__ import print_function
import re
import sys
from os import stat, walk, getcwd, sep, remove
from copy import copy
from time import time, sleep
from types import ListType
from shutil import copyfile
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir, normcase
from itertools import chain
@ -472,7 +472,7 @@ class mbedToolchain:
if msg:
if not silent:
print msg
print(msg)
self.output += msg + "\n"
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"
msg = '[%(severity)s] %(target_name)s::%(toolchain_name)s::%(file)s@%(line)s: %(message)s' % event
if not silent:
print msg
print(msg)
self.output += msg + "\n"
elif event['type'] == 'progress':
@ -793,7 +793,8 @@ class mbedToolchain:
def copy_files(self, files_paths, trg_path, resources=None, rel_path=None):
# 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:
if source is None:
@ -982,7 +983,7 @@ class mbedToolchain:
res['command']
])
objects.append(result['object'])
except ToolException, err:
except ToolException as err:
if p._taskqueue.queue:
p._taskqueue.queue.clear()
sleep(0.5)
@ -1010,7 +1011,7 @@ class mbedToolchain:
dep_path = base + '.d'
try:
deps = self.parse_dependencies(dep_path) if (exists(dep_path)) else []
except IOError, IndexError:
except (IOError, IndexError):
deps = []
config_file = ([self.config.app_config_location]
if self.config.app_config_location else [])
@ -1177,7 +1178,7 @@ class mbedToolchain:
# ANY CHANGE OF PARAMETERS OR RETURN VALUES WILL BREAK COMPATIBILITY
def debug(self, message):
if self.VERBOSE:
if type(message) is ListType:
if isinstance(message, list):
message = ' '.join(message)
message = "[DEBUG] " + 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
limitations under the License.
"""
from __future__ import print_function
import sys
import inspect
import os
@ -30,6 +31,11 @@ from collections import OrderedDict
import logging
from intelhex import IntelHex
try:
unicode
except NameError:
unicode = str
def remove_if_in(lst, thing):
if thing in lst:
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"""
text = command if shell else ' '.join(command)
if verbose:
print text
print(text)
return_code = call(command, shell=shell, cwd=cwd)
if check and return_code != 0:
raise Exception('ERROR %d: "%s"' % (return_code, text))
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:
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)
_stdout, _stderr = process.communicate()
except OSError:
print "[OS ERROR] Command: "+(' '.join(command))
print("[OS ERROR] Command: "+(' '.join(command)))
raise
return _stdout, _stderr, process.returncode
@ -318,13 +324,13 @@ def check_required_modules(required_modules, verbose=True):
except ImportError as exc:
not_installed_modules.append(module_name)
if verbose:
print "Error: %s" % exc
print("Error: %s" % exc)
if verbose:
if not_installed_modules:
print ("Warning: Module(s) %s not installed. Please install " + \
"required module(s) before using this script.")\
% (', '.join(not_installed_modules))
print("Warning: Module(s) %s not installed. Please install "
"required module(s) before using this script."
% (', '.join(not_installed_modules)))
if not_installed_modules:
return False
@ -342,11 +348,11 @@ def dict_to_ascii(dictionary):
"""
if isinstance(dictionary, dict):
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):
return [dict_to_ascii(element) for element in dictionary]
elif isinstance(dictionary, unicode):
return dictionary.encode('ascii')
return dictionary.encode('ascii').decode()
else:
return dictionary