Skip to content

Commit c679dee

Browse files
authored
Merge pull request #5848 from theotherjimmy/compile-py3
Python2+3: mbed compile, mbed test --compile, python unit tests
2 parents 3b1438b + 2d50a90 commit c679dee

Some content is hidden

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

55 files changed

+544
-446
lines changed

.travis.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
--data @- << DATA\n{
1111
"state": "$0",
1212
"description": "$1",
13-
"context": "travis-ci/$NAME",
13+
"context": "travis-ci/$NAME/$(python --version)",
1414
"target_url": "https://travis-ci.org/$TRAVIS_REPO_SLUG/jobs/$TRAVIS_JOB_ID"
1515
}\nDATA'
1616
@@ -73,6 +73,10 @@ matrix:
7373
7474
- env:
7575
- NAME=tools
76+
python:
77+
- '2.7'
78+
- '3.5'
79+
- '3.6'
7680
install:
7781
# Install dependencies
7882
- sudo apt-get install gcc-arm-embedded
@@ -175,7 +179,7 @@ matrix:
175179
- mkdir BUILD
176180
script:
177181
# Run local mbed 2 testing
178-
- python2 -u tools/build_travis.py --vendor "${NAME#mbed2-}"
182+
- python -u tools/build_travis.py --vendor "${NAME#mbed2-}"
179183
- <<: *mbed-2
180184
env: NAME=mbed2-STM
181185
- <<: *mbed-2
@@ -190,3 +194,8 @@ matrix:
190194
env: NAME=mbed2-NUVOTON
191195
- <<: *mbed-2
192196
env: NAME=mbed2-RENESAS
197+
# Change python version here only because 3x the other jobs does not add any more coverage
198+
python:
199+
- '2.7'
200+
- '3.5'
201+
- '3.6'

tools/arm_pack_manager/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
from urllib2 import urlopen, URLError
1+
try:
2+
from urllib2 import urlopen, URLError
3+
except ImportError:
4+
from urllib.request import urlopen, URLError
25
from bs4 import BeautifulSoup
36
from os.path import join, dirname, basename
47
from os import makedirs
58
from errno import EEXIST
69
from threading import Thread
7-
from Queue import Queue
10+
try:
11+
from Queue import Queue
12+
except ImportError:
13+
from queue import Queue
814
from re import compile, sub
915
from sys import stderr, stdout
1016
from itertools import takewhile

tools/arm_pack_manager/pack_manager.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from __future__ import print_function, division, absolute_import
12
import argparse
23
from os.path import basename
34
from tools.arm_pack_manager import Cache
45
from os.path import basename, join, dirname, exists
56
from os import makedirs
67
from itertools import takewhile
78
from fuzzywuzzy import process
8-
from tools.arm_pack_manager import Cache
9+
from .arm_pack_manager import Cache
910

1011
parser = argparse.ArgumentParser(description='A Handy little utility for keeping your cache of pack files up to date.')
1112
subparsers = parser.add_subparsers(title="Commands")
@@ -69,7 +70,7 @@ def fuzzy_find(matches, urls) :
6970
for key, value in process.extract(match, urls, limit=None) :
7071
choices.setdefault(key, 0)
7172
choices[key] += value
72-
choices = sorted([(v, k) for k, v in choices.iteritems()], reverse=True)
73+
choices = sorted([(v, k) for k, v in choices.items()], reverse=True)
7374
if not choices : return []
7475
elif len(choices) == 1 : return [choices[0][1]]
7576
elif choices[0][0] > choices[1][0] : choices = choices[:1]
@@ -133,12 +134,12 @@ def command_find_part (cache, matches, long=False, intersection=True,
133134
aliases = sum([fuzzy_find([m], cache.aliases.keys()) for m in matches], [])
134135
if print_parts:
135136
for part in choices :
136-
print part
137+
print(part)
137138
if long :
138139
pp.pprint(cache.index[part])
139140
if print_aliases:
140141
for alias in aliases :
141-
print alias
142+
print(alias)
142143
if long :
143144
pp.pprint(cache.index[cache.aliases[alias]])
144145

@@ -155,7 +156,7 @@ def command_dump_parts (cache, out, parts, intersection=False) :
155156
else :
156157
for part in parts :
157158
index.update(dict(cache.find_device(part)))
158-
for n, p in index.iteritems() :
159+
for n, p in index.items() :
159160
try :
160161
if not exists(join(out, dirname(p['algorithm']['file']))) :
161162
makedirs(join(out, dirname(p['algorithm']['file'])))

tools/build.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
1818
LIBRARIES BUILD
1919
"""
20+
from __future__ import print_function, division, absolute_import
21+
2022
import sys
2123
from time import time
2224
from os.path import join, abspath, dirname
@@ -130,7 +132,7 @@
130132

131133
# Only prints matrix of supported toolchains
132134
if options.supported_toolchains:
133-
print mcu_toolchain_matrix(platform_filter=options.general_filter_regex)
135+
print(mcu_toolchain_matrix(platform_filter=options.general_filter_regex))
134136
exit(0)
135137

136138

@@ -184,7 +186,7 @@
184186
tt_id = "%s::%s" % (toolchain, target)
185187
if toolchain not in TARGET_MAP[target].supported_toolchains:
186188
# Log this later
187-
print "%s skipped: toolchain not supported" % tt_id
189+
print("%s skipped: toolchain not supported" % tt_id)
188190
skipped.append(tt_id)
189191
else:
190192
try:
@@ -224,26 +226,24 @@
224226
successes.append(tt_id)
225227
else:
226228
skipped.append(tt_id)
227-
except Exception, e:
229+
except Exception as e:
228230
if options.verbose:
229231
import traceback
230232
traceback.print_exc(file=sys.stdout)
231233
sys.exit(1)
232234
failures.append(tt_id)
233-
print e
235+
print(e)
234236

235237

236238
# Write summary of the builds
237-
print
238-
print "Completed in: (%.2f)s" % (time() - start)
239-
print
239+
print("\nCompleted in: (%.2f)s\n" % (time() - start))
240240

241241
for report, report_name in [(successes, "Build successes:"),
242242
(skipped, "Build skipped:"),
243243
(failures, "Build failures:"),
244244
]:
245245
if report:
246-
print print_build_results(report, report_name),
246+
print(print_build_results(report, report_name))
247247

248248
if failures:
249249
sys.exit(1)

tools/build_api.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,35 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17+
from __future__ import print_function, division, absolute_import
1718

1819
import re
1920
import tempfile
2021
import datetime
2122
import uuid
22-
from types import ListType
2323
from shutil import rmtree
2424
from os.path import join, exists, dirname, basename, abspath, normpath, splitext
2525
from os.path import relpath
2626
from os import linesep, remove, makedirs
2727
from time import time
2828
from intelhex import IntelHex
2929
from json import load, dump
30-
from tools.arm_pack_manager import Cache
31-
32-
from tools.utils import mkdir, run_cmd, run_cmd_ext, NotSupportedException,\
33-
ToolException, InvalidReleaseTargetException, intelhex_offset
34-
from tools.paths import MBED_CMSIS_PATH, MBED_TARGETS_PATH, MBED_LIBRARIES,\
35-
MBED_HEADER, MBED_DRIVERS, MBED_PLATFORM, MBED_HAL, MBED_CONFIG_FILE,\
36-
MBED_LIBRARIES_DRIVERS, MBED_LIBRARIES_PLATFORM, MBED_LIBRARIES_HAL,\
37-
BUILD_DIR
38-
from tools.targets import TARGET_NAMES, TARGET_MAP
39-
from tools.libraries import Library
40-
from tools.toolchains import TOOLCHAIN_CLASSES
4130
from jinja2 import FileSystemLoader
4231
from jinja2.environment import Environment
43-
from tools.config import Config
32+
33+
from .arm_pack_manager import Cache
34+
from .utils import (mkdir, run_cmd, run_cmd_ext, NotSupportedException,
35+
ToolException, InvalidReleaseTargetException,
36+
intelhex_offset)
37+
from .paths import (MBED_CMSIS_PATH, MBED_TARGETS_PATH, MBED_LIBRARIES,
38+
MBED_HEADER, MBED_DRIVERS, MBED_PLATFORM, MBED_HAL,
39+
MBED_CONFIG_FILE, MBED_LIBRARIES_DRIVERS,
40+
MBED_LIBRARIES_PLATFORM, MBED_LIBRARIES_HAL,
41+
BUILD_DIR)
42+
from .targets import TARGET_NAMES, TARGET_MAP
43+
from .libraries import Library
44+
from .toolchains import TOOLCHAIN_CLASSES
45+
from .config import Config
4446

4547
RELEASE_VERSIONS = ['2', '5']
4648

@@ -124,7 +126,7 @@ def get_config(src_paths, target, toolchain_name):
124126
toolchain_name - the string that identifies the build tools
125127
"""
126128
# Convert src_paths to a list if needed
127-
if type(src_paths) != ListType:
129+
if not isinstance(src_paths, list):
128130
src_paths = [src_paths]
129131

130132
# Pass all params to the unified prepare_resources()
@@ -404,7 +406,7 @@ def scan_resources(src_paths, toolchain, dependencies_paths=None,
404406

405407
# Add additional include directories if passed
406408
if inc_dirs:
407-
if type(inc_dirs) == ListType:
409+
if isinstance(inc_dirs, list):
408410
resources.inc_dirs.extend(inc_dirs)
409411
else:
410412
resources.inc_dirs.append(inc_dirs)
@@ -462,7 +464,7 @@ def build_project(src_paths, build_path, target, toolchain_name,
462464
"""
463465

464466
# Convert src_path to a list if needed
465-
if type(src_paths) != ListType:
467+
if not isinstance(src_paths, list):
466468
src_paths = [src_paths]
467469
# Extend src_paths wiht libraries_paths
468470
if libraries_paths is not None:
@@ -533,9 +535,9 @@ def build_project(src_paths, build_path, target, toolchain_name,
533535
memap_bars = memap_instance.generate_output('bars',
534536
real_stats_depth, None,
535537
getattr(toolchain.target, 'device_name', None))
536-
print memap_bars
538+
print(memap_bars)
537539
else:
538-
print memap_table
540+
print(memap_table)
539541

540542
# Write output to file in JSON format
541543
map_out = join(build_path, name + "_map.json")
@@ -619,7 +621,7 @@ def build_library(src_paths, build_path, target, toolchain_name,
619621
"""
620622

621623
# Convert src_path to a list if needed
622-
if type(src_paths) != ListType:
624+
if not isinstance(src_paths, list):
623625
src_paths = [src_paths]
624626

625627
# Build path
@@ -788,7 +790,7 @@ def build_lib(lib_id, target, toolchain_name, verbose=False,
788790
inc_dirs = lib.inc_dirs
789791
inc_dirs_ext = lib.inc_dirs_ext
790792

791-
if type(src_paths) != ListType:
793+
if not isinstance(src_paths, list):
792794
src_paths = [src_paths]
793795

794796
# The first path will give the name to the library

tools/build_everything.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
test_builds = {}
155155
total_build_success = True
156156

157-
for target_name, target_toolchains in build_config.iteritems():
157+
for target_name, target_toolchains in build_config.items():
158158
target = TARGET_MAP[target_name]
159159

160160
for target_toolchain in target_toolchains:

tools/build_travis.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
See the License for the specific language governing permissions and
1919
limitations under the License.
2020
"""
21+
from __future__ import print_function, division, absolute_import
2122

2223
import os
2324
import sys
@@ -382,11 +383,12 @@ def run_builds(dry_run, vendor):
382383
toolchain_list = build["toolchains"]
383384
if type(toolchain_list) != type([]): toolchain_list = [toolchain_list]
384385
for toolchain in toolchain_list:
385-
cmdline = "python tools/build.py -m %s -t %s -c --silent "% (build["target"], toolchain)
386+
cmdline = ("%s tools/build.py -m %s -t %s -c --silent "%
387+
(sys.executable, build["target"], toolchain))
386388
libs = build.get("libs", [])
387389
if libs:
388390
cmdline = cmdline + " ".join(["--" + l for l in libs])
389-
print "Executing: " + cmdline
391+
print("Executing: %s" % cmdline)
390392
if not dry_run:
391393
if os.system(cmdline) != 0:
392394
sys.exit(1)
@@ -408,19 +410,15 @@ def run_test_linking(dry_run, vendor):
408410
for test_lib in tests:
409411
test_names = tests[test_lib]
410412
test_lib_switch = "--" + test_lib if test_lib else ""
411-
cmdline = "python tools/make.py -m %s -t %s -c --silent %s -n %s " % (link["target"], toolchain, test_lib_switch, ",".join(test_names))
412-
print "Executing: " + cmdline
413+
cmdline = ("%s tools/make.py -m %s -t %s -c --silent %s "
414+
"-n %s" % (sys.executable, link["target"],
415+
toolchain, test_lib_switch,
416+
",".join(test_names)))
417+
print("Executing: %s" % cmdline)
413418
if not dry_run:
414419
if os.system(cmdline) != 0:
415420
sys.exit(1)
416421

417-
def run_test_testsuite(dry_run, vendor):
418-
cmdline = "python tools/singletest.py --version"
419-
print "Executing: " + cmdline
420-
if not dry_run:
421-
if os.system(cmdline) != 0:
422-
sys.exit(1)
423-
424422
if __name__ == "__main__":
425423
parser = ArgumentParser()
426424

@@ -434,4 +432,3 @@ def run_test_testsuite(dry_run, vendor):
434432

435433
run_builds("-s" in sys.argv, options.vendor)
436434
run_test_linking("-s" in sys.argv, options.vendor)
437-
run_test_testsuite("-s" in sys.argv, options.vendor)

0 commit comments

Comments
 (0)