43
43
import errno
44
44
import ctypes
45
45
from itertools import chain , repeat
46
- import argparse
47
- import tempfile
48
46
import zipfile
47
+ import argparse
49
48
50
49
51
50
# Application version
152
151
_cwd = os .getcwd ()
153
152
154
153
# Logging and output
155
- def log (msg ):
156
- sys .stdout .write (msg )
154
+ def log (msg , is_error = False ):
155
+ sys .stderr . write ( msg ) if is_error else sys . stdout .write (msg )
157
156
158
157
def message (msg ):
159
158
return "[mbed] %s\n " % msg
@@ -168,14 +167,18 @@ def action(msg):
168
167
log (message (line ))
169
168
170
169
def warning (msg ):
171
- for line in msg .splitlines ():
172
- sys .stderr .write ("[mbed] WARNING: %s\n " % line )
173
- sys .stderr .write ("---\n " )
170
+ lines = msg .splitlines ()
171
+ log (message ("WARNING: %s" % lines .pop (0 )), True )
172
+ for line in lines :
173
+ log (" %s\n " % line , True )
174
+ log ("---\n " , True )
174
175
175
176
def error (msg , code = - 1 ):
176
- for line in msg .splitlines ():
177
- sys .stderr .write ("[mbed] ERROR: %s\n " % line )
178
- sys .stderr .write ("---\n " )
177
+ lines = msg .splitlines ()
178
+ log (message ("ERROR: %s" % lines .pop (0 )), True )
179
+ for line in lines :
180
+ log (" %s\n " % line , True )
181
+ log ("---\n " , True )
179
182
sys .exit (code )
180
183
181
184
def offline_warning (offline , top = True ):
@@ -217,15 +220,15 @@ class ProcessException(Exception):
217
220
218
221
def popen (command , stdin = None , ** kwargs ):
219
222
# print for debugging
220
- info (' Exec "' + ' ' .join (command )+ '" in ' + getcwd ())
223
+ info (" Exec \" %s \" in \" %s \" " % ( ' ' .join (command ), getcwd () ))
221
224
proc = None
222
225
try :
223
226
proc = subprocess .Popen (command , ** kwargs )
224
227
except OSError as e :
225
228
if e .args [0 ] == errno .ENOENT :
226
229
error (
227
- "Could not execute \" %s\" .\n "
228
- "Please verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (command [ 0 ] , command [0 ]), e .args [0 ])
230
+ "Could not execute \" %s\" in \" %s \" .\n "
231
+ "You can verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (' ' . join ( command ), getcwd () , command [0 ]), e .args [0 ])
229
232
else :
230
233
raise e
231
234
@@ -234,14 +237,14 @@ def popen(command, stdin=None, **kwargs):
234
237
235
238
def pquery (command , output_callback = None , stdin = None , ** kwargs ):
236
239
if very_verbose :
237
- info ('Query "' + ' ' .join (command )+ '" in ' + getcwd ())
240
+ info ("Exec \" %s \" in \" %s \" " % ( ' ' .join (command ), getcwd () ))
238
241
try :
239
242
proc = subprocess .Popen (command , bufsize = 0 , stdout = subprocess .PIPE , stderr = subprocess .PIPE , ** kwargs )
240
243
except OSError as e :
241
244
if e .args [0 ] == errno .ENOENT :
242
245
error (
243
- "Could not execute \" %s\" .\n "
244
- "Please verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (command [ 0 ] , command [0 ]), e .args [0 ])
246
+ "Could not execute \" %s\" in \" %s \" .\n "
247
+ "You can verify that it's installed and accessible from your current path by executing \" %s\" .\n " % (' ' . join ( command ), getcwd () , command [0 ]), e .args [0 ])
245
248
else :
246
249
raise e
247
250
@@ -785,9 +788,9 @@ def update(rev=None, clean=False, clean_files=False, is_local=False):
785
788
else :
786
789
err = "Unable to update \" %s\" in \" %s\" ." % (os .path .basename (getcwd ()), getcwd ())
787
790
if not remote :
788
- info (err + " The local repository is not associated with a remote one." )
791
+ info (err + "\n The local repository is not associated with a remote one. \n You should associate your repository with a remote one." )
789
792
if not branch :
790
- info (err + " Working set is not on a branch." )
793
+ info (err + "\n The working set is not on a branch. \n You should switch to a branch or create a new one from the current revision ." )
791
794
792
795
def status ():
793
796
return pquery ([git_cmd , 'status' , '-s' ] + (['-v' ] if very_verbose else []))
@@ -971,9 +974,9 @@ def action_progress(line, sep):
971
974
if m .group (1 ) == "Receiving objects" :
972
975
show_progress ('Downloading' , (float (m .group (3 )) / float (m .group (4 ))) * 80 )
973
976
if m .group (1 ) == "Resolving deltas" :
974
- show_progress ('Downloading' , (float (m .group (3 )) / float (m .group (4 ))) * 10 + 80 )
977
+ show_progress ('Downloading' , (float (m .group (3 )) / float (m .group (4 ))) * 20 + 80 )
975
978
if m .group (1 ) == "Checking out files" :
976
- show_progress ('Downloading ' , (float (m .group (3 )) / float (m .group (4 ))) * 10 + 90 )
979
+ show_progress ('Checking out ' , (float (m .group (3 )) / float (m .group (4 ))) * 100 )
977
980
978
981
979
982
# Repository object
@@ -1496,23 +1499,23 @@ def check_requirements(self, show_warning=False):
1496
1499
pquery ([python_cmd , '-m' , 'pip' , 'install' , '-q' , '-r' , os .path .join (req_path , req_file )])
1497
1500
missing = []
1498
1501
except ProcessException :
1499
- warning ( "Unable to auto-install required Python modules." )
1502
+ pass
1500
1503
1501
1504
except (IOError , ImportError , OSError ):
1502
1505
pass
1503
1506
1504
1507
if missing :
1505
- err = (
1506
- "----------------------------------------------------------------- \n "
1508
+ msg = (
1509
+ "Unable to auto-install required Python modules. \n "
1507
1510
"The mbed OS tools in this program require the following Python modules: %s\n "
1508
1511
"You can install all missing modules by running \" pip install -r %s\" in \" %s\" " % (', ' .join (missing ), req_file , req_path ))
1509
1512
if os .name == 'posix' :
1510
- err += "\n On Posix systems (Linux, Mac, etc) you might have to switch to superuser account or use \" sudo\" "
1513
+ msg += "\n On Posix systems (Linux, Mac, etc) you might have to switch to superuser account or use \" sudo\" "
1511
1514
1512
1515
if show_warning :
1513
- warning (err )
1516
+ warning (msg )
1514
1517
else :
1515
- error (err , 1 )
1518
+ error (msg , 1 )
1516
1519
1517
1520
1518
1521
# Routines after cloning mbed-os
@@ -1562,7 +1565,7 @@ def update_tools(self, path):
1562
1565
def get_tools (self ):
1563
1566
mbed_tools_path = self .get_tools_dir ()
1564
1567
if not mbed_tools_path :
1565
- error (' The mbed tools were not found in "%s". \n Run ` mbed deploy` to install dependencies and tools. ' % self .path , - 1 )
1568
+ error (" The mbed tools were not found in \ " %s\ " . \n You can run \" mbed deploy\" to install dependencies and tools." % self .path , - 1 )
1566
1569
return mbed_tools_path
1567
1570
1568
1571
def get_env (self ):
@@ -1585,14 +1588,14 @@ def get_target(self, target=None):
1585
1588
target = detected ['name' ]
1586
1589
1587
1590
if target is None :
1588
- error ("Please specify target using the -m switch or set default target using command ' mbed target' " , 1 )
1591
+ error ("Please specify target using the -m switch or set default target using command \" mbed target\" " , 1 )
1589
1592
return target
1590
1593
1591
1594
def get_toolchain (self , toolchain = None ):
1592
1595
toolchain_cfg = self .get_cfg ('TOOLCHAIN' )
1593
1596
tchain = toolchain if toolchain else toolchain_cfg
1594
1597
if tchain is None :
1595
- error ("Please specify toolchain using the -t switch or set default toolchain using command ' mbed toolchain' " , 1 )
1598
+ error ("Please specify toolchain using the -t switch or set default toolchain using command \" mbed toolchain\" " , 1 )
1596
1599
return tchain
1597
1600
1598
1601
def set_defaults (self , target = None , toolchain = None ):
@@ -1623,7 +1626,7 @@ def ignore_build_dir(self):
1623
1626
def detect_target (self , info = None ):
1624
1627
targets = self .get_detected_targets ()
1625
1628
if targets == False :
1626
- error ("The target detection requires that the 'mbed-ls' python module is installed.\n You can install mbed-ls by running ' pip install mbed-ls' ." , 1 )
1629
+ error ("The target detection requires that the 'mbed-ls' python module is installed.\n You can install mbed-ls by running \" pip install mbed-ls\" ." , 1 )
1627
1630
elif len (targets ) > 1 :
1628
1631
error ("Multiple targets were detected.\n Only 1 target board should be connected to your system." , 1 )
1629
1632
elif len (targets ) == 0 :
@@ -1785,6 +1788,8 @@ def formaturl(url, format="default"):
1785
1788
def mbed_sterm (port , baudrate = 9600 , echo = True , reset = False , sterm = False ):
1786
1789
try :
1787
1790
from .mbed_terminal import MbedTerminal
1791
+ except (ValueError ): # relative import fails on Python 2 in non-package mode
1792
+ from mbed_terminal import MbedTerminal
1788
1793
except (IOError , ImportError , OSError ):
1789
1794
error ("The serial terminal functionality requires that the 'mbed-terminal' python module is installed.\n You can install mbed-terminal by running 'pip install mbed-terminal'." , 1 )
1790
1795
@@ -1808,7 +1813,7 @@ def mbed_sterm(port, baudrate=9600, echo=True, reset=False, sterm=False):
1808
1813
1809
1814
# Subparser handling
1810
1815
parser = argparse .ArgumentParser (prog = 'mbed' ,
1811
- description = "Command-line code management tool for ARM mbed OS - http://www.mbed.com\n version %s\n \n Use ' mbed <command> -h|--help' for detailed help.\n Online manual and guide available at https://github.com/ARMmbed/mbed-cli" % ver ,
1816
+ description = "Command-line code management tool for ARM mbed OS - http://www.mbed.com\n version %s\n \n Use \" mbed <command> -h|--help\" for detailed help.\n Online manual and guide available at https://github.com/ARMmbed/mbed-cli" % ver ,
1812
1817
formatter_class = argparse .RawTextHelpFormatter )
1813
1818
subparsers = parser .add_subparsers (title = "Commands" , metavar = " " )
1814
1819
parser .add_argument ("--version" , action = "store_true" , dest = "version" , help = "print version number and exit" )
@@ -1963,7 +1968,7 @@ def new(name, scm='git', program=False, library=False, mbedlib=False, create_onl
1963
1968
description = (
1964
1969
"Imports mbed program and its dependencies from a source control based URL\n "
1965
1970
"(GitHub, Bitbucket, mbed.org) into the current directory or specified\n path.\n "
1966
- "Use ' mbed add <URL>' to add a library into an existing program." ))
1971
+ "Use \" mbed add <URL>\" to add a library into an existing program." ))
1967
1972
def import_ (url , path = None , ignore = False , depth = None , protocol = None , insecure = False , offline = False , top = True ):
1968
1973
global cwd_root
1969
1974
offline_warning (offline , top )
@@ -2041,7 +2046,7 @@ def import_(url, path=None, ignore=False, depth=None, protocol=None, insecure=Fa
2041
2046
description = (
2042
2047
"Adds mbed library and its dependencies from a source control based URL\n "
2043
2048
"(GitHub, Bitbucket, mbed.org) into an existing program.\n "
2044
- "Use ' mbed import <URL>' to import as a program" ))
2049
+ "Use \" mbed import <URL>\" to import as a program" ))
2045
2050
def add (url , path = None , ignore = False , depth = None , protocol = None , insecure = False , offline = False , top = True ):
2046
2051
offline_warning (offline , top )
2047
2052
@@ -2065,7 +2070,7 @@ def add(url, path=None, ignore=False, depth=None, protocol=None, insecure=False,
2065
2070
hidden_aliases = ['rm' , 'rem' ],
2066
2071
description = (
2067
2072
"Remove specified library, its dependencies and references from the current\n "
2068
- "You can re-add the library from its URL via ' mbed add <library URL>' ." ))
2073
+ "You can re-add the library from its URL via \" mbed add <URL>\" ." ))
2069
2074
def remove (path ):
2070
2075
repo = Repo .fromrepo ()
2071
2076
if not Repo .isrepo (path ):
@@ -2088,8 +2093,8 @@ def remove(path):
2088
2093
help = 'Find and add missing libraries' ,
2089
2094
description = (
2090
2095
"Import missing dependencies in an existing program or library.\n "
2091
- "Use ' mbed import <URL>' and ' mbed add <URL>' instead of cloning manually and \n "
2092
- "then running ' mbed deploy' " ))
2096
+ "Hint: Use \" mbed import <URL>\" and \" mbed add <URL>\" instead of cloning\n "
2097
+ "manually and then running \" mbed deploy\" " ))
2093
2098
def deploy (ignore = False , depth = None , protocol = None , insecure = False , offline = False , top = True ):
2094
2099
offline_warning (offline , top )
2095
2100
@@ -2444,7 +2449,7 @@ def status_(ignore=False):
2444
2449
dict (name = ['--sterm' ], action = 'store_true' , help = 'Open serial terminal after compiling. Can be chained with --flash' ),
2445
2450
dict (name = ['-N' , '--artifact-name' ], help = 'Name of the built program or library' ),
2446
2451
dict (name = ['-S' , '--supported' ], dest = 'supported' , const = True , choices = ["matrix" , "toolchains" , "targets" ], nargs = "?" , help = 'Shows supported matrix of targets and toolchains' ),
2447
- dict (name = '--app-config' , dest = "app_config" , help = "Path of an app configuration file ( Default is to look for ' mbed_app.json') " ),
2452
+ dict (name = '--app-config' , dest = "app_config" , help = "Path of an application configuration file. Default is to look for \" mbed_app.json\" . " ),
2448
2453
help = 'Compile code using the mbed build tools' ,
2449
2454
description = "Compile this program using the mbed build tools." )
2450
2455
def compile_ (toolchain = None , target = None , profile = False , compile_library = False , compile_config = False , config_prefix = None , source = False , build = False , clean = False , flash = False , sterm = False , artifact_name = None , supported = False , app_config = None ):
@@ -2532,15 +2537,15 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
2532
2537
try :
2533
2538
from mbed_host_tests .host_tests_toolbox import flash_dev
2534
2539
except (IOError , ImportError , OSError ):
2535
- error ("The '-f/--flash' option requires that the 'mbed-greentea' python module is installed.\n You can install mbed-greentea by running ' pip install mbed-greentea'." , 1 )
2540
+ error ("The '-f/--flash' option requires that the 'mbed-greentea' python module is installed.\n You can install mbed-greentea by running \" %s -m pip install mbed-greentea\" ." % python_cmd , 1 )
2536
2541
2537
2542
if flash :
2538
2543
fw_name = artifact_name if artifact_name else program .name
2539
2544
fw_fbase = os .path .join (build_path , fw_name )
2540
2545
fw_file = fw_fbase + ('.hex' if os .path .exists (fw_fbase + '.hex' ) else '.bin' )
2541
2546
if not os .path .exists (fw_file ):
2542
2547
error ("Build program file (firmware) not found \" %s\" " % fw_file , 1 )
2543
- if not flash_dev (detected ['msd' ], fw_file , program_cycle_s = 2 ):
2548
+ if not flash_dev (detected ['msd' ], fw_file , program_cycle_s = 4 ):
2544
2549
error ("Unable to flash the target board connected to your system." , 1 )
2545
2550
2546
2551
if flash or sterm :
@@ -2564,7 +2569,7 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
2564
2569
dict (name = ['--profile' ], action = 'append' , help = 'Path of a build profile configuration file. Example: mbed-os/tools/profiles/debug.json' ),
2565
2570
dict (name = ['-c' , '--clean' ], action = 'store_true' , help = 'Clean the build directory before compiling' ),
2566
2571
dict (name = '--test-spec' , dest = "test_spec" , help = "Path used for the test spec file used when building and running tests (the default path is the build directory)" ),
2567
- dict (name = '--app-config' , dest = "app_config" , help = "Path of an app configuration file ( Default is to look for ' mbed_app.json') " ),
2572
+ dict (name = '--app-config' , dest = "app_config" , help = "Path of an application configuration file. Default is to look for \" mbed_app.json\" " ),
2568
2573
dict (name = '--test-config' , dest = "test_config" , help = "Path or mbed OS keyword of a test configuration file. Example: ethernet, odin_wifi, or path/to/config.json" ),
2569
2574
help = 'Find, build and run tests' ,
2570
2575
description = "Find, build, and run tests in a program and libraries" )
@@ -2659,7 +2664,7 @@ def test_(toolchain=None, target=None, compile_list=False, run_list=False, compi
2659
2664
dict (name = '--source' , action = 'append' , help = 'Source directory. Default: . (current dir)' ),
2660
2665
dict (name = ['-c' , '--clean' ], action = 'store_true' , help = 'Clean the build directory before compiling' ),
2661
2666
dict (name = ['-S' , '--supported' ], dest = 'supported' , const = True , choices = ['matrix' , 'ides' ], nargs = '?' , help = 'Shows supported matrix of targets and toolchains' ),
2662
- dict (name = '--app-config' , dest = "app_config" , help = "Path of an app configuration file ( Default is to look for ' mbed_app.json') " ),
2667
+ dict (name = '--app-config' , dest = "app_config" , help = "Path of an application configuration file. Default is to look for \" mbed_app.json\" " ),
2663
2668
help = 'Generate an IDE project' ,
2664
2669
description = (
2665
2670
"Generate IDE project files for the current program." ))
@@ -2751,7 +2756,7 @@ def detect():
2751
2756
2752
2757
if unknown_found :
2753
2758
warning ("If you're developing a new target, you can mock the device to continue your development. "
2754
- "Use ' mbedls --mock ID:NAME' to do so (see ' mbedls --help' for more information) " )
2759
+ "Use \" mbedls --mock ID:NAME\" to do so. See \" mbedls --help\" for more information. " )
2755
2760
2756
2761
2757
2762
# Serial terminal command
@@ -2851,7 +2856,7 @@ def config_(var=None, value=None, global_cfg=False, unset=False, list_config=Fal
2851
2856
if program .is_cwd and not var == 'ROOT' :
2852
2857
error (
2853
2858
"Could not find mbed program in current path \" %s\" .\n "
2854
- "Change the current directory to a valid mbed program, set the current directory as an mbed program with ' mbed config root .' , or use the '--global' option to set global configuration." % program .path )
2859
+ "Change the current directory to a valid mbed program, set the current directory as an mbed program with \" mbed config root .\" , or use the '--global' option to set global configuration." % program .path )
2855
2860
with cd (program .path ):
2856
2861
if unset :
2857
2862
program .set_cfg (var , None )
@@ -2874,7 +2879,7 @@ def config_(var=None, value=None, global_cfg=False, unset=False, list_config=Fal
2874
2879
help = 'Set or get default target' ,
2875
2880
description = (
2876
2881
"Set or get default toolchain\n "
2877
- "This is an alias to ' mbed config [--global] target [name]' \n " ))
2882
+ "This is an alias to \" mbed config [--global] target [name]\" \n " ))
2878
2883
def target_ (name = None , global_cfg = False , supported = False ):
2879
2884
if supported :
2880
2885
return compile_ (supported = supported )
@@ -2888,7 +2893,7 @@ def target_(name=None, global_cfg=False, supported=False):
2888
2893
help = 'Set or get default toolchain' ,
2889
2894
description = (
2890
2895
"Set or get default toolchain\n "
2891
- "This is an alias to ' mbed config [--global] toolchain [name]' \n " ))
2896
+ "This is an alias to \" mbed config [--global] toolchain [name]\" \n " ))
2892
2897
def toolchain_ (name = None , global_cfg = False , supported = False ):
2893
2898
if supported :
2894
2899
return compile_ (supported = supported )
@@ -3001,8 +3006,11 @@ def main():
3001
3006
status = pargs .command (pargs )
3002
3007
except ProcessException as e :
3003
3008
error (
3004
- "\" %s\" returned error code %d.\n "
3005
- "Command \" %s\" in \" %s\" " % (e .args [1 ], e .args [0 ], e .args [2 ], e .args [3 ]), e .args [0 ])
3009
+ "\" %s\" returned error.\n "
3010
+ "Code: %d\n "
3011
+ "Path: \" %s\" \n "
3012
+ "Command: \" %s\" \n "
3013
+ "Tip: You could retry the last command with \" -v\" flag for verbose output\n " % (e .args [1 ], e .args [0 ], e .args [3 ], e .args [2 ]), e .args [0 ])
3006
3014
except OSError as e :
3007
3015
if e .args [0 ] == errno .ENOENT :
3008
3016
error (
0 commit comments