From: Andrea Righi Date: Mon, 14 Nov 2022 19:01:43 +0000 (+0100) Subject: annotations: try to compact annotations before saving it X-Git-Tag: v0.1~70 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=fb0efcca38646d3ebb772288738c57517366e66a;p=annotations.git annotations: try to compact annotations before saving it Signed-off-by: Andrea Righi --- diff --git a/annotations b/annotations index adb397d..138507d 100755 --- a/annotations +++ b/annotations @@ -111,7 +111,7 @@ def autodetect_annotations(args): with open('debian/debian.env', 'rt') as fd: args.file = fd.read().rstrip().split('=')[1] + '/config/annotations' except: - print('error: could not determine DEBDIR, try using: --file/-f') + arg_fail('error: could not determine DEBDIR, try using: --file/-f') def main(): args = _ARGPARSER.parse_args() diff --git a/kconfig/annotations.py b/kconfig/annotations.py index 221585e..c3eb457 100644 --- a/kconfig/annotations.py +++ b/kconfig/annotations.py @@ -16,8 +16,8 @@ class Config(object): Basic configuration file object """ self.fname = fname - self.raw_data = self._load(fname) - self.config = self._parse(self.raw_data) + raw_data = self._load(fname) + self.config = self._parse(raw_data) def _load(self, fname: str) -> str: with open(fname, 'rt') as fd: @@ -56,6 +56,12 @@ class Annotation(Config): self.header = '' for line in data.splitlines(): if re.match(r'^#.*', line): + m = re.match(r'^# ARCH: (.*)', line) + if m: + self.arch = list(m.group(1).split(' ')) + m = re.match(r'^# FLAVOUR: (.*)', line) + if m: + self.flavour = list(m.group(1).split(' ')) self.header += line + "\n" else: break @@ -132,8 +138,30 @@ class Annotation(Config): if arch in self.config[conf]['policy'] and conf not in c.config: self.config[conf]['policy'][flavour] = '-' + def _compact(self): + # Try to remove redundant settings: if the config value of a flavour is + # the same as the one of the main arch simply drop it. + for conf in self.config: + if 'policy' not in self.config[conf]: + continue + for flavour in self.flavour: + if flavour not in self.config[conf]['policy']: + continue + m = re.match(r'^(.*?)-(.*)$', flavour) + if not m: + continue + arch = m.group(1) + if arch not in self.config[conf]['policy']: + continue + if self.config[conf]['policy'][flavour] == self.config[conf]['policy'][arch]: + del self.config[conf]['policy'][flavour] + def save(self, fname: str): """ Save annotations data to the annotation file """ + # Compact annotations structure + self._compact() + + # Save annotations to disk with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as tmp: # Write header tmp.write(self.header + '\n')