From c4b5bbad491e846aa57acf8b58207be913589f54 Mon Sep 17 00:00:00 2001 From: Tomas Sladecek Date: Wed, 29 Mar 2023 13:02:55 +0200 Subject: [PATCH] by default, save/print all manifests; add option to save/print only patched ones; enforce json utf-8 encoding --- src/ksux/__init__.py | 25 +++++++++++++++++++++---- src/ksux/src/create_manifests.py | 8 ++++---- src/ksux/src/save_manifests.py | 2 +- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/ksux/__init__.py b/src/ksux/__init__.py index 3e1dd78..4fbb32e 100644 --- a/src/ksux/__init__.py +++ b/src/ksux/__init__.py @@ -18,6 +18,8 @@ def main(): help='Patches directory. Script will read only files with yaml/yml/json extensions') parser.add_argument('-e', '--output_extension', help='Extension of output files', choices=['yaml', 'json', 'yml'], default='json') + parser.add_argument('--patched_only', help='If all manifests should be saved, even those which were not patched', + action='store_true') parser.add_argument('--dry-run', help='Print manifests to stdout', action="store_true") parser.add_argument('-q', '--quiet', help='Do not print debug, info and warning messages', action='store_true') parser.add_argument('--version', help='Package version', action="store_true") @@ -33,19 +35,34 @@ def main(): else: logging.root.setLevel(logging.INFO) - manifests = patch_manifests(base_dir=args.base_dir, patches_dir=args.patches_dir) + final, all_manifests = patch_manifests(base_dir=args.base_dir, patches_dir=args.patches_dir) + + # Create final list of manifests + if not args.patched_only: + versions = all_manifests.keys() + final = [] + + for v in versions: + version = all_manifests[v] + kinds = version.keys() + for k in kinds: + kind = version[k] + resource_names = kind.keys() + + for r in resource_names: + final.append(kind[r]) if args.dry_run: if args.output_extension in ['yaml', 'yml']: print('---') - for i, m in enumerate(manifests): + for i, m in enumerate(final): round_trip_dump(m, sys.stdout) print('---') else: - print(json.dumps(manifests)) + print(json.dumps(final)) else: - save_manifests(patched_manifests=manifests, out_dir=args.out_dir, extension=args.output_extension) + save_manifests(patched_manifests=final, out_dir=args.out_dir, extension=args.output_extension) if args.output_extension in ['yaml', 'yml']: logging.warning( diff --git a/src/ksux/src/create_manifests.py b/src/ksux/src/create_manifests.py index 928e58f..a0241c5 100644 --- a/src/ksux/src/create_manifests.py +++ b/src/ksux/src/create_manifests.py @@ -1,6 +1,6 @@ import logging import os -from typing import Dict, List +from typing import Dict, List, Tuple from .patch import read_patches, patch_manifest from .read_file import read_yaml, read_json @@ -58,12 +58,12 @@ def read_manifests(base_dir: str) -> Dict: return manifests -def patch_manifests(base_dir: str, patches_dir: str) -> List[Dict]: +def patch_manifests(base_dir: str, patches_dir: str) -> Tuple[List[Dict], Dict]: """ Patch all manifests :param base_dir: directory with manifest templates :param patches_dir: dire - :return: + :return: list of patched manifests + all manifests """ manifests = read_manifests(base_dir=base_dir) patches = read_patches(patches_dir=patches_dir) @@ -72,4 +72,4 @@ def patch_manifests(base_dir: str, patches_dir: str) -> List[Dict]: for patch in patches: patched.append(patch_manifest(patch, manifests)) - return patched + return patched, manifests diff --git a/src/ksux/src/save_manifests.py b/src/ksux/src/save_manifests.py index df8251a..7c477b5 100644 --- a/src/ksux/src/save_manifests.py +++ b/src/ksux/src/save_manifests.py @@ -21,7 +21,7 @@ def save_manifests( with open(output_path, 'w') as f: if extension == 'json': - json.dump(p, f, indent=2) + json.dump(p, f, indent=2, ensure_ascii=False) elif extension == 'yaml' or extension == 'yml': round_trip_dump(p, f) else: