Skip to content

Commit

Permalink
builtins: use WestCommand.config
Browse files Browse the repository at this point in the history
Update built-in commands to use the recently-added config attribute.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
  • Loading branch information
mbolivar-nordic committed Mar 10, 2022
1 parent 0ece5e9 commit 9096a94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 52 deletions.
60 changes: 24 additions & 36 deletions src/west/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''\
Expand Down Expand Up @@ -154,58 +152,48 @@ def do_run(self, args, user_args):
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

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)
# local or global, whichever comes first
configfiles = [LOCAL, GLOBAL]

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:
log.dbg(f'{args.name} is unset')
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)

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]
self._perm_error(pe, what, args.name)

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}")
31 changes: 15 additions & 16 deletions src/west/app/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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
Expand Down Expand Up @@ -227,8 +227,9 @@ def local(self, args) -> Path:
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

Expand Down Expand Up @@ -306,9 +307,9 @@ def bootstrap(self, args) -> Path:
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

Expand Down Expand Up @@ -425,7 +426,7 @@ def delay(func, project):
# 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:
Expand Down Expand Up @@ -811,14 +812,12 @@ def init_state(self, args):
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] = []

Expand Down Expand Up @@ -975,7 +974,7 @@ def toplevel_projects(self):
' 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')
Expand Down

0 comments on commit 9096a94

Please sign in to comment.