From eb1a9c226d740bd3bcca2bfefef18a9575aeac56 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Tue, 3 Oct 2023 21:37:09 +0200 Subject: [PATCH] FEAT: remove empty tables from `pyproject.toml` (#185) * FIX: do not abort setup.cfg check if no `pyproject.toml` --- src/repoma/check_dev_files/setup_cfg.py | 28 +++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/repoma/check_dev_files/setup_cfg.py b/src/repoma/check_dev_files/setup_cfg.py index 5510f5b8..684a680e 100644 --- a/src/repoma/check_dev_files/setup_cfg.py +++ b/src/repoma/check_dev_files/setup_cfg.py @@ -37,13 +37,13 @@ def main(ignore_author: bool) -> None: if CONFIG_PATH.setup_cfg.exists(): _convert_to_pyproject() - if not CONFIG_PATH.pyproject.exists(): - return executor = Executor() executor(_check_required_options) if not ignore_author: executor(_update_author_data) executor(_fix_long_description) + if CONFIG_PATH.pyproject.exists(): + executor(_remove_empty_tables) executor.finalize() @@ -244,6 +244,30 @@ def __fix_long_description_in_setup_cfg() -> None: raise PrecommitError(msg) +def _remove_empty_tables() -> None: + if not CONFIG_PATH.pyproject.exists(): + return + pyproject = load_pyproject() + if __recursive_remove_empty_tables(pyproject): + write_pyproject(pyproject) + msg = f"Removed empty tables from {CONFIG_PATH.pyproject}" + raise PrecommitError(msg) + + +def __recursive_remove_empty_tables(table: Union[Container, Table]) -> bool: + updated = False + items = list(table.items()) + for key, value in items: + if not isinstance(value, Table): + continue + if len(value) == 0: + del table[key] + updated = True + else: + updated |= __recursive_remove_empty_tables(value) + return updated + + def has_pyproject_build_system() -> bool: if not CONFIG_PATH.pyproject.exists(): return False