Skip to content

Commit 3a76212

Browse files
committed
Add support to list global and local configuration
via `mbed config -L/--list`
1 parent 0a80555 commit 3a76212

File tree

1 file changed

+79
-28
lines changed

1 file changed

+79
-28
lines changed

mbed/mbed.py

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def pquery(command, stdin=None, **kwargs):
199199
stdout, _ = proc.communicate(stdin)
200200

201201
if very_verbose:
202-
print str(stdout).strip()
202+
log(str(stdout).strip())
203203

204204
if proc.returncode != 0:
205205
raise ProcessException(proc.returncode, command[0], ' '.join(command), os.getcwd())
@@ -1149,6 +1149,9 @@ def get_cfg(self, *args, **kwargs):
11491149
def set_cfg(self, *args, **kwargs):
11501150
return Cfg(self.path).set(*args, **kwargs)
11511151

1152+
def list_cfg(self, *args, **kwargs):
1153+
return Cfg(self.path).list(*args, **kwargs)
1154+
11521155
def set_root(self):
11531156
return self.set_cfg('ROOT', '.')
11541157

@@ -1308,7 +1311,7 @@ def get_macros(self):
13081311
macros = f.read().splitlines()
13091312
return macros
13101313

1311-
# Global class, used for global config
1314+
# Global class used for global config
13121315
class Global(object):
13131316
def __init__(self):
13141317
self.path = os.path.join(os.path.expanduser("~"), '.mbed')
@@ -1324,7 +1327,10 @@ def get_cfg(self, *args, **kwargs):
13241327
def set_cfg(self, *args, **kwargs):
13251328
return Cfg(self.path).set(*args, **kwargs)
13261329

1330+
def list_cfg(self, *args, **kwargs):
1331+
return Cfg(self.path).list(*args, **kwargs)
13271332

1333+
# Cfg classed used for handling the config backend
13281334
class Cfg(object):
13291335
path = None
13301336
file = ".mbed"
@@ -1371,6 +1377,22 @@ def get(self, var, default_val=None):
13711377
return m.group(2)
13721378
return default_val
13731379

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+
13741396

13751397
def formaturl(url, format="default"):
13761398
url = "%s" % url
@@ -1407,6 +1429,7 @@ def formaturl(url, format="default"):
14071429
formatter_class=argparse.RawTextHelpFormatter)
14081430
subparsers = parser.add_subparsers(title="Commands", metavar=" ")
14091431
parser.add_argument("--version", action="store_true", dest="version", help="print version number and exit")
1432+
subcommands = {}
14101433

14111434
# Process handling
14121435
def subcommand(name, *args, **kwargs):
@@ -1417,6 +1440,7 @@ def __subcommand(command):
14171440
kwargs['formatter_class'] = argparse.RawDescriptionHelpFormatter
14181441

14191442
subparser = subparsers.add_parser(name, **kwargs)
1443+
subcommands[name] = subparser
14201444

14211445
for arg in args:
14221446
arg = dict(arg)
@@ -1899,7 +1923,7 @@ def sync(recursive=True, keep_refs=False, top=True):
18991923
"View the dependency tree of the current program or library."))
19001924
def list_(detailed=False, prefix='', p_path=None, ignore=False):
19011925
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'))
19031927

19041928
for i, lib in enumerate(sorted(repo.libs, key=lambda l: l.path)):
19051929
if prefix:
@@ -1923,7 +1947,7 @@ def status_(ignore=False):
19231947
repo = Repo.fromrepo()
19241948
if repo.dirty():
19251949
action("Status for \"%s\":" % repo.name)
1926-
print repo.status()
1950+
log(repo.status())
19271951

19281952
for lib in repo.libs:
19291953
if lib.check_repo(ignore):
@@ -2178,45 +2202,72 @@ def detect():
21782202

21792203
# Generic config command
21802204
@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"'),
21822206
dict(name='value', nargs='?', help='Value. Will show the currently set default value for a variable if not specified.'),
21832207
dict(name=['-G', '--global'], dest='global_cfg', action='store_true', help='Use global settings, not local'),
21842208
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".'),
21852210
help='Tool configuration',
21862211
description=(
21872212
"Gets, sets or unsets mbed tool configuration options.\n"
21882213
"Options can be global (via the --global switch) or local (per program)\n"
21892214
"Global options are always overridden by local/program options.\n"
21902215
"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):
21922217
name = var
21932218
var = str(var).upper()
2194-
2195-
if global_cfg:
2196-
# Global configuration
2219+
2220+
if list_config:
21972221
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]))
22042227
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()
22112247
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)
22142250
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))
22172253
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")
22202271

22212272

22222273
# Build system and exporters
@@ -2264,7 +2315,7 @@ def main():
22642315
sys.exit(1)
22652316

22662317
if '--version' in sys.argv:
2267-
print ver
2318+
log(ver)
22682319
sys.exit(0)
22692320

22702321
pargs, remainder = parser.parse_known_args()

0 commit comments

Comments
 (0)