Skip to content

python 2->3 #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ target/
# Doxygen
html/
latex/

# editors
*.idea

# you ain't ready for this
Pipfile*
3 changes: 3 additions & 0 deletions mbed/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from __future__ import absolute_import
from .mbed import main
main()
66 changes: 35 additions & 31 deletions mbed/mbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# pylint: disable=too-many-nested-blocks, too-many-public-methods, too-many-instance-attributes, too-many-statements
# pylint: disable=invalid-name, missing-docstring, bad-continuation

from __future__ import print_function
import traceback
import sys
import re
Expand All @@ -28,13 +29,21 @@
import stat
import errno
import ctypes
from itertools import chain, izip, repeat
from urlparse import urlparse
import urllib2
from itertools import chain, repeat
import zipfile
import argparse
import tempfile

try:
# Python 2
basestring = (unicode, str)
from urlparse import urlparse
from urllib2 import urlopen
except NameError:
# Python 3
basestring = str
from urllib.parse import urlparse
from urllib.request import urlopen

# Application version
ver = '1.3.0'
Expand Down Expand Up @@ -173,7 +182,7 @@ def progress_cursor():
progress_spinner = progress_cursor()

def progress():
sys.stdout.write(progress_spinner.next())
sys.stdout.write(next(progress_spinner))
sys.stdout.flush()
sys.stdout.write('\b')

Expand Down Expand Up @@ -313,7 +322,7 @@ def fetch_rev(url, rev):
if not os.path.exists(rev_file):
action("Downloading library build \"%s\" (might take a minute)" % rev)
outfd = open(rev_file, 'wb')
inurl = urllib2.urlopen(url)
inurl = urlopen(url)
outfd.write(inurl.read())
outfd.close()
except:
Expand Down Expand Up @@ -1128,7 +1137,7 @@ def remove(self, dest, *args, **kwargs):
def clone(self, url, path, rev=None, depth=None, protocol=None, **kwargs):
# Sorted so repositories that match urls are attempted first
sorted_scms = [(scm.isurl(url), scm) for scm in scms.values()]
sorted_scms = sorted(sorted_scms, key=lambda (m, _): not m)
sorted_scms = sorted(sorted_scms, key=lambda m__: not m__[0])

for _, scm in sorted_scms:
main = True
Expand Down Expand Up @@ -2206,7 +2215,7 @@ def sync(recursive=True, keep_refs=False, top=True):
def list_(detailed=False, prefix='', p_path=None, ignore=False):
repo = Repo.fromrepo()

print "%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else repo.revtype(repo.rev, fmt=6)) or 'no revision'))
print("%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else repo.revtype(repo.rev, fmt=6)) or 'no revision')))

for i, lib in enumerate(sorted(repo.libs, key=lambda l: l.path)):
nprefix = (prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ')) if prefix else ''
Expand Down Expand Up @@ -2239,16 +2248,16 @@ def releases_(detailed=False, unstable=False, recursive=False, prefix='', p_path
rels.append(tag[1] + " %s%s" % ('#' + tag[0] if detailed else "", " <- current" if tag[1] in revtags else ""))

# Print header
print "%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else repo.revtype(repo.rev, fmt=6)) or 'no revision'))
print("%s (%s)" % (prefix + (relpath(p_path, repo.path) if p_path else repo.name), ((repo.url + ('#' + str(repo.rev)[:12] if repo.rev else '') if detailed else repo.revtype(repo.rev, fmt=6)) or 'no revision')))

# Print list of tags
rprefix = (prefix[:-3] + ('| ' if prefix[-3] == '|' else ' ')) if recursive and prefix else ''
rprefix += '| ' if recursive and len(repo.libs) > 1 else ' '
if len(rels):
for rel in rels:
print rprefix + '* ' + rel
print(rprefix + '* ' + rel)
else:
print rprefix + 'No release tags detected'
print(rprefix + 'No release tags detected')

if recursive:
for i, lib in enumerate(sorted(repo.libs, key=lambda l: l.path)):
Expand Down Expand Up @@ -2330,10 +2339,10 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
# Compile configuration
popen([python_cmd, os.path.join(tools_dir, 'get_config.py')]
+ ['-t', tchain, '-m', target]
+ list(chain.from_iterable(izip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(izip(repeat('--source'), source)))
+ list(chain.from_iterable(zip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(zip(repeat('--source'), source)))
+ (['-v'] if verbose else [])
+ (list(chain.from_iterable(izip(repeat('--prefix'), config_prefix))) if config_prefix else []),
+ (list(chain.from_iterable(zip(repeat('--prefix'), config_prefix))) if config_prefix else []),
env=env)
else:
# If the user hasn't supplied a build directory, ignore the default build directory
Expand All @@ -2348,10 +2357,10 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
build_path = os.path.join(os.path.relpath(program.path, orig_path), program.build_dir, 'libraries', os.path.basename(orig_path), target, tchain)

popen([python_cmd, '-u', os.path.join(tools_dir, 'build.py')]
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
+ list(chain.from_iterable(zip(repeat('-D'), macros)))
+ ['-t', tchain, '-m', target]
+ list(chain.from_iterable(izip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(izip(repeat('--source'), source)))
+ list(chain.from_iterable(zip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(zip(repeat('--source'), source)))
+ ['--build', build_path]
+ (['-c'] if clean else [])
+ (['--artifact-name', artifact_name] if artifact_name else [])
Expand All @@ -2364,10 +2373,10 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
build_path = os.path.join(os.path.relpath(program.path, orig_path), program.build_dir, target, tchain)

popen([python_cmd, '-u', os.path.join(tools_dir, 'make.py')]
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
+ list(chain.from_iterable(zip(repeat('-D'), macros)))
+ ['-t', tchain, '-m', target]
+ list(chain.from_iterable(izip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(izip(repeat('--source'), source)))
+ list(chain.from_iterable(zip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(zip(repeat('--source'), source)))
+ ['--build', build_path]
+ (['-c'] if clean else [])
+ (['--artifact-name', artifact_name] if artifact_name else [])
Expand Down Expand Up @@ -2453,9 +2462,9 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi

if compile_list:
popen([python_cmd, '-u', os.path.join(tools_dir, 'test.py'), '--list']
+ list(chain.from_iterable(izip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(zip(repeat('--profile'), profile or [])))
+ ['-t', tchain, '-m', target]
+ list(chain.from_iterable(izip(repeat('--source'), source)))
+ list(chain.from_iterable(zip(repeat('--source'), source)))
+ (['-n', tests_by_name] if tests_by_name else [])
+ (['-v'] if verbose else [])
+ (['--app-config', app_config] if app_config else [])
Expand All @@ -2469,11 +2478,11 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
program.ignore_build_dir()

popen([python_cmd, '-u', os.path.join(tools_dir, 'test.py')]
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
+ list(chain.from_iterable(izip(repeat('--profile'), profile or [])))
+ list(chain.from_iterable(zip(repeat('-D'), macros)))
+ list(chain.from_iterable(zip(repeat('--profile'), profile or [])))
+ ['-t', tchain, '-m', target]
+ (['-c'] if clean else [])
+ list(chain.from_iterable(izip(repeat('--source'), source)))
+ list(chain.from_iterable(zip(repeat('--source'), source)))
+ ['--build', build_path]
+ ['--test-spec', test_spec]
+ (['-n', tests_by_name] if tests_by_name else [])
Expand Down Expand Up @@ -2544,11 +2553,11 @@ def export(ide=None, target=None, source=False, clean=False, supported=False, ap
program.ignore_build_dir()

popen([python_cmd, '-u', os.path.join(tools_dir, 'project.py')]
+ list(chain.from_iterable(izip(repeat('-D'), macros)))
+ list(chain.from_iterable(zip(repeat('-D'), macros)))
+ ['-i', ide.lower()]
+ ['-m', target]
+ (['-c'] if clean else [])
+ list(chain.from_iterable(izip(repeat('--source'), source)))
+ list(chain.from_iterable(zip(repeat('--source'), source)))
+ (['--app-config', app_config] if app_config else [])
+ args,
env=env)
Expand Down Expand Up @@ -2716,11 +2725,6 @@ def main():
# Help messages adapt based on current dir
cwd_root = getcwd()

if sys.version_info[0] != 2 or sys.version_info[1] < 7:
error(
"mbed CLI is compatible with Python version >= 2.7 and < 3.0\n"
"Please refer to the online guide available at https://github.com/ARMmbed/mbed-cli")

# Parse/run command
if len(sys.argv) <= 1:
help_()
Expand Down