builtins: use WestCommand.config

Update built-in commands to use the recently-added config attribute.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2022-03-03 16:49:53 -08:00 committed by Marti Bolivar
parent 2ac3e279ff
commit eb7325d679
2 changed files with 38 additions and 51 deletions

View File

@ -5,11 +5,9 @@
'''West config commands'''
import argparse
import configparser
from west import log
from west.configuration import read_config, update_config, delete_config, \
ConfigFile
from west.configuration import ConfigFile
from west.commands import WestCommand, CommandError
CONFIG_DESCRIPTION = '''\
@ -154,35 +152,33 @@ class Config(WestCommand):
self.write(args)
def list(self, args):
cfg = configparser.ConfigParser()
what = args.configfile or ALL
read_config(configfile=what, config=cfg)
for s in cfg.sections():
for k, v in cfg[s].items():
log.inf(f'{s}.{k}={v}')
for option, value in self.config.items(configfile=what):
log.inf(f'{option}={value}')
def delete(self, args):
section, key = self._sk(args)
if args.delete_all:
what = ALL
configfiles = [ALL]
elif args.configfile:
what = args.configfile
configfiles = [args.configfile]
else:
what = None # local or global, whichever comes first
# local or global, whichever comes first
configfiles = [LOCAL, GLOBAL]
try:
delete_config(section, key, configfile=what)
except KeyError:
log.dbg(f'{args.name} was not set in requested location(s)')
raise CommandError(returncode=1)
except PermissionError as pe:
self._perm_error(pe, what, section, key)
for i, configfile in enumerate(configfiles):
try:
self.config.delete(args.name, configfile=configfile)
return
except KeyError:
if i == len(configfiles) - 1:
log.dbg(
f'{args.name} was not set in requested location(s)')
raise CommandError(returncode=1)
except PermissionError as pe:
self._perm_error(pe, configfile, args.name)
def read(self, args):
section, key = self._sk(args)
cfg = configparser.ConfigParser()
read_config(configfile=args.configfile or ALL, config=cfg)
value = cfg.get(section, key, fallback=None)
value = self.config.get(args.name, configfile=args.configfile or ALL)
if value is not None:
log.inf(value)
else:
@ -190,22 +186,14 @@ class Config(WestCommand):
raise CommandError(returncode=1)
def write(self, args):
section, key = self._sk(args)
what = args.configfile or LOCAL
try:
update_config(section, key, args.value, configfile=what)
self.config.set(args.name, args.value, configfile=what)
except PermissionError as pe:
self._perm_error(pe, what, section, key)
self._perm_error(pe, what, args.name)
def _sk(self, args):
name_list = args.name.split(".", 1)
if len(name_list) != 2:
self.parser.error(f"name '{args.name}' should be in the form "
"<section>.<key>")
return name_list[0], name_list[1]
def _perm_error(self, pe, what, section, key):
def _perm_error(self, pe, what, name):
rootp = ('; are you root/administrator?' if what in [SYSTEM, ALL]
else '')
log.die(f"can't update {section}.{key}: "
log.die(f"can't update {name}: "
f"permission denied when writing {pe.filename}{rootp}")

View File

@ -19,7 +19,7 @@ import textwrap
from time import perf_counter
from urllib.parse import urlparse
from west.configuration import config, update_config
from west.configuration import Configuration
from west import log
from west import util
from west.commands import WestCommand, CommandError
@ -227,8 +227,9 @@ With neither, -m {MANIFEST_URL_DEFAULT} is assumed.
log.small_banner(f'Creating {west_dir} and local configuration file')
self.create(west_dir)
os.chdir(topdir)
update_config('manifest', 'path', os.fspath(rel_manifest))
update_config('manifest', 'file', manifest_filename, topdir=topdir)
self.config = Configuration(topdir=topdir)
self.config.set('manifest.path', os.fspath(rel_manifest))
self.config.set('manifest.file', manifest_filename)
return topdir
@ -306,9 +307,9 @@ With neither, -m {MANIFEST_URL_DEFAULT} is assumed.
except shutil.Error as e:
log.die(e)
log.small_banner('setting manifest.path to', manifest_path)
update_config('manifest', 'path', manifest_path, topdir=topdir)
update_config('manifest', 'file', temp_manifest_filename,
topdir=topdir)
self.config = Configuration(topdir=topdir)
self.config.set('manifest.path', manifest_path)
self.config.set('manifest.file', temp_manifest_filename)
return topdir
@ -425,7 +426,7 @@ The following arguments are available:
# Special-case the manifest repository while it's
# still showing up in the 'projects' list. Yet
# more evidence we should tackle #327.
path = config.get('manifest', 'path')
path = self.config.get('manifest.path')
apath = abspath(os.path.join(self.topdir, path))
ppath = Path(apath).as_posix()
else:
@ -811,14 +812,12 @@ class Update(_ProjectCommand):
if args.exclude_west:
log.wrn('ignoring --exclude-west')
self.narrow = args.narrow or config.getboolean('update', 'narrow',
fallback=False)
self.path_cache = args.path_cache or config.get('update', 'path-cache',
fallback=None)
self.name_cache = args.name_cache or config.get('update', 'name-cache',
fallback=None)
self.sync_submodules = config.getboolean('update', 'sync-submodules',
fallback=True)
config = self.config
self.narrow = args.narrow or config.getboolean('update.narrow')
self.path_cache = args.path_cache or config.get('update.path-cache')
self.name_cache = args.name_cache or config.get('update.name-cache')
self.sync_submodules = config.getboolean('update.sync-submodules',
default=True)
self.group_filter: List[str] = []
@ -975,7 +974,7 @@ class Update(_ProjectCommand):
' Only plain "west update" can currently update them.')
def fetch_strategy(self):
cfg = config.get('update', 'fetch', fallback=None)
cfg = self.config.get('update.fetch')
if cfg is not None and cfg not in ('always', 'smart'):
log.wrn(f'ignoring invalid config update.fetch={cfg}; '
'choices: always, smart')