Fix delete swap if swap doesnt exist

This commit is contained in:
Lucas Noki 2020-01-21 23:58:34 +01:00
parent dd03126057
commit 2c944fe209

View File

@ -43,7 +43,8 @@ class SwapModule(AbstractModule):
print("Swap is located here:", final_list[5]) print("Swap is located here:", final_list[5])
self.swap_file = final_list[5] self.swap_file = final_list[5]
except IndexError: except IndexError:
print("Swap file doesn´t exist!\n") print("Swap file doesn´t exist!")
return "None"
def get_swap_size(self): def get_swap_size(self):
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") 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])) print("Temporary swapiness is " + str(actions[selected_swapiness]))
def delete_swap(self): def delete_swap(self):
self.swap_location_check() location = self.swap_location_check()
disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ if location == "None":
"sudo rm {}".format(self.swap_file) return None
else:
disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \
"sudo rm {}".format(self.swap_file)
subprocess.call(disable_swapfile, shell=True) subprocess.call(disable_swapfile, shell=True)
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
if not output_swapon: if not output_swapon:
print("Swap deleted!") print("Swap deleted!")
print(output_free.strip()) print(output_free.strip())