From e85050e796fe625a97a701ca4fc280a2d190be76 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Mon, 30 Jan 2023 14:21:31 +0100 Subject: ... --- tools/sops-inventory/sops_inventory/__main__.py | 34 ++++++++++++++----------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'tools') diff --git a/tools/sops-inventory/sops_inventory/__main__.py b/tools/sops-inventory/sops_inventory/__main__.py index 2ee1b91d..3f694917 100644 --- a/tools/sops-inventory/sops_inventory/__main__.py +++ b/tools/sops-inventory/sops_inventory/__main__.py @@ -9,6 +9,8 @@ import subprocess from operator import attrgetter, itemgetter +from itertools import chain + from yaml import load, YAMLError try: from yaml import CLoader as Loader @@ -46,16 +48,9 @@ class BooleanAction(argparse.Action): setattr(namespace, self.dest, False if option_string.startswith('--no') else True) -def main(): - parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument('--list-files', '--no-list-files', action=BooleanAction, default=False, help='Only list sops files') - parser.add_argument('path', metavar='PATH', nargs='?', type=Path, default=Path('.'), help='Base directory to take inventory of') - args = parser.parse_args() - - inventory = defaultdict(list) - - with subprocess.Popen(['git', '-C', args.path, 'ls-files', '-z'], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE) as proc: - files = sorted(map(lambda child: args.path / child.decode('utf-8').strip(), readnull(proc.stdout)), key=attrgetter('parts')) +def sops_files(path): + with subprocess.Popen(['git', '-C', path, 'ls-files', '-z'], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE) as proc: + files = sorted(map(lambda child: path / child.decode('utf-8').strip(), readnull(proc.stdout)), key=attrgetter('parts')) for child in files: try: with child.open(mode='r') as fh: @@ -82,7 +77,7 @@ def main(): key_info.add(r['recipient']) case _: raise NotImplementedError - inventory[frozenset(key_info)].append(child.relative_to(args.path)) + yield (frozenset(key_info), child.relative_to(path)) except (YAMLError, ValueError) as e: pass @@ -90,15 +85,24 @@ def main(): if proc.returncode != 0: raise RuntimeError(f'git ls-files returned with {proc.returncode}') +def main(): + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser.add_argument('--list-files', '--no-list-files', action=BooleanAction, default=False, help='Only list sops files') + parser.add_argument('path', metavar='PATH', nargs='?', type=Path, default=Path('.'), help='Base directory to take inventory of') + args = parser.parse_args() + if not args.list_files: - for keys, files in sorted(inventory.items(), key=itemgetter(0)): + inventory = defaultdict(list) + for key_info, path in sops_files(args.path): + inventory[key_info].append(path) + + for keys, files in sorted(inventory.items(), key=lambda kv: sorted(kv[0])): print(', '.join(sorted(keys)) + ':') for file in files: print(' - ' + str(file)) else: - for _, files in inventory.items(): - for file in files: - print(file) + for _, file in sorted(sops_files(args.path), key=lambda kv: kv[1].parts): + print(file) if __name__ == '__main__': os.exit(main()) -- cgit v1.2.3