35
35
36
36
37
37
# Application version
38
- ver = '0.8.5 '
38
+ ver = '0.8.7 '
39
39
40
40
# Default paths to Mercurial and Git
41
41
hg_cmd = 'hg'
97
97
regex_url_ref = r'^(.*/([\w.+-]+)(?:\.\w+)?)/?(?:#(.*))?$'
98
98
99
99
# git url (no #rev)
100
- regex_git_url = r'^(git@|git \://|ssh\://|https?\://) ([^/:]+)[:/](.+?)(\.git|\/?)$'
100
+ regex_git_url = r'^(git\://|ssh\://|https?\://|)(([^/:@]+)(\:([^/:@]+))?@)? ([^/:]+)[:/](.+?)(\.git|\/?)$'
101
101
# hg url (no #rev)
102
102
regex_hg_url = r'^(file|ssh|https?)://([^/:]+)/([^/]+)/?([^/]+?)?$'
103
103
119
119
verbose = False
120
120
very_verbose = False
121
121
122
+ # stores current working directory for recursive operations
123
+ cwd_root = ""
124
+
122
125
123
126
# Logging and output
124
127
def message (msg ):
@@ -1216,7 +1219,7 @@ def get_env(self):
1216
1219
def post_action (self ):
1217
1220
mbed_tools_path = self .get_tools_dir ()
1218
1221
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 ))) :
1220
1223
self .add_tools (os .path .join (self .path , '.temp' ))
1221
1224
mbed_tools_path = self .get_tools_dir ()
1222
1225
@@ -1305,7 +1308,10 @@ class Global(object):
1305
1308
def __init__ (self ):
1306
1309
self .path = os .path .join (os .path .expanduser ("~" ), '.mbed' )
1307
1310
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
1309
1315
1310
1316
def get_cfg (self , * args , ** kwargs ):
1311
1317
return Cfg (self .path ).get (* args , ** kwargs )
@@ -1327,7 +1333,7 @@ def set(self, var, val):
1327
1333
try :
1328
1334
with open (fl ) as f :
1329
1335
lines = f .read ().splitlines ()
1330
- except IOError :
1336
+ except ( IOError , OSError ) :
1331
1337
lines = []
1332
1338
1333
1339
for line in lines :
@@ -1338,16 +1344,20 @@ def set(self, var, val):
1338
1344
if not val is None :
1339
1345
lines += [var + "=" + val ]
1340
1346
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
1343
1353
1344
1354
# Gets config value
1345
1355
def get (self , var , default_val = None ):
1346
1356
fl = os .path .join (self .path , self .file )
1347
1357
try :
1348
1358
with open (fl ) as f :
1349
1359
lines = f .read ().splitlines ()
1350
- except IOError :
1360
+ except ( IOError , OSError ) :
1351
1361
lines = []
1352
1362
1353
1363
for line in lines :
@@ -1369,11 +1379,11 @@ def formaturl(url, format="default"):
1369
1379
m = re .match (regex_git_url , url )
1370
1380
if m :
1371
1381
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 ))
1373
1383
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 ))
1375
1385
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 ))
1377
1387
else :
1378
1388
m = re .match (regex_hg_url , url )
1379
1389
if m :
@@ -1386,11 +1396,6 @@ def formaturl(url, format="default"):
1386
1396
return url
1387
1397
1388
1398
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
-
1394
1399
# Subparser handling
1395
1400
parser = argparse .ArgumentParser (prog = 'mbed' ,
1396
1401
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
1522
1527
1523
1528
# Import command
1524
1529
@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.' ),
1527
1532
dict (name = ['-I' , '--ignore' ], action = 'store_true' , help = 'Ignore errors related to cloning and updating.' ),
1528
1533
dict (name = '--depth' , nargs = '?' , help = 'Number of revisions to fetch from the remote repository. Default: all revisions.' ),
1529
1534
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):
1664
1669
dict (name = ['-M' , '--message' ], dest = 'msg' , type = str , nargs = '?' , help = 'Commit message. Default: prompts for commit message.' ),
1665
1670
help = 'Publish program or library' ,
1666
1671
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 "
1668
1673
"This command performs various consistency checks for local uncommitted changes\n "
1669
1674
"and unpublished revisions and encourages to commit/push them.\n "
1670
1675
"Online guide about collaboration is available at:\n "
1671
- "www.mbed.com/collab_guide" % cwd_type ))
1676
+ "www.mbed.com/collab_guide" ))
1672
1677
def publish (all_refs = None , msg = None , top = True ):
1673
1678
if top :
1674
1679
action ("Checking for local modifications..." )
@@ -1716,13 +1721,16 @@ def publish(all_refs=None, msg=None, top=True):
1716
1721
dict (name = '--protocol' , nargs = '?' , help = 'Transport protocol for the source control management. Supported: https, http, ssh, git. Default: inferred from URL.' ),
1717
1722
help = 'Update to branch, tag, revision or latest' ,
1718
1723
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 "
1720
1725
"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." ))
1722
1727
def update (rev = None , clean = False , clean_files = False , clean_deps = False , ignore = False , top = True , depth = None , protocol = None ):
1723
1728
if top and clean :
1724
1729
sync ()
1725
1730
1731
+ cwd_type = Repo .pathtype (cwd_root )
1732
+ cwd_dest = "program" if cwd_type == "directory" else "library"
1733
+
1726
1734
repo = Repo .fromrepo ()
1727
1735
# A copy of repo containing the .lib layout before updating
1728
1736
repo_orig = Repo .fromrepo ()
@@ -1818,8 +1826,8 @@ def update(rev=None, clean=False, clean_files=False, clean_deps=False, ignore=Fa
1818
1826
help = 'Synchronize library references' ,
1819
1827
description = (
1820
1828
"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." ))
1823
1831
def sync (recursive = True , keep_refs = False , top = True ):
1824
1832
if top and recursive :
1825
1833
action ("Synchronizing dependency references..." )
@@ -1868,6 +1876,7 @@ def sync(recursive=True, keep_refs=False, top=True):
1868
1876
sync (keep_refs = keep_refs , top = False )
1869
1877
1870
1878
# Update the .lib reference in the parent repository
1879
+ cwd_type = Repo .pathtype (cwd_root )
1871
1880
if top and cwd_type == "library" :
1872
1881
repo = Repo .fromrepo ()
1873
1882
repo .write ()
@@ -1879,7 +1888,7 @@ def sync(recursive=True, keep_refs=False, top=True):
1879
1888
dict (name = ['-I' , '--ignore' ], action = 'store_true' , help = 'Ignore errors related to missing libraries.' ),
1880
1889
help = 'View dependency tree' ,
1881
1890
description = (
1882
- "View the dependency tree in this %s." % cwd_type ))
1891
+ "View the dependency tree of the current program or library." ))
1883
1892
def list_ (detailed = False , prefix = '' , p_path = None , ignore = False ):
1884
1893
repo = Repo .fromrepo ()
1885
1894
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):
1901
1910
dict (name = ['-I' , '--ignore' ], action = 'store_true' , help = 'Ignore errors related to missing libraries.' ),
1902
1911
help = 'Show version control status\n \n ' ,
1903
1912
description = (
1904
- "Show uncommitted changes in this %s and its dependencies." % cwd_type ))
1913
+ "Show uncommitted changes a program or library and its dependencies." ))
1905
1914
def status_ (ignore = False ):
1906
1915
repo = Repo .fromrepo ()
1907
1916
if repo .dirty ():
@@ -1918,7 +1927,7 @@ def status_(ignore=False):
1918
1927
@subcommand ('compile' ,
1919
1928
dict (name = ['-t' , '--toolchain' ], help = 'Compile toolchain. Example: ARM, uARM, GCC_ARM, IAR' ),
1920
1929
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.' ),
1922
1931
dict (name = '--config' , dest = 'compile_config' , action = 'store_true' , help = 'Show run-time compile configuration' ),
1923
1932
dict (name = '--prefix' , dest = 'config_prefix' , action = 'append' , help = 'Restrict listing to parameters that have this prefix' ),
1924
1933
dict (name = '--source' , action = 'append' , help = 'Source directory. Default: . (current dir)' ),
@@ -2229,39 +2238,50 @@ def toolchain_(name=None, global_cfg=False, supported=False):
2229
2238
def help_ ():
2230
2239
return parser .print_help ()
2231
2240
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 :
2255
2266
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