3535
3636
3737# Application version
38- ver = '0.8.5 '
38+ ver = '0.8.7 '
3939
4040# Default paths to Mercurial and Git
4141hg_cmd = 'hg'
9797regex_url_ref = r'^(.*/([\w.+-]+)(?:\.\w+)?)/?(?:#(.*))?$'
9898
9999# git url (no #rev)
100- regex_git_url = r'^(git@|git \://|ssh\://|https?\://) ([^/:]+)[:/](.+?)(\.git|\/?)$'
100+ regex_git_url = r'^(git\://|ssh\://|https?\://|)(([^/:@]+)(\:([^/:@]+))?@)? ([^/:]+)[:/](.+?)(\.git|\/?)$'
101101# hg url (no #rev)
102102regex_hg_url = r'^(file|ssh|https?)://([^/:]+)/([^/]+)/?([^/]+?)?$'
103103
119119verbose = False
120120very_verbose = False
121121
122+ # stores current working directory for recursive operations
123+ cwd_root = ""
124+
122125
123126# Logging and output
124127def message (msg ):
@@ -1216,7 +1219,7 @@ def get_env(self):
12161219 def post_action (self ):
12171220 mbed_tools_path = self .get_tools_dir ()
12181221
1219- if not mbed_tools_path and self .is_classic :
1222+ if not mbed_tools_path and ( self .is_classic or os . path . exists ( os . path . join ( self . path , Cfg . file ))) :
12201223 self .add_tools (os .path .join (self .path , '.temp' ))
12211224 mbed_tools_path = self .get_tools_dir ()
12221225
@@ -1305,7 +1308,10 @@ class Global(object):
13051308 def __init__ (self ):
13061309 self .path = os .path .join (os .path .expanduser ("~" ), '.mbed' )
13071310 if not os .path .exists (self .path ):
1308- os .mkdir (self .path )
1311+ try :
1312+ os .mkdir (self .path )
1313+ except (IOError , OSError ):
1314+ pass
13091315
13101316 def get_cfg (self , * args , ** kwargs ):
13111317 return Cfg (self .path ).get (* args , ** kwargs )
@@ -1327,7 +1333,7 @@ def set(self, var, val):
13271333 try :
13281334 with open (fl ) as f :
13291335 lines = f .read ().splitlines ()
1330- except IOError :
1336+ except ( IOError , OSError ) :
13311337 lines = []
13321338
13331339 for line in lines :
@@ -1338,16 +1344,20 @@ def set(self, var, val):
13381344 if not val is None :
13391345 lines += [var + "=" + val ]
13401346
1341- with open (fl , 'w' ) as f :
1342- f .write ('\n ' .join (lines ) + '\n ' )
1347+ try :
1348+ with open (fl , 'w' ) as f :
1349+ f .write ('\n ' .join (lines ) + '\n ' )
1350+ except (IOError , OSError ):
1351+ warning ("Unable to write config file %s" % fl )
1352+ pass
13431353
13441354 # Gets config value
13451355 def get (self , var , default_val = None ):
13461356 fl = os .path .join (self .path , self .file )
13471357 try :
13481358 with open (fl ) as f :
13491359 lines = f .read ().splitlines ()
1350- except IOError :
1360+ except ( IOError , OSError ) :
13511361 lines = []
13521362
13531363 for line in lines :
@@ -1369,11 +1379,11 @@ def formaturl(url, format="default"):
13691379 m = re .match (regex_git_url , url )
13701380 if m :
13711381 if format == "ssh" :
1372- url = 'ssh://git@%s /%s.git' % (m .group (2 ), m .group (3 ))
1382+ url = 'ssh://%s%s /%s.git' % (m .group (2 ) or 'git@' , m .group (6 ), m . group ( 7 ))
13731383 elif format == "http" :
1374- url = 'http://%s/%s' % (m .group (2 ), m .group (3 ))
1384+ url = 'http://%s%s /%s' % (m .group (2 ) if m .group (5 ) or m . group ( 3 ) != 'git' else '' , m . group ( 6 ), m . group ( 7 ))
13751385 elif format == "https" :
1376- url = 'https://%s/%s' % (m .group (2 ), m .group (3 ))
1386+ url = 'https://%s%s /%s' % (m .group (2 ) if m .group (5 ) or m . group ( 3 ) != 'git' else '' , m . group ( 6 ), m . group ( 7 ))
13771387 else :
13781388 m = re .match (regex_hg_url , url )
13791389 if m :
@@ -1386,11 +1396,6 @@ def formaturl(url, format="default"):
13861396 return url
13871397
13881398
1389- # Help messages adapt based on current dir
1390- cwd_root = os .getcwd ()
1391- cwd_type = Repo .pathtype (cwd_root )
1392- cwd_dest = "program" if cwd_type == "directory" else "library"
1393-
13941399# Subparser handling
13951400parser = argparse .ArgumentParser (prog = 'mbed' ,
13961401 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 ,
@@ -1522,8 +1527,8 @@ def new(name, scm='git', program=False, library=False, mbedlib=False, create_onl
15221527
15231528# Import command
15241529@subcommand ('import' ,
1525- dict (name = 'url' , help = 'URL of the %s' % cwd_dest ),
1526- dict (name = 'path' , nargs = '?' , help = 'Destination name or path. Default: current %s.' % cwd_type ),
1530+ dict (name = 'url' , help = 'URL of the program' ),
1531+ dict (name = 'path' , nargs = '?' , help = 'Destination name or path. Default: current directory.' ),
15271532 dict (name = ['-I' , '--ignore' ], action = 'store_true' , help = 'Ignore errors related to cloning and updating.' ),
15281533 dict (name = '--depth' , nargs = '?' , help = 'Number of revisions to fetch from the remote repository. Default: all revisions.' ),
15291534 dict (name = '--protocol' , nargs = '?' , help = 'Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.' ),
@@ -1664,11 +1669,11 @@ def deploy(ignore=False, depth=None, protocol=None, top=True):
16641669 dict (name = ['-M' , '--message' ], dest = 'msg' , type = str , nargs = '?' , help = 'Commit message. Default: prompts for commit message.' ),
16651670 help = 'Publish program or library' ,
16661671 description = (
1667- "Publishes this %s and all dependencies to their associated remote\n repository URLs.\n "
1672+ "Publishes the current program or library and all dependencies to their\n associated remote repository URLs.\n "
16681673 "This command performs various consistency checks for local uncommitted changes\n "
16691674 "and unpublished revisions and encourages to commit/push them.\n "
16701675 "Online guide about collaboration is available at:\n "
1671- "www.mbed.com/collab_guide" % cwd_type ))
1676+ "www.mbed.com/collab_guide" ))
16721677def publish (all_refs = None , msg = None , top = True ):
16731678 if top :
16741679 action ("Checking for local modifications..." )
@@ -1716,13 +1721,16 @@ def publish(all_refs=None, msg=None, top=True):
17161721 dict (name = '--protocol' , nargs = '?' , help = 'Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.' ),
17171722 help = 'Update to branch, tag, revision or latest' ,
17181723 description = (
1719- "Updates this %s and its dependencies to specified branch , tag or revision.\n "
1724+ "Updates the current program or library and its dependencies to specified\n branch , tag or revision.\n "
17201725 "Alternatively fetches from associated remote repository URL and updates to the\n "
1721- "latest revision in the current branch." % cwd_type ))
1726+ "latest revision in the current branch." ))
17221727def update (rev = None , clean = False , clean_files = False , clean_deps = False , ignore = False , top = True , depth = None , protocol = None ):
17231728 if top and clean :
17241729 sync ()
17251730
1731+ cwd_type = Repo .pathtype (cwd_root )
1732+ cwd_dest = "program" if cwd_type == "directory" else "library"
1733+
17261734 repo = Repo .fromrepo ()
17271735 # A copy of repo containing the .lib layout before updating
17281736 repo_orig = Repo .fromrepo ()
@@ -1818,8 +1826,8 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
18181826 help = 'Synchronize library references' ,
18191827 description = (
18201828 "Synchronizes all library and dependency references (.lib files) in the\n "
1821- "current %s .\n "
1822- "Note that this will remove all invalid library references." % cwd_type ))
1829+ "current program or library .\n "
1830+ "Note that this will remove all invalid library references." ))
18231831def sync (recursive = True , keep_refs = False , top = True ):
18241832 if top and recursive :
18251833 action ("Synchronizing dependency references..." )
@@ -1868,6 +1876,7 @@ def sync(recursive=True, keep_refs=False, top=True):
18681876 sync (keep_refs = keep_refs , top = False )
18691877
18701878 # Update the .lib reference in the parent repository
1879+ cwd_type = Repo .pathtype (cwd_root )
18711880 if top and cwd_type == "library" :
18721881 repo = Repo .fromrepo ()
18731882 repo .write ()
@@ -1879,7 +1888,7 @@ def sync(recursive=True, keep_refs=False, top=True):
18791888 dict (name = ['-I' , '--ignore' ], action = 'store_true' , help = 'Ignore errors related to missing libraries.' ),
18801889 help = 'View dependency tree' ,
18811890 description = (
1882- "View the dependency tree in this %s." % cwd_type ))
1891+ "View the dependency tree of the current program or library." ))
18831892def list_ (detailed = False , prefix = '' , p_path = None , ignore = False ):
18841893 repo = Repo .fromrepo ()
18851894 print prefix + (relpath (p_path , repo .path ) if p_path else repo .name ), '(%s)' % ((repo .url + ('#' + str (repo .rev )[:12 ] if repo .rev else '' ) if detailed else str (repo .rev )[:12 ]) or 'no revision' )
@@ -1901,7 +1910,7 @@ def list_(detailed=False, prefix='', p_path=None, ignore=False):
19011910 dict (name = ['-I' , '--ignore' ], action = 'store_true' , help = 'Ignore errors related to missing libraries.' ),
19021911 help = 'Show version control status\n \n ' ,
19031912 description = (
1904- "Show uncommitted changes in this %s and its dependencies." % cwd_type ))
1913+ "Show uncommitted changes a program or library and its dependencies." ))
19051914def status_ (ignore = False ):
19061915 repo = Repo .fromrepo ()
19071916 if repo .dirty ():
@@ -1918,7 +1927,7 @@ def status_(ignore=False):
19181927@subcommand ('compile' ,
19191928 dict (name = ['-t' , '--toolchain' ], help = 'Compile toolchain. Example: ARM, uARM, GCC_ARM, IAR' ),
19201929 dict (name = ['-m' , '--mcu' ], help = 'Compile target. Example: K64F, NUCLEO_F401RE, NRF51822...' ),
1921- dict (name = '--library' , dest = 'compile_library' , action = 'store_true' , help = 'Compile the current %s as a static library.' % cwd_type ),
1930+ dict (name = '--library' , dest = 'compile_library' , action = 'store_true' , help = 'Compile the current program or library as a static library.' ),
19221931 dict (name = '--config' , dest = 'compile_config' , action = 'store_true' , help = 'Show run-time compile configuration' ),
19231932 dict (name = '--prefix' , dest = 'config_prefix' , action = 'append' , help = 'Restrict listing to parameters that have this prefix' ),
19241933 dict (name = '--source' , action = 'append' , help = 'Source directory. Default: . (current dir)' ),
@@ -2229,39 +2238,50 @@ def toolchain_(name=None, global_cfg=False, supported=False):
22292238def help_ ():
22302239 return parser .print_help ()
22312240
2232- # Parse/run command
2233- if len (sys .argv ) <= 1 :
2234- help_ ()
2235- sys .exit (1 )
2236-
2237- if '--version' in sys .argv :
2238- print ver
2239- sys .exit (0 )
2240-
2241- pargs , remainder = parser .parse_known_args ()
2242- status = 1
2243-
2244- try :
2245- very_verbose = pargs .very_verbose
2246- verbose = very_verbose or pargs .verbose
2247- log ('Working path \" %s\" (%s)' % (os .getcwd (), cwd_type ))
2248- status = pargs .command (pargs )
2249- except ProcessException as e :
2250- error (
2251- "\" %s\" returned error code %d.\n "
2252- "Command \" %s\" in \" %s\" " % (e [1 ], e [0 ], e [2 ], e [3 ]), e [0 ])
2253- except OSError as e :
2254- if e [0 ] == errno .ENOENT :
2241+
2242+ def main ():
2243+ global verbose , very_verbose , remainder , cwd_root
2244+
2245+ # Help messages adapt based on current dir
2246+ cwd_root = os .getcwd ()
2247+
2248+ # Parse/run command
2249+ if len (sys .argv ) <= 1 :
2250+ help_ ()
2251+ sys .exit (1 )
2252+
2253+ if '--version' in sys .argv :
2254+ print ver
2255+ sys .exit (0 )
2256+
2257+ pargs , remainder = parser .parse_known_args ()
2258+ status = 1
2259+
2260+ try :
2261+ very_verbose = pargs .very_verbose
2262+ verbose = very_verbose or pargs .verbose
2263+ log ('Working path \" %s\" (%s)' % (os .getcwd (), Repo .pathtype (cwd_root )))
2264+ status = pargs .command (pargs )
2265+ except ProcessException as e :
22552266 error (
2256- "Could not detect one of the command-line tools.\n "
2257- "You could retry the last command with \" -v\" flag for verbose output\n " , e [0 ])
2258- else :
2259- error ('OS Error: %s' % e [1 ], e [0 ])
2260- except KeyboardInterrupt as e :
2261- log ('User aborted!' , - 1 )
2262- sys .exit (255 )
2263- except Exception as e :
2264- if very_verbose :
2265- traceback .print_exc (file = sys .stdout )
2266- error ("Unknown Error: %s" % e , 255 )
2267- sys .exit (status or 0 )
2267+ "\" %s\" returned error code %d.\n "
2268+ "Command \" %s\" in \" %s\" " % (e [1 ], e [0 ], e [2 ], e [3 ]), e [0 ])
2269+ except OSError as e :
2270+ if e [0 ] == errno .ENOENT :
2271+ error (
2272+ "Could not detect one of the command-line tools.\n "
2273+ "You could retry the last command with \" -v\" flag for verbose output\n " , e [0 ])
2274+ else :
2275+ error ('OS Error: %s' % e [1 ], e [0 ])
2276+ except KeyboardInterrupt as e :
2277+ log ('User aborted!' , - 1 )
2278+ sys .exit (255 )
2279+ except Exception as e :
2280+ if very_verbose :
2281+ traceback .print_exc (file = sys .stdout )
2282+ error ("Unknown Error: %s" % e , 255 )
2283+ sys .exit (status or 0 )
2284+
2285+
2286+ if __name__ == "__main__" :
2287+ main ()
0 commit comments