From 2fea99c5de038ade440a847c4c7cb253179993aa Mon Sep 17 00:00:00 2001 From: Matthieu Muffato Date: Mon, 10 Oct 2022 04:13:23 +0100 Subject: [PATCH] Moved the view uninstallation to a separate function to mirror view_install --- shpc/client/view.py | 2 +- shpc/main/modules/base.py | 43 ++++++++++++++++++++++----------------- shpc/tests/test_views.py | 2 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/shpc/client/view.py b/shpc/client/view.py index eddaa600e..400796b83 100644 --- a/shpc/client/view.py +++ b/shpc/client/view.py @@ -175,4 +175,4 @@ def main(args, parser, extra, subparser): cli.view_install(view_name, module_name, force=args.force) if command == "uninstall": - cli.uninstall(module_name, view=view_name, force=args.force) + cli.view_uninstall(view_name, module_name, force=args.force) diff --git a/shpc/main/modules/base.py b/shpc/main/modules/base.py index 995515352..04f00facc 100644 --- a/shpc/main/modules/base.py +++ b/shpc/main/modules/base.py @@ -67,23 +67,34 @@ def modulefile(self): def templatefile(self): return "%s.%s" % (self.container.templatefile, self.module_extension) - def uninstall(self, name, view=None, force=False): + def view_uninstall(self, view, name, force=False): """ - Given a unique resource identifier, uninstall a module. If a view - name is provided, assume we only want to uninstall from the view + Uninstall a module from a view. """ module = self.new_module(name) - # We need to look for the module in all views and show to the user first - views_with_module = set() + # Ask before deleting anything! + if not force: + msg = name + "?" + if not utils.confirm_uninstall(msg, force): + return + + # Only uninstall from the view + if view not in self.views: + logger.exit("View %s does not exist, cannot uninstall." % view) + return self.views[view].uninstall(module.module_dir) - # Only populate if the command is not directed to a view - if not view: + def uninstall(self, name, force=False): + """ + Given a unique resource identifier, uninstall a module. + """ + module = self.new_module(name) - # If uninstalling the entire module, clean up symbolic links in all views - for view_name, entry in self.views.items(): - if entry.exists(module.module_dir): - views_with_module.add(view_name) + # We need to look for the module in all views and show to the user first + views_with_module = set() + for view_name, entry in self.views.items(): + if entry.exists(module.module_dir): + views_with_module.add(view_name) # Ask before deleting anything! if not force: @@ -96,12 +107,6 @@ def uninstall(self, name, view=None, force=False): if not utils.confirm_uninstall(msg, force): return - # Only uninstall from the view - if view: - if view not in self.views: - logger.exit("View %s does not exist, cannot uninstall." % view) - return self.views[view].uninstall(module.module_dir) - # Podman needs image deletion self.container.delete(module.name) @@ -124,8 +129,8 @@ def uninstall(self, name, view=None, force=False): ) # If uninstalling the entire module, clean up symbolic links in all views - for view_name, view in self.views.items(): - view.uninstall(module.module_dir) + for view_name in views_with_module: + self.views[view_name].uninstall(module.module_dir) # parent of versioned directory has module .version module_dir = os.path.dirname(module.module_dir) diff --git a/shpc/tests/test_views.py b/shpc/tests/test_views.py index cbce599c4..3c2302a4d 100644 --- a/shpc/tests/test_views.py +++ b/shpc/tests/test_views.py @@ -95,7 +95,7 @@ def test_views(tmp_path, module_sys, module_file, container_tech, remote): module_file = os.path.join(module_path, module_file[0]) assert os.path.islink(module_file) - client.uninstall("ghcr.io/autamus/emacs:27.2", view=view_name, force=True) + client.view_uninstall(view_name, "ghcr.io/autamus/emacs:27.2", force=True) # The view should be removed assert "emacs" not in os.listdir(os.path.join(view.path))