@@ -199,7 +199,7 @@ def pquery(command, stdin=None, **kwargs):
199
199
stdout , _ = proc .communicate (stdin )
200
200
201
201
if very_verbose :
202
- print str (stdout ).strip ()
202
+ log ( str (stdout ).strip () )
203
203
204
204
if proc .returncode != 0 :
205
205
raise ProcessException (proc .returncode , command [0 ], ' ' .join (command ), os .getcwd ())
@@ -1149,6 +1149,9 @@ def get_cfg(self, *args, **kwargs):
1149
1149
def set_cfg (self , * args , ** kwargs ):
1150
1150
return Cfg (self .path ).set (* args , ** kwargs )
1151
1151
1152
+ def list_cfg (self , * args , ** kwargs ):
1153
+ return Cfg (self .path ).list (* args , ** kwargs )
1154
+
1152
1155
def set_root (self ):
1153
1156
return self .set_cfg ('ROOT' , '.' )
1154
1157
@@ -1308,7 +1311,7 @@ def get_macros(self):
1308
1311
macros = f .read ().splitlines ()
1309
1312
return macros
1310
1313
1311
- # Global class, used for global config
1314
+ # Global class used for global config
1312
1315
class Global (object ):
1313
1316
def __init__ (self ):
1314
1317
self .path = os .path .join (os .path .expanduser ("~" ), '.mbed' )
@@ -1324,7 +1327,10 @@ def get_cfg(self, *args, **kwargs):
1324
1327
def set_cfg (self , * args , ** kwargs ):
1325
1328
return Cfg (self .path ).set (* args , ** kwargs )
1326
1329
1330
+ def list_cfg (self , * args , ** kwargs ):
1331
+ return Cfg (self .path ).list (* args , ** kwargs )
1327
1332
1333
+ # Cfg classed used for handling the config backend
1328
1334
class Cfg (object ):
1329
1335
path = None
1330
1336
file = ".mbed"
@@ -1371,6 +1377,22 @@ def get(self, var, default_val=None):
1371
1377
return m .group (2 )
1372
1378
return default_val
1373
1379
1380
+ # Get all config var/values pairs
1381
+ def list (self ):
1382
+ fl = os .path .join (self .path , self .file )
1383
+ try :
1384
+ with open (fl ) as f :
1385
+ lines = f .read ().splitlines ()
1386
+ except (IOError , OSError ):
1387
+ lines = []
1388
+
1389
+ vars = {}
1390
+ for line in lines :
1391
+ m = re .match (r'^([\w+-]+)\=(.*)?$' , line )
1392
+ if m and m .group (1 ) and m .group (1 ) != 'ROOT' :
1393
+ vars [m .group (1 )] = m .group (2 )
1394
+ return vars
1395
+
1374
1396
1375
1397
def formaturl (url , format = "default" ):
1376
1398
url = "%s" % url
@@ -1407,6 +1429,7 @@ def formaturl(url, format="default"):
1407
1429
formatter_class = argparse .RawTextHelpFormatter )
1408
1430
subparsers = parser .add_subparsers (title = "Commands" , metavar = " " )
1409
1431
parser .add_argument ("--version" , action = "store_true" , dest = "version" , help = "print version number and exit" )
1432
+ subcommands = {}
1410
1433
1411
1434
# Process handling
1412
1435
def subcommand (name , * args , ** kwargs ):
@@ -1417,6 +1440,7 @@ def __subcommand(command):
1417
1440
kwargs ['formatter_class' ] = argparse .RawDescriptionHelpFormatter
1418
1441
1419
1442
subparser = subparsers .add_parser (name , ** kwargs )
1443
+ subcommands [name ] = subparser
1420
1444
1421
1445
for arg in args :
1422
1446
arg = dict (arg )
@@ -1899,7 +1923,7 @@ def sync(recursive=True, keep_refs=False, top=True):
1899
1923
"View the dependency tree of the current program or library." ))
1900
1924
def list_ (detailed = False , prefix = '' , p_path = None , ignore = False ):
1901
1925
repo = Repo .fromrepo ()
1902
- 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' )
1926
+ log ( 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' ) )
1903
1927
1904
1928
for i , lib in enumerate (sorted (repo .libs , key = lambda l : l .path )):
1905
1929
if prefix :
@@ -1923,7 +1947,7 @@ def status_(ignore=False):
1923
1947
repo = Repo .fromrepo ()
1924
1948
if repo .dirty ():
1925
1949
action ("Status for \" %s\" :" % repo .name )
1926
- print repo .status ()
1950
+ log ( repo .status () )
1927
1951
1928
1952
for lib in repo .libs :
1929
1953
if lib .check_repo (ignore ):
@@ -2178,45 +2202,72 @@ def detect():
2178
2202
2179
2203
# Generic config command
2180
2204
@subcommand ('config' ,
2181
- dict (name = 'var' , help = 'Variable name. E.g. "target", "toolchain", "protocol"' ),
2205
+ dict (name = 'var' , nargs = '?' , help = 'Variable name. E.g. "target", "toolchain", "protocol"' ),
2182
2206
dict (name = 'value' , nargs = '?' , help = 'Value. Will show the currently set default value for a variable if not specified.' ),
2183
2207
dict (name = ['-G' , '--global' ], dest = 'global_cfg' , action = 'store_true' , help = 'Use global settings, not local' ),
2184
2208
dict (name = ['-U' , '--unset' ], dest = 'unset' , action = 'store_true' , help = 'Unset the specified variable.' ),
2209
+ dict (name = ['-L' , '--list' ], dest = 'list_config' , action = 'store_true' , help = 'List mbed tool configuration. Not to be confused with compile configuration, e.g. "mbed compile --config".' ),
2185
2210
help = 'Tool configuration' ,
2186
2211
description = (
2187
2212
"Gets, sets or unsets mbed tool configuration options.\n "
2188
2213
"Options can be global (via the --global switch) or local (per program)\n "
2189
2214
"Global options are always overridden by local/program options.\n "
2190
2215
"Currently supported options: target, toolchain, protocol, depth, cache" ))
2191
- def config_ (var , value = None , global_cfg = False , unset = False ):
2216
+ def config_ (var = None , value = None , global_cfg = False , unset = False , list_config = False ):
2192
2217
name = var
2193
2218
var = str (var ).upper ()
2194
-
2195
- if global_cfg :
2196
- # Global configuration
2219
+
2220
+ if list_config :
2197
2221
g = Global ()
2198
- if unset :
2199
- g .set_cfg (var , None )
2200
- action ('Unset global %s' % name )
2201
- elif value :
2202
- g .set_cfg (var , value )
2203
- action ('%s now set as global %s' % (value , name ))
2222
+ g_vars = g .list_cfg ().items ()
2223
+ action ("Global config:" )
2224
+ if g_vars :
2225
+ for v in g_vars :
2226
+ log ("%s=%s\n " % (v [0 ], v [1 ]))
2204
2227
else :
2205
- value = g .get_cfg (var )
2206
- action (('%s' % value ) if value else 'No global %s set' % (name ))
2207
- else :
2208
- # Find the root of the program
2209
- program = Program (os .getcwd ())
2210
- with cd (program .path ):
2228
+ log ("No global configuration is set\n " )
2229
+ log ("\n " )
2230
+
2231
+ p = Program (os .getcwd ())
2232
+ action ("Local config (%s):" % p .path )
2233
+ if not p .is_cwd :
2234
+ p_vars = p .list_cfg ().items ()
2235
+ if p_vars :
2236
+ for v in p_vars :
2237
+ log ("%s=%s\n " % (v [0 ], v [1 ]))
2238
+ else :
2239
+ log ("No local configuration is set\n " )
2240
+ else :
2241
+ log ("Couldn't find valid mbed program in %s" % p .path )
2242
+
2243
+ elif name :
2244
+ if global_cfg :
2245
+ # Global configuration
2246
+ g = Global ()
2211
2247
if unset :
2212
- program .set_cfg (var , None )
2213
- action ('Unset default %s in program "%s" ' % ( name , program . name ) )
2248
+ g .set_cfg (var , None )
2249
+ action ('Unset global %s' % name )
2214
2250
elif value :
2215
- program .set_cfg (var , value )
2216
- action ('%s now set as default %s in program "%s" ' % (value , name , program . name ))
2251
+ g .set_cfg (var , value )
2252
+ action ('%s now set as global %s' % (value , name ))
2217
2253
else :
2218
- value = program .get_cfg (var )
2219
- action (('%s' % value ) if value else 'No default %s set in program "%s"' % (name , program .name ))
2254
+ value = g .get_cfg (var )
2255
+ action (('%s' % value ) if value else 'No global %s set' % (name ))
2256
+ else :
2257
+ # Find the root of the program
2258
+ program = Program (os .getcwd ())
2259
+ with cd (program .path ):
2260
+ if unset :
2261
+ program .set_cfg (var , None )
2262
+ action ('Unset default %s in program "%s"' % (name , program .name ))
2263
+ elif value :
2264
+ program .set_cfg (var , value )
2265
+ action ('%s now set as default %s in program "%s"' % (value , name , program .name ))
2266
+ else :
2267
+ value = program .get_cfg (var )
2268
+ action (('%s' % value ) if value else 'No default %s set in program "%s"' % (name , program .name ))
2269
+ else :
2270
+ subcommands ['config' ].error ("too few arguments" )
2220
2271
2221
2272
2222
2273
# Build system and exporters
@@ -2264,7 +2315,7 @@ def main():
2264
2315
sys .exit (1 )
2265
2316
2266
2317
if '--version' in sys .argv :
2267
- print ver
2318
+ log ( ver )
2268
2319
sys .exit (0 )
2269
2320
2270
2321
pargs , remainder = parser .parse_known_args ()
0 commit comments