From ecb2952a7e70b64725657585a093abfb824e5841 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Thu, 7 May 2026 18:25:19 +0200 Subject: [PATCH] feat(commit): individual packages from config respected --- archmgr.py | 8 ++++---- commands/commit.py | 5 +++-- commands/config.py | 6 +++++- commands/pull.py | 5 ++++- commands/util/diff.py | 24 ++++++++++++++++++------ config.yml | 4 +++- config/__init__.py | 13 +++++++++++++ config/merger.py | 10 ++++++++++ config/validator.py | 10 +++++++++- 9 files changed, 69 insertions(+), 16 deletions(-) diff --git a/archmgr.py b/archmgr.py index 6274865..88795aa 100755 --- a/archmgr.py +++ b/archmgr.py @@ -30,18 +30,18 @@ if __name__ == "__main__": \\___/ """) - print(load_config("config.yml")) + conf = load_config("config.yml") try: if args.cmd == "commit": - commit.commit(args.force, args.no_render) + commit.commit(conf, args.force, args.no_render) elif args.cmd == "config": - config.config() + config.config(conf) elif args.cmd == "init": init.init(args.force) elif args.cmd == "setup": setup.setup() elif args.cmd == "pull": - pull.pull(args.rebase, args.apply) + pull.pull(conf, args.rebase, args.apply) elif args.cmd == "push": push.push(args.force) except KeyboardInterrupt as e: diff --git a/commands/commit.py b/commands/commit.py index e08d5d0..88f9c8f 100644 --- a/commands/commit.py +++ b/commands/commit.py @@ -3,9 +3,10 @@ from commands.util.diff import pkg_diff from commands.util.input_mgr import confirm, password from commands.util.printing.diff import print_diff +from config.dtype import ArchMgrConfig -def commit(force: bool = False, no_render: bool = False): +def commit(config: ArchMgrConfig, force: bool = False, no_render: bool = False): """Commit the changes to the system Args: @@ -15,7 +16,7 @@ def commit(force: bool = False, no_render: bool = False): """ # TODO: Make sure we don't uninstall critical system packages by accident (i.e. prompt user) # Probably do that check in the pacman util lib tho - add, remove = pkg_diff([], pacman.list_explicitly_installed()) + add, remove = pkg_diff(config["pkgs"]["individual"] or [], pacman.list_explicitly_installed()) print_diff(add, remove) if confirm(False, "Do you really want to proceed?"): pacman.install_package_list(add) diff --git a/commands/config.py b/commands/config.py index dcad489..4bc74b6 100644 --- a/commands/config.py +++ b/commands/config.py @@ -1,4 +1,8 @@ -def config(): +from config.dtype import ArchMgrConfig + + +def config(config: ArchMgrConfig): print(""" Your config can be found at """) + print(config) diff --git a/commands/pull.py b/commands/pull.py index 9ef9c79..f0e1689 100644 --- a/commands/pull.py +++ b/commands/pull.py @@ -1,2 +1,5 @@ -def pull(rebase: bool = False, apply: bool = False): +from config.dtype import ArchMgrConfig + + +def pull(config: ArchMgrConfig, rebase: bool = False, apply: bool = False): print("pull") diff --git a/commands/util/diff.py b/commands/util/diff.py index 9e34b32..c87b5bf 100644 --- a/commands/util/diff.py +++ b/commands/util/diff.py @@ -12,11 +12,23 @@ def pkg_diff(target: List[str], actual: List[str]) -> tuple[List[str], List[str] A tuple with first the missing (not installed) packages and second the extraneous (to be uninstalled) packages """ - for i, pkg in enumerate(actual): + removed = [] + diffed_out = [] + for pkg in actual: try: - idx = target.index(pkg) - target.pop(idx) - actual.pop(i) + diffed_out.append(target.index(pkg)) except Exception: - pass - return (target, actual) + removed.append(pkg) + + diffed_out.reverse() + new_pkgs = [] + if len(diffed_out) > 0: + curr = diffed_out.pop() + for i, pkg in enumerate(target): + if i != curr: + new_pkgs.append(pkg) + else: + if len(diffed_out) > 0: + curr = diffed_out.pop() + + return (new_pkgs, removed) diff --git a/config.yml b/config.yml index 6adf582..aaf2dec 100644 --- a/config.yml +++ b/config.yml @@ -6,7 +6,9 @@ requires: pkgs: individual: - - pkg_name + - acpi + - tk + - tldr bundles: - name: hyprland repos: diff --git a/config/__init__.py b/config/__init__.py index b6b5ed6..e25ed07 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -13,6 +13,11 @@ def _load_config_file(file: str): def default_config() -> ArchMgrConfig: + """Get the default configuration + + Returns: + The default config + """ return { "pkgs": { "individual": [], @@ -59,6 +64,14 @@ def default_config() -> ArchMgrConfig: def load_config(file: str) -> ArchMgrConfig: + """Load the configuration from the specified file path + + Args: + file: Path to the file to be loaded + + Returns: + The loaded, validated and parsed config + """ # Load and validate initial config try: loaded_conf = _load_config_file(file) diff --git a/config/merger.py b/config/merger.py index 9271768..859dd96 100644 --- a/config/merger.py +++ b/config/merger.py @@ -3,6 +3,16 @@ from config.dtype import ArchMgrConfig def merge_configs(config: ArchMgrConfig, new_config: ArchMgrConfig) -> ArchMgrConfig: + """Merge two configs, with the new_config taking precedence over the config + in the conflicting fields with arrays and dicts merged + + Args: + config: Base config + new_config: Config to merge into the base config + + Returns: + The merged config + """ if len(new_config) == 0 or len(config) == 0: return config diff --git a/config/validator.py b/config/validator.py index 7a4611f..128aea0 100644 --- a/config/validator.py +++ b/config/validator.py @@ -5,7 +5,15 @@ with open("config.schema.json") as file: schema = json.load(file) -def validate(config): +def validate(config: dict | list): + """Validate the specified config + + Args: + config: The raw configuration + + Returns: + True if the config is valid, False otherwise + """ try: jsonschema.validate(config, schema) except jsonschema.SchemaError: