From 708e12fa469c05ae74f2c7bf5c021529ab517573 Mon Sep 17 00:00:00 2001 From: Andrea Righi Date: Tue, 29 Nov 2022 09:01:13 +0100 Subject: [PATCH] UBUNTU: [Packaging] 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 --- debian/scripts/misc/annotations | 26 +++++++++++++++ debian/scripts/misc/kconfig/annotations.py | 38 ++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/debian/scripts/misc/annotations b/debian/scripts/misc/annotations index 1056bb55f822..5af9ebd0bdb6 100755 --- a/debian/scripts/misc/annotations +++ b/debian/scripts/misc/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/debian/scripts/misc/kconfig/annotations.py b/debian/scripts/misc/kconfig/annotations.py index 5d21ef8afa5e..fa218e7601fe 100644 --- a/debian/scripts/misc/kconfig/annotations.py +++ b/debian/scripts/misc/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 """