From 2c944fe209ad25eac007948e21cb9550fe4e4d0a Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Tue, 21 Jan 2020 23:58:34 +0100 Subject: [PATCH] Fix delete swap if swap doesnt exist --- modules/swap/module.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index f4834c4..53e421b 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -43,7 +43,8 @@ class SwapModule(AbstractModule): print("Swap is located here:", final_list[5]) self.swap_file = final_list[5] except IndexError: - print("Swap file doesn´t exist!\n") + print("Swap file doesn´t exist!") + return "None" def get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") @@ -116,13 +117,16 @@ class SwapModule(AbstractModule): print("Temporary swapiness is " + str(actions[selected_swapiness])) def delete_swap(self): - self.swap_location_check() - disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ - "sudo rm {}".format(self.swap_file) + location = self.swap_location_check() + if location == "None": + return None + else: + disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ + "sudo rm {}".format(self.swap_file) - subprocess.call(disable_swapfile, shell=True) - output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") - if not output_swapon: - print("Swap deleted!") - print(output_free.strip()) + subprocess.call(disable_swapfile, shell=True) + output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") + output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + if not output_swapon: + print("Swap deleted!") + print(output_free.strip())