From 18127c02c06e033114e3ef3fcc0a3cca77248c49 Mon Sep 17 00:00:00 2001 From: Andrea Righi Date: Tue, 29 Nov 2022 09:13:44 +0100 Subject: [PATCH] annotations: set and delete configs from command line Allow to set and delete config options from command line using the 'annotations' script (instead of manually editing the annotations file). This also provides an interface to manage the annotations file from other scripts. Signed-off-by: Andrea Righi --- annotations | 26 ++++++++++++++++++++++++++ kconfig/annotations.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/annotations b/annotations index 1056bb5..5af9ebd 100755 --- a/annotations +++ b/annotations @@ -31,8 +31,14 @@ def make_parser(): help='Select flavour (default is "generic")') parser.add_argument('--config', '-c', action='store', help='Select a specific config option') + parser.add_argument('--note', '-n', action='store', + help='Write a specific note to a config option in annotations (used with --write)') ga = parser.add_argument_group(title='Action').add_mutually_exclusive_group(required=True) + + ga.add_argument('--write', '-w', action='store', + metavar='VALUE', dest='value', + help='Set a specific config value in annotations (use \'null\' to remove)') ga.add_argument('--query', '-q', action='store_true', help='Query annotations') ga.add_argument('--export', '-e', action='store_true', @@ -60,6 +66,24 @@ def do_query(args): res = a.search_config(config=args.config, arch=args.arch, flavour=args.flavour) print(json.dumps(res, indent=4)) +def do_write(args): + if args.config is None: + arg_fail('error: --write requires --config') + + # Set the value in annotations ('null' means remove) + a = Annotation(args.file) + if args.value == 'null': + a.remove(args.config, arch=args.arch, flavour=args.flavour) + else: + a.set(args.config, arch=args.arch, flavour=args.flavour, value=args.value, note=args.note) + + # Save back to annotations + a.save(args.file) + + # Query and print back the value + res = a.search_config(config=args.config) + print(json.dumps(res, indent=4)) + def do_export(args): if args.arch is None: arg_fail('error: --export requires --arch') @@ -150,6 +174,8 @@ def main(): autodetect_annotations(args) if args.query: do_query(args) + elif args.value: + do_write(args) elif args.export: do_export(args) elif args.import_file: diff --git a/kconfig/annotations.py b/kconfig/annotations.py index 5d21ef8..fa218e7 100644 --- a/kconfig/annotations.py +++ b/kconfig/annotations.py @@ -110,6 +110,44 @@ class Annotation(Config): raise Exception(str(e) + f', line = {line}') return config + def _remove_entry(self, config : str): + if 'policy' in self.config[config]: + del self.config[config]['policy'] + if 'note' in self.config[config]: + del self.config[config]['note'] + if not self.config[config]: + del self.config[config] + + def remove(self, config : str, arch: str = None, flavour: str = None): + if config not in self.config: + return + if arch is not None: + if flavour is not None: + flavour = f'{arch}-{flavour}' + else: + flavour = arch + del self.config[config]['policy'][flavour] + if not self.config[config]['policy']: + self._remove_entry(config) + else: + self._remove_entry(config) + + def set(self, config : str, arch: str = None, flavour: str = None, + value : str = None, note : str = None): + if config not in self.config: + self.config[config] = { 'policy': {} } + if arch is not None: + if flavour is not None: + flavour = f'{arch}-{flavour}' + else: + flavour = arch + self.config[config]['policy'][flavour] = value + else: + for arch in self.arch: + self.config[config]['policy'][arch] = value + if note is not None: + self.config[config]['note'] = "'" + note.replace("'", '') + "'" + def update(self, c: KConfig, arch: str, flavour: str = None, configs: list = []): """ Merge configs from a Kconfig object into Annotation object """ -- 2.31.1