From b90ca6223a7c835c66ddcf859fcd7e04fcd62927 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Thu, 11 Jan 2024 17:08:49 +0100 Subject: [PATCH 1/4] De-duplicated cfbs sorting rules Introduced here: https://github.com/cfengine/cfbs/pull/181 Signed-off-by: Ole Herman Schumacher Elgesem --- cfbs/commands.py | 40 +++++++++------------------------------- cfbs/pretty.py | 7 +++++++ 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/cfbs/commands.py b/cfbs/commands.py index 0ce74af5..522cced8 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -29,7 +29,12 @@ ) from cfbs.args import get_args -from cfbs.pretty import pretty, pretty_check_file, pretty_file +from cfbs.pretty import ( + pretty, + pretty_check_file, + pretty_file, + cfbs_default_sorting_rules, +) from cfbs.build import ( init_out_folder, perform_build_steps, @@ -95,34 +100,7 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int: if not filenames: user_error("Filenames missing for cfbs pretty command") - cfbs_sorting_rules = None - if not keep_order: - # These sorting rules achieve 3 things: - # 1. Top level keys are sorted according to a specified list - # 2. Module names in "index" and "provides" are sorted alphabetically - # 3. Fields inside module objects are sorted according to a specified list - # for "index", "provides", and "build" - - module_key_sorting = ( - MODULE_KEYS, - None, - ) - cfbs_sorting_rules = { - None: ( - TOP_LEVEL_KEYS, - { - "(index|provides)": ( - "alphabetic", # Module names are sorted alphabetically - {".*": module_key_sorting}, - ), - "build": ( # An array, not an object - None, # Don't sort elements of array - {".*": module_key_sorting}, - ), - }, - ), - } - + sorting_rules = cfbs_default_sorting_rules if keep_order else None num_files = 0 for f in filenames: if not f or not f.endswith(".json"): @@ -132,11 +110,11 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int: ) try: if check: - if not pretty_check_file(f, cfbs_sorting_rules): + if not pretty_check_file(f, sorting_rules): num_files += 1 print("Would reformat %s" % f) else: - pretty_file(f, cfbs_sorting_rules) + pretty_file(f, sorting_rules) except FileNotFoundError: user_error("File '%s' not found" % f) except json.decoder.JSONDecodeError as ex: diff --git a/cfbs/pretty.py b/cfbs/pretty.py index 8ac30a0e..d1d2e678 100644 --- a/cfbs/pretty.py +++ b/cfbs/pretty.py @@ -25,10 +25,17 @@ ) +# These sorting rules achieve 3 things: +# 1. Top level keys are sorted according to a specified list +# 2. Module names in "index" and "provides" are sorted alphabetically +# 3. Fields inside module objects are sorted according to a specified list +# for "index", "provides", and "build" + module_key_sorting = ( MODULE_KEYS, None, ) + cfbs_default_sorting_rules = { None: ( TOP_LEVEL_KEYS, From e01f33354125ccd04580346531d7baa1507c0cc6 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Thu, 11 Jan 2024 17:10:41 +0100 Subject: [PATCH 2/4] Renamed sorting rule variables One can be local with underscore prefix, the other can use all caps like the other globals. Signed-off-by: Ole Herman Schumacher Elgesem --- cfbs/cfbs_config.py | 4 ++-- cfbs/commands.py | 4 ++-- cfbs/pretty.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cfbs/cfbs_config.py b/cfbs/cfbs_config.py index cc7d9317..aa7b00b6 100644 --- a/cfbs/cfbs_config.py +++ b/cfbs/cfbs_config.py @@ -17,7 +17,7 @@ fetch_archive, SUPPORTED_ARCHIVES, ) -from cfbs.pretty import pretty, cfbs_default_sorting_rules +from cfbs.pretty import pretty, CFBS_DEFAULT_SORTING_RULES from cfbs.cfbs_json import CFBSJson from cfbs.module import Module from cfbs.prompts import prompt_user, YES_NO_CHOICES @@ -76,7 +76,7 @@ def __init__(self, filename="./cfbs.json", index=None, non_interactive=False): self.non_interactive = non_interactive def save(self): - data = pretty(self._data, cfbs_default_sorting_rules) + "\n" + data = pretty(self._data, CFBS_DEFAULT_SORTING_RULES) + "\n" with open(self.path, "w") as f: f.write(data) diff --git a/cfbs/commands.py b/cfbs/commands.py index 522cced8..9adea2cb 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -33,7 +33,7 @@ pretty, pretty_check_file, pretty_file, - cfbs_default_sorting_rules, + CFBS_DEFAULT_SORTING_RULES, ) from cfbs.build import ( init_out_folder, @@ -100,7 +100,7 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int: if not filenames: user_error("Filenames missing for cfbs pretty command") - sorting_rules = cfbs_default_sorting_rules if keep_order else None + sorting_rules = CFBS_DEFAULT_SORTING_RULES if keep_order else None num_files = 0 for f in filenames: if not f or not f.endswith(".json"): diff --git a/cfbs/pretty.py b/cfbs/pretty.py index d1d2e678..8a0b8152 100644 --- a/cfbs/pretty.py +++ b/cfbs/pretty.py @@ -31,22 +31,22 @@ # 3. Fields inside module objects are sorted according to a specified list # for "index", "provides", and "build" -module_key_sorting = ( +_module_key_sorting = ( MODULE_KEYS, None, ) -cfbs_default_sorting_rules = { +CFBS_DEFAULT_SORTING_RULES = { None: ( TOP_LEVEL_KEYS, { "(index|provides)": ( "alphabetic", # Module names are sorted alphabetically - {".*": module_key_sorting}, + {".*": _module_key_sorting}, ), "build": ( # An array, not an object None, # Don't sort elements of array - {".*": module_key_sorting}, + {".*": _module_key_sorting}, ), }, ), From e4f988be02290e6f33b16ba32d78540f1e79b980 Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Thu, 11 Jan 2024 17:20:44 +0100 Subject: [PATCH 3/4] Fixed pretty formatting of first cfbs init commit Signed-off-by: Ole Herman Schumacher Elgesem --- cfbs/commands.py | 19 ++++++++++++------- cfbs/pretty.py | 5 ++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cfbs/commands.py b/cfbs/commands.py index 9adea2cb..3d283f9e 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -141,12 +141,14 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int: default="Example description", ) - config = { - "name": name, - "type": "policy-set", # TODO: Prompt whether user wants to make a module - "description": description, - "build": [], - } + config = OrderedDict( + { + "name": name, + "type": "policy-set", # TODO: Prompt whether user wants to make a module + "description": description, + "build": [], + } + ) if index: config["index"] = index @@ -207,7 +209,10 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int: config["git"] = do_git - write_json(cfbs_filename(), config) + data = pretty(config, CFBS_DEFAULT_SORTING_RULES) + "\n" + print(data) + with open(cfbs_filename(), "w") as f: + f.write(data) assert is_cfbs_repo() if do_git: diff --git a/cfbs/pretty.py b/cfbs/pretty.py index 8a0b8152..a57db906 100644 --- a/cfbs/pretty.py +++ b/cfbs/pretty.py @@ -53,7 +53,7 @@ } -def _children_sort(child, name, sorting_rules): +def _children_sort(child: OrderedDict, name, sorting_rules): """Recursively sort child objects in a JSON object. :param child: child object to start with @@ -127,8 +127,7 @@ def _children_sort(child, name, sorting_rules): Only JSON objects (dictionaries) are sorted by this function, arrays are ignored. """ - if type(child) is not OrderedDict: - return + assert type(child) is OrderedDict for key in child: if type(child[key]) not in (list, tuple): From 586a32df814e9227bc60c68638b878a9ce493eab Mon Sep 17 00:00:00 2001 From: Ole Herman Schumacher Elgesem Date: Thu, 11 Jan 2024 20:06:33 +0100 Subject: [PATCH 4/4] Updated imports for top level keys and module keys Signed-off-by: Ole Herman Schumacher Elgesem --- cfbs/commands.py | 3 ++- cfbs/validate.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cfbs/commands.py b/cfbs/commands.py index 3d283f9e..34c2121e 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -34,12 +34,13 @@ pretty_check_file, pretty_file, CFBS_DEFAULT_SORTING_RULES, + TOP_LEVEL_KEYS, + MODULE_KEYS, ) from cfbs.build import ( init_out_folder, perform_build_steps, ) -from cfbs.cfbs_json import TOP_LEVEL_KEYS, MODULE_KEYS from cfbs.cfbs_config import CFBSConfig, CFBSReturnWithoutCommit from cfbs.validate import validate_config from cfbs.internal_file_management import ( diff --git a/cfbs/validate.py b/cfbs/validate.py index 0ce2f557..bde6a0b5 100644 --- a/cfbs/validate.py +++ b/cfbs/validate.py @@ -5,7 +5,7 @@ from collections import OrderedDict from cfbs.utils import is_a_commit_hash, user_error -from cfbs.cfbs_json import TOP_LEVEL_KEYS, MODULE_KEYS +from cfbs.pretty import TOP_LEVEL_KEYS, MODULE_KEYS from cfbs.cfbs_config import CFBSConfig from cfbs.build import AVAILABLE_BUILD_STEPS