From 23771e22beddbf2ebdf663b3f278475cd2ce21ea Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Fri, 17 Jan 2020 19:21:48 +0100 Subject: [PATCH 01/21] Initial swap file --- modules/swap/module.py | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 modules/swap/module.py diff --git a/modules/swap/module.py b/modules/swap/module.py new file mode 100644 index 0000000..519dfe5 --- /dev/null +++ b/modules/swap/module.py @@ -0,0 +1,61 @@ +import subprocess +from PyInquirer import prompt +from AbstractModule import AbstractModule + + +class VimModule(AbstractModule): + def __init__(self): + super().__init__() + + +# Check default swap location +def convert_string(string): + li = list(string.split()) + return li + + +output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") +final_list = convert_string(output_swaps) +print(final_list[5]) +# End of location check + +output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") +output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + +option = input("Enter option. 1.Create/Resize 2.Delete: ") + +if option == "1": + if not output_swapon: + size = input("How big should the swap be(numbers in Gigabyte)? ") + create_swapfile = """sudo fallocate -l {}G /swapfile && + sudo chmod 600 /swapfile && + sudo mkswap /swapfile && + sudo swapon /swapfile""".format(size) + subprocess.call(create_swapfile, shell=True) + + output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + print(output_free.strip()) + + else: + print("") + print("Swap already installed! You can resize it!") + resize = input("How big should your swap become(numbers in Gigabyte)? ") + resize_swapfile = """sudo swapoff /swapfile && + sudo fallocate -l {}G /swapfile && + sudo mkswap /swapfile && + sudo swapon /swapfile""".format(resize) + subprocess.call(resize_swapfile, shell=True) + output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + print(output_free.strip()) +elif option == "2": + disable_swapfile = """sudo swapoff /swapfile && + sudo rm /swapfile""" + + 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()) +else: + print("Wrong input!") From fdd1c48bac5ec26464433384fd9758e6af37b7c1 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 18 Jan 2020 04:15:08 +0100 Subject: [PATCH 02/21] Re-implement SwapModule --- modules/swap/__init__.py | 5 ++ modules/swap/module.py | 107 ++++++++++++++++++++++----------------- 2 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 modules/swap/__init__.py diff --git a/modules/swap/__init__.py b/modules/swap/__init__.py new file mode 100644 index 0000000..9e23e02 --- /dev/null +++ b/modules/swap/__init__.py @@ -0,0 +1,5 @@ +from .module import SwapModule + + +def get_module(): + return SwapModule() \ No newline at end of file diff --git a/modules/swap/module.py b/modules/swap/module.py index 519dfe5..0b0dcee 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -1,61 +1,76 @@ import subprocess -from PyInquirer import prompt from AbstractModule import AbstractModule +from PyInquirer import prompt -class VimModule(AbstractModule): +class SwapModule(AbstractModule): + def __init__(self): super().__init__() + self.swap_file = None + def run(self): -# Check default swap location -def convert_string(string): - li = list(string.split()) - return li + actions = ["Create/Resize", "Delete"] + menu = [ + { + 'type': 'list', + 'message': 'Select action', + 'name': 'action', + 'choices': list(map(lambda x: {"name": x}, actions)) + } + ] + selected_action = prompt(menu)['action'] + if selected_action is "Create/Resize": + self.create_resize_swap() + elif selected_action is "Delete": + self.delete_swap() + # Check default swap location + def swap_location_check(self) -> None: + output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") + li = list(output_swaps.split()) + final_list = li + try: + print("Swap is located here:", final_list[5]) + self.swap_file = final_list[5] + except IndexError: + print("Swap file doesn´t exist!\n") + # End of location check -output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") -final_list = convert_string(output_swaps) -print(final_list[5]) -# End of location check + def create_resize_swap(self): + output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") + if not output_swapon: + size = input("How big should the swap be(numbers in Gigabyte)? ") + create_swapfile = """sudo fallocate -l {}G /swapfile && + sudo chmod 600 /swapfile && + sudo mkswap /swapfile && + sudo swapon /swapfile""".format(size) + subprocess.call(create_swapfile, shell=True) -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") + print(output_free.strip()) -option = input("Enter option. 1.Create/Resize 2.Delete: ") + else: + print("") + print("Swap already installed! You can resize it!") + resize = input("How big should your swap become(numbers in Gigabyte)? ") + resize_swapfile = """sudo swapoff /swapfile && + sudo fallocate -l {}G /swapfile && + sudo mkswap /swapfile && + sudo swapon /swapfile""".format(resize) + subprocess.call(resize_swapfile, shell=True) + output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + print(output_free.strip()) -if option == "1": - if not output_swapon: - size = input("How big should the swap be(numbers in Gigabyte)? ") - create_swapfile = """sudo fallocate -l {}G /swapfile && - sudo chmod 600 /swapfile && - sudo mkswap /swapfile && - sudo swapon /swapfile""".format(size) - subprocess.call(create_swapfile, shell=True) + def delete_swap(self): + self.swap_location_check() + disable_swapfile = """sudo swapoff {0} && + sudo rm {0}""".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") - print(output_free.strip()) - - else: - print("") - print("Swap already installed! You can resize it!") - resize = input("How big should your swap become(numbers in Gigabyte)? ") - resize_swapfile = """sudo swapoff /swapfile && - sudo fallocate -l {}G /swapfile && - sudo mkswap /swapfile && - sudo swapon /swapfile""".format(resize) - subprocess.call(resize_swapfile, shell=True) - output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") - print(output_free.strip()) -elif option == "2": - disable_swapfile = """sudo swapoff /swapfile && - sudo rm /swapfile""" - - 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()) -else: - print("Wrong input!") + if not output_swapon: + print("Swap deleted!") + print(output_free.strip()) From 526cfda6a87293b3fdf23ec7ace708d02634d8cd Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 18 Jan 2020 19:24:09 +0100 Subject: [PATCH 03/21] Add get swap size option, Add get swap location option Remove unnecessary comments Correct size output --- modules/swap/module.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 0b0dcee..20d9510 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -11,7 +11,7 @@ class SwapModule(AbstractModule): def run(self): - actions = ["Create/Resize", "Delete"] + actions = ["Create/Resize", "Delete", "Get location", "Get swap size"] menu = [ { 'type': 'list', @@ -25,8 +25,11 @@ class SwapModule(AbstractModule): self.create_resize_swap() elif selected_action is "Delete": self.delete_swap() + elif selected_action is "Get location": + self.swap_location_check() + elif selected_action is "Get swap size": + self.get_swap_size() - # Check default swap location def swap_location_check(self) -> None: output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") li = list(output_swaps.split()) @@ -36,7 +39,15 @@ class SwapModule(AbstractModule): self.swap_file = final_list[5] except IndexError: print("Swap file doesn´t exist!\n") - # End of location check + + def get_swap_size(self) -> None: + swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") + li = list(swap_size.split()) + final_list = li + try: + print("Swap is {}b big!".format(final_list[7])) + except IndexError: + print("Swap file doesn´t exist!") def create_resize_swap(self): output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") From aa48b95652529538498e5051ce092fa6b081f8a1 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sun, 19 Jan 2020 21:03:38 +0100 Subject: [PATCH 04/21] Replace 'is' with '==' so the interpreter stops annoying me --- modules/swap/module.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 20d9510..e810041 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -11,7 +11,7 @@ class SwapModule(AbstractModule): def run(self): - actions = ["Create/Resize", "Delete", "Get location", "Get swap size"] + actions = ["Create/Resize", "Delete", "Get swap location", "Get swap size"] menu = [ { 'type': 'list', @@ -21,13 +21,13 @@ class SwapModule(AbstractModule): } ] selected_action = prompt(menu)['action'] - if selected_action is "Create/Resize": + if selected_action == "Create/Resize": self.create_resize_swap() - elif selected_action is "Delete": + elif selected_action == "Delete": self.delete_swap() - elif selected_action is "Get location": + elif selected_action == "Get swap location": self.swap_location_check() - elif selected_action is "Get swap size": + elif selected_action == "Get swap size": self.get_swap_size() def swap_location_check(self) -> None: From 68b2a8074ca7d6ee95c2382608559a698fd1be15 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sun, 19 Jan 2020 21:52:29 +0100 Subject: [PATCH 05/21] Add functions to make swap persistent, add function to show swapiness --- modules/swap/module.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index e810041..264e540 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -11,7 +11,9 @@ class SwapModule(AbstractModule): def run(self): - actions = ["Create/Resize", "Delete", "Get swap location", "Get swap size"] + actions = ["Create/Resize temp. swap", "Create/Resize persistent swap", "Delete swap", + "Make temp. swap persistent", "Get swap location", "Get swap size", + "Show swapiness"] menu = [ { 'type': 'list', @@ -21,8 +23,15 @@ class SwapModule(AbstractModule): } ] selected_action = prompt(menu)['action'] - if selected_action == "Create/Resize": + if selected_action == "Create/Resize temp. swap": self.create_resize_swap() + elif selected_action == "Create/Resize persistent swap": + self.create_resize_swap() + self.make_swap_persistent() + elif selected_action == "Make temp. swap persistent": + self.make_swap_persistent() + elif selected_action == "Show swapiness": + self.show_swapiness() elif selected_action == "Delete": self.delete_swap() elif selected_action == "Get swap location": @@ -63,8 +72,12 @@ class SwapModule(AbstractModule): print(output_free.strip()) else: + swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") + li = list(swap_size.split()) + final_list = li print("") print("Swap already installed! You can resize it!") + print("Curr. swap size: {}b".format(final_list[7])) resize = input("How big should your swap become(numbers in Gigabyte)? ") resize_swapfile = """sudo swapoff /swapfile && sudo fallocate -l {}G /swapfile && @@ -74,6 +87,20 @@ class SwapModule(AbstractModule): output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") print(output_free.strip()) + def make_swap_persistent(self): + self.swap_location_check() + backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" + enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file) + + subprocess.call(backup_fstab, shell=True) + subprocess.call(enable_persistence, shell=True) + print("Swap is now persistent!") + + def show_swapiness(self): + get_swapiness = "cat /proc/sys/vm/swappiness" + + subprocess.call(get_swapiness, shell=True) + def delete_swap(self): self.swap_location_check() disable_swapfile = """sudo swapoff {0} && From 2d22f15f2205afac1563ad5efeb76ba69fc2b868 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Mon, 20 Jan 2020 14:58:24 +0100 Subject: [PATCH 06/21] Add function to adjust temp. swapiness --- modules/swap/module.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 264e540..005864f 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -13,7 +13,7 @@ class SwapModule(AbstractModule): actions = ["Create/Resize temp. swap", "Create/Resize persistent swap", "Delete swap", "Make temp. swap persistent", "Get swap location", "Get swap size", - "Show swapiness"] + "Show swapiness", "Adjust temp. swapiness"] menu = [ { 'type': 'list', @@ -38,6 +38,8 @@ class SwapModule(AbstractModule): self.swap_location_check() elif selected_action == "Get swap size": self.get_swap_size() + elif selected_action == "Adjust temp. swapiness": + self.adjust_swapiness_temp() def swap_location_check(self) -> None: output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") @@ -101,6 +103,31 @@ class SwapModule(AbstractModule): subprocess.call(get_swapiness, shell=True) + def adjust_swapiness_temp(self): + actions = ["Light", "Default", "Aggressive"] + menu = [ + { + 'type': 'list', + 'message': 'Select action', + 'name': 'action', + 'choices': list(map(lambda x: {"name": x}, actions)) + } + ] + selected_swapiness = prompt(menu)['action'] + + if selected_swapiness == "Light": + adjust = "sudo sysctl vm.swappiness=25" + subprocess.call(adjust, shell=True) + print("Temporary swapiness is 25") + elif selected_swapiness == "Default": + adjust = "sudo sysctl vm.swappiness=60" + subprocess.call(adjust, shell=True) + print("Temporary swapiness is 60") + elif selected_swapiness == "Aggressive": + adjust = "sudo sysctl vm.swappiness=100" + subprocess.call(adjust, shell=True) + print("Temporary swapiness is 100") + def delete_swap(self): self.swap_location_check() disable_swapfile = """sudo swapoff {0} && From d3e2c99198455b882bb0560d41bb3970460b3f2c Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Mon, 20 Jan 2020 22:33:11 +0100 Subject: [PATCH 07/21] Reformatting --- modules/swap/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swap/__init__.py b/modules/swap/__init__.py index 9e23e02..907d911 100644 --- a/modules/swap/__init__.py +++ b/modules/swap/__init__.py @@ -2,4 +2,4 @@ from .module import SwapModule def get_module(): - return SwapModule() \ No newline at end of file + return SwapModule() From e573118b386514ec7cb24d5d138f78251cccd5eb Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Mon, 20 Jan 2020 22:33:50 +0100 Subject: [PATCH 08/21] Remove if-"hell" in adjust swappiness --- modules/swap/module.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 005864f..c0f7ece 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -104,29 +104,24 @@ class SwapModule(AbstractModule): subprocess.call(get_swapiness, shell=True) def adjust_swapiness_temp(self): - actions = ["Light", "Default", "Aggressive"] + actions = { + "Light": 25, + "Default": 60, + "Aggressive": 100 + } menu = [ { 'type': 'list', 'message': 'Select action', 'name': 'action', - 'choices': list(map(lambda x: {"name": x}, actions)) + 'choices': list(map(lambda x: {"name": x}, actions.keys())) } ] selected_swapiness = prompt(menu)['action'] - if selected_swapiness == "Light": - adjust = "sudo sysctl vm.swappiness=25" - subprocess.call(adjust, shell=True) - print("Temporary swapiness is 25") - elif selected_swapiness == "Default": - adjust = "sudo sysctl vm.swappiness=60" - subprocess.call(adjust, shell=True) - print("Temporary swapiness is 60") - elif selected_swapiness == "Aggressive": - adjust = "sudo sysctl vm.swappiness=100" - subprocess.call(adjust, shell=True) - print("Temporary swapiness is 100") + adjust = "sudo sysctl vm.swappiness=" + str(actions[selected_swapiness]) + subprocess.call(adjust, shell=True) + print("Temporary swapiness is " + str(actions[selected_swapiness])) def delete_swap(self): self.swap_location_check() From 4177fe2a5f2860d79f37ce57d4840fbeeb8cb0a0 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Mon, 20 Jan 2020 22:42:01 +0100 Subject: [PATCH 09/21] Remove if-"hell" from run function --- modules/swap/module.py | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index c0f7ece..03682e4 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -11,35 +11,30 @@ class SwapModule(AbstractModule): def run(self): - actions = ["Create/Resize temp. swap", "Create/Resize persistent swap", "Delete swap", - "Make temp. swap persistent", "Get swap location", "Get swap size", - "Show swapiness", "Adjust temp. swapiness"] + actions = { + "Create/Resize temp. swap": self.create_resize_swap, + "Create/Resize persistent swap": self.create_resize_peristent_swap, + "Delete swap": self.delete_swap, + "Make temp. swap persistent": self.make_swap_persistent, + "Get swap location": self.swap_location_check, + "Get swap size": self.get_swap_size, + "Show swapiness": self.show_swapiness, + "Adjust temp. swapiness": self.adjust_swapiness_temp + } menu = [ { 'type': 'list', 'message': 'Select action', 'name': 'action', - 'choices': list(map(lambda x: {"name": x}, actions)) + 'choices': list(map(lambda x: {"name": x}, actions.keys())) } ] selected_action = prompt(menu)['action'] - if selected_action == "Create/Resize temp. swap": - self.create_resize_swap() - elif selected_action == "Create/Resize persistent swap": - self.create_resize_swap() - self.make_swap_persistent() - elif selected_action == "Make temp. swap persistent": - self.make_swap_persistent() - elif selected_action == "Show swapiness": - self.show_swapiness() - elif selected_action == "Delete": - self.delete_swap() - elif selected_action == "Get swap location": - self.swap_location_check() - elif selected_action == "Get swap size": - self.get_swap_size() - elif selected_action == "Adjust temp. swapiness": - self.adjust_swapiness_temp() + actions[selected_action]() + + def create_resize_peristent_swap(self): + self.create_resize_swap() + self.make_swap_persistent() def swap_location_check(self) -> None: output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") From dd031260574b5711f8ff423fbdd83d75fd5b7c3c Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Tue, 21 Jan 2020 23:26:57 +0100 Subject: [PATCH 10/21] Fixed merge criterias: delete unnecassary local variable, space between be and paranthesis, make return type consistent, fix multiline strings --- .gitignore | 1 + modules/swap/module.py | 35 ++++++++++++++++------------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 5335deb..61e041f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ .vscode devenv +env \ No newline at end of file diff --git a/modules/swap/module.py b/modules/swap/module.py index 03682e4..f4834c4 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -36,20 +36,18 @@ class SwapModule(AbstractModule): self.create_resize_swap() self.make_swap_persistent() - def swap_location_check(self) -> None: + def swap_location_check(self): output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") - li = list(output_swaps.split()) - final_list = li + final_list = list(output_swaps.split()) try: print("Swap is located here:", final_list[5]) self.swap_file = final_list[5] except IndexError: print("Swap file doesn´t exist!\n") - def get_swap_size(self) -> None: + def get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - li = list(swap_size.split()) - final_list = li + final_list = list(swap_size.split()) try: print("Swap is {}b big!".format(final_list[7])) except IndexError: @@ -58,11 +56,11 @@ class SwapModule(AbstractModule): def create_resize_swap(self): output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") if not output_swapon: - size = input("How big should the swap be(numbers in Gigabyte)? ") - create_swapfile = """sudo fallocate -l {}G /swapfile && - sudo chmod 600 /swapfile && - sudo mkswap /swapfile && - sudo swapon /swapfile""".format(size) + size = input("How big should the swap be (numbers in Gigabyte)? ") + create_swapfile = "sudo fallocate -l {}G /swapfile && ".format(size) + \ + "sudo chmod 600 /swapfile && " + \ + "sudo mkswap /swapfile && " + \ + "sudo swapon /swapfile" subprocess.call(create_swapfile, shell=True) output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") @@ -70,16 +68,15 @@ class SwapModule(AbstractModule): else: swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - li = list(swap_size.split()) - final_list = li + final_list = list(swap_size.split()) print("") print("Swap already installed! You can resize it!") print("Curr. swap size: {}b".format(final_list[7])) resize = input("How big should your swap become(numbers in Gigabyte)? ") - resize_swapfile = """sudo swapoff /swapfile && - sudo fallocate -l {}G /swapfile && - sudo mkswap /swapfile && - sudo swapon /swapfile""".format(resize) + resize_swapfile = "sudo swapoff /swapfile && " + \ + "sudo fallocate -l {}G /swapfile && ".format(resize) + \ + "sudo mkswap /swapfile && " + \ + "sudo swapon /swapfile" subprocess.call(resize_swapfile, shell=True) output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") print(output_free.strip()) @@ -120,8 +117,8 @@ class SwapModule(AbstractModule): def delete_swap(self): self.swap_location_check() - disable_swapfile = """sudo swapoff {0} && - sudo rm {0}""".format(self.swap_file) + 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") From 2c944fe209ad25eac007948e21cb9550fe4e4d0a Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Tue, 21 Jan 2020 23:58:34 +0100 Subject: [PATCH 11/21] 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()) From 82ec2336805997458071737b035453d7cb8e113b Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Wed, 22 Jan 2020 00:05:31 +0100 Subject: [PATCH 12/21] Removed env from .gitignore --- .gitignore | 3 +-- modules/swap/module.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 61e041f..e7e232e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ __pycache__ .vscode -devenv -env \ No newline at end of file +devenv \ No newline at end of file diff --git a/modules/swap/module.py b/modules/swap/module.py index 53e421b..e65c13c 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -44,7 +44,7 @@ class SwapModule(AbstractModule): self.swap_file = final_list[5] except IndexError: print("Swap file doesn´t exist!") - return "None" + return None def get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") @@ -118,7 +118,7 @@ class SwapModule(AbstractModule): def delete_swap(self): location = self.swap_location_check() - if location == "None": + if location is None: return None else: disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ From 5d519f238083751ef7777c1bdfe84cabf3735a22 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Thu, 23 Jan 2020 12:06:00 +0100 Subject: [PATCH 13/21] Make methods private, Fix error case when someone tries to delete swap even if it doesnt exist in the first place, Fix typo --- modules/swap/module.py | 44 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index e65c13c..adc6fb0 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -12,14 +12,14 @@ class SwapModule(AbstractModule): def run(self): actions = { - "Create/Resize temp. swap": self.create_resize_swap, - "Create/Resize persistent swap": self.create_resize_peristent_swap, - "Delete swap": self.delete_swap, - "Make temp. swap persistent": self.make_swap_persistent, - "Get swap location": self.swap_location_check, - "Get swap size": self.get_swap_size, - "Show swapiness": self.show_swapiness, - "Adjust temp. swapiness": self.adjust_swapiness_temp + "Create/Resize temp. swap": self._create_resize_swap, + "Create/Resize persistent swap": self._create_resize_persistent_swap, + "Delete swap": self._delete_swap, + "Make temp. swap persistent": self._make_swap_persistent, + "Get swap location": self._swap_location_check, + "Get swap size": self._get_swap_size, + "Show swapiness": self._show_swapiness, + "Adjust temp. swapiness": self._adjust_swapiness_temp } menu = [ { @@ -32,11 +32,11 @@ class SwapModule(AbstractModule): selected_action = prompt(menu)['action'] actions[selected_action]() - def create_resize_peristent_swap(self): - self.create_resize_swap() - self.make_swap_persistent() + def _create_resize_persistent_swap(self): + self._create_resize_swap() + self._make_swap_persistent() - def swap_location_check(self): + def _swap_location_check(self): output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") final_list = list(output_swaps.split()) try: @@ -44,9 +44,9 @@ class SwapModule(AbstractModule): self.swap_file = final_list[5] except IndexError: print("Swap file doesn´t exist!") - return None + return "NoSwap" - def get_swap_size(self): + def _get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") final_list = list(swap_size.split()) try: @@ -54,7 +54,7 @@ class SwapModule(AbstractModule): except IndexError: print("Swap file doesn´t exist!") - def create_resize_swap(self): + def _create_resize_swap(self): output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") if not output_swapon: size = input("How big should the swap be (numbers in Gigabyte)? ") @@ -82,8 +82,8 @@ class SwapModule(AbstractModule): output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") print(output_free.strip()) - def make_swap_persistent(self): - self.swap_location_check() + def _make_swap_persistent(self): + self._swap_location_check() backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file) @@ -91,12 +91,12 @@ class SwapModule(AbstractModule): subprocess.call(enable_persistence, shell=True) print("Swap is now persistent!") - def show_swapiness(self): + def _show_swapiness(self): get_swapiness = "cat /proc/sys/vm/swappiness" subprocess.call(get_swapiness, shell=True) - def adjust_swapiness_temp(self): + def _adjust_swapiness_temp(self): actions = { "Light": 25, "Default": 60, @@ -116,9 +116,9 @@ class SwapModule(AbstractModule): subprocess.call(adjust, shell=True) print("Temporary swapiness is " + str(actions[selected_swapiness])) - def delete_swap(self): - location = self.swap_location_check() - if location is None: + def _delete_swap(self): + location = self._swap_location_check() + if location == "NoSwap": return None else: disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ From d2c729f0ab30852d21dfdb8e188fd5ef729549d6 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 01:01:37 +0100 Subject: [PATCH 14/21] Pull out indice and rename variable --- modules/swap/module.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index adc6fb0..5a5f5b9 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -38,19 +38,18 @@ class SwapModule(AbstractModule): def _swap_location_check(self): output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") - final_list = list(output_swaps.split()) + swap_location = output_swaps.split()[5] try: - print("Swap is located here:", final_list[5]) - self.swap_file = final_list[5] + print("Swap is located here:", swap_location) + self.swap_file = swap_location except IndexError: print("Swap file doesn´t exist!") - return "NoSwap" def _get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - final_list = list(swap_size.split()) + swap_size = swap_size.split()[7] try: - print("Swap is {}b big!".format(final_list[7])) + print("Swap is {}b big!".format(swap_size)) except IndexError: print("Swap file doesn´t exist!") @@ -69,10 +68,10 @@ class SwapModule(AbstractModule): else: swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - final_list = list(swap_size.split()) + swap_size = swap_size.split()[7] print("") print("Swap already installed! You can resize it!") - print("Curr. swap size: {}b".format(final_list[7])) + print("Curr. swap size: {}b".format(swap_size)) resize = input("How big should your swap become(numbers in Gigabyte)? ") resize_swapfile = "sudo swapoff /swapfile && " + \ "sudo fallocate -l {}G /swapfile && ".format(resize) + \ From 153b8f6d049a2039ebf3a49285d8db839b5a636a Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 01:02:59 +0100 Subject: [PATCH 15/21] Rename to meaningful variable name --- modules/swap/module.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 5a5f5b9..01a0d15 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -96,7 +96,7 @@ class SwapModule(AbstractModule): subprocess.call(get_swapiness, shell=True) def _adjust_swapiness_temp(self): - actions = { + options = { "Light": 25, "Default": 60, "Aggressive": 100 @@ -106,14 +106,14 @@ class SwapModule(AbstractModule): 'type': 'list', 'message': 'Select action', 'name': 'action', - 'choices': list(map(lambda x: {"name": x}, actions.keys())) + 'choices': list(map(lambda x: {"name": x}, options.keys())) } ] selected_swapiness = prompt(menu)['action'] - adjust = "sudo sysctl vm.swappiness=" + str(actions[selected_swapiness]) + adjust = "sudo sysctl vm.swappiness=" + str(options[selected_swapiness]) subprocess.call(adjust, shell=True) - print("Temporary swapiness is " + str(actions[selected_swapiness])) + print("Temporary swapiness is " + str(options[selected_swapiness])) def _delete_swap(self): location = self._swap_location_check() From dcb5681aa320c8d3d9b39e89faed9333de4b3ab5 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 01:03:30 +0100 Subject: [PATCH 16/21] Fix return type, remove unnecessary space Add newline to .gitignore --- .gitignore | 2 +- modules/swap/module.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index e7e232e..5335deb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ __pycache__ .vscode -devenv \ No newline at end of file +devenv diff --git a/modules/swap/module.py b/modules/swap/module.py index 01a0d15..b99e58a 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -85,7 +85,6 @@ class SwapModule(AbstractModule): self._swap_location_check() backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file) - subprocess.call(backup_fstab, shell=True) subprocess.call(enable_persistence, shell=True) print("Swap is now persistent!") @@ -116,9 +115,9 @@ class SwapModule(AbstractModule): print("Temporary swapiness is " + str(options[selected_swapiness])) def _delete_swap(self): - location = self._swap_location_check() - if location == "NoSwap": - return None + self._swap_location_check() + if self.swap_file is None: + return else: disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ "sudo rm {}".format(self.swap_file) From 035e4ce6ae3546dc916abd45af5f120f58c7d6bc Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 02:10:12 +0100 Subject: [PATCH 17/21] Inverse if case for better readability Remove unnecessary line --- modules/swap/module.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index b99e58a..4d2d064 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -55,18 +55,7 @@ class SwapModule(AbstractModule): def _create_resize_swap(self): output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - if not output_swapon: - size = input("How big should the swap be (numbers in Gigabyte)? ") - create_swapfile = "sudo fallocate -l {}G /swapfile && ".format(size) + \ - "sudo chmod 600 /swapfile && " + \ - "sudo mkswap /swapfile && " + \ - "sudo swapon /swapfile" - subprocess.call(create_swapfile, shell=True) - - output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") - print(output_free.strip()) - - else: + if output_swapon: swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") swap_size = swap_size.split()[7] print("") @@ -81,6 +70,17 @@ class SwapModule(AbstractModule): output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") print(output_free.strip()) + else: + size = input("How big should the swap be (numbers in Gigabyte)? ") + create_swapfile = "sudo fallocate -l {}G /swapfile && ".format(size) + \ + "sudo chmod 600 /swapfile && " + \ + "sudo mkswap /swapfile && " + \ + "sudo swapon /swapfile" + subprocess.call(create_swapfile, shell=True) + + output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") + print(output_free.strip()) + def _make_swap_persistent(self): self._swap_location_check() backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" @@ -91,7 +91,6 @@ class SwapModule(AbstractModule): def _show_swapiness(self): get_swapiness = "cat /proc/sys/vm/swappiness" - subprocess.call(get_swapiness, shell=True) def _adjust_swapiness_temp(self): From c3e9bcc147a766e5cf49a028942547aa0478b8d2 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 02:12:10 +0100 Subject: [PATCH 18/21] Remove else because of early return --- modules/swap/module.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 4d2d064..9f7d370 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -117,13 +117,12 @@ class SwapModule(AbstractModule): self._swap_location_check() if self.swap_file is None: return - else: - disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ - "sudo rm {}".format(self.swap_file) + 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()) From de6b0917e81a8aa6c33bcbdaabff1ae3a2c5da9b Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 02:38:12 +0100 Subject: [PATCH 19/21] put index access in try block --- modules/swap/module.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 9f7d370..49d3ef7 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -38,8 +38,8 @@ class SwapModule(AbstractModule): def _swap_location_check(self): output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") - swap_location = output_swaps.split()[5] try: + swap_location = output_swaps.split()[5] print("Swap is located here:", swap_location) self.swap_file = swap_location except IndexError: @@ -47,8 +47,8 @@ class SwapModule(AbstractModule): def _get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") - swap_size = swap_size.split()[7] try: + swap_size = swap_size.split()[7] print("Swap is {}b big!".format(swap_size)) except IndexError: print("Swap file doesn´t exist!") From 59050e06655c285cb4ec821c80e45c87e4d69475 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 02:38:41 +0100 Subject: [PATCH 20/21] string formatting --- modules/swap/module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 49d3ef7..3ee717e 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -61,7 +61,7 @@ class SwapModule(AbstractModule): print("") print("Swap already installed! You can resize it!") print("Curr. swap size: {}b".format(swap_size)) - resize = input("How big should your swap become(numbers in Gigabyte)? ") + resize = input("How big should your swap become (numbers in Gigabyte)? ") resize_swapfile = "sudo swapoff /swapfile && " + \ "sudo fallocate -l {}G /swapfile && ".format(resize) + \ "sudo mkswap /swapfile && " + \ From 9636e817cb0ba0db3646846704e35b3a8d310a62 Mon Sep 17 00:00:00 2001 From: Lucas Noki Date: Sat, 25 Jan 2020 02:39:17 +0100 Subject: [PATCH 21/21] inline swap_file member as swap_location --- modules/swap/module.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/modules/swap/module.py b/modules/swap/module.py index 3ee717e..b03140d 100644 --- a/modules/swap/module.py +++ b/modules/swap/module.py @@ -7,16 +7,14 @@ class SwapModule(AbstractModule): def __init__(self): super().__init__() - self.swap_file = None def run(self): - actions = { "Create/Resize temp. swap": self._create_resize_swap, "Create/Resize persistent swap": self._create_resize_persistent_swap, "Delete swap": self._delete_swap, "Make temp. swap persistent": self._make_swap_persistent, - "Get swap location": self._swap_location_check, + "Get swap location": self._get_swap_location, "Get swap size": self._get_swap_size, "Show swapiness": self._show_swapiness, "Adjust temp. swapiness": self._adjust_swapiness_temp @@ -36,14 +34,15 @@ class SwapModule(AbstractModule): self._create_resize_swap() self._make_swap_persistent() - def _swap_location_check(self): + def _get_swap_location(self): output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") try: swap_location = output_swaps.split()[5] print("Swap is located here:", swap_location) - self.swap_file = swap_location + return swap_location except IndexError: print("Swap file doesn´t exist!") + return None def _get_swap_size(self): swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") @@ -82,9 +81,12 @@ class SwapModule(AbstractModule): print(output_free.strip()) def _make_swap_persistent(self): - self._swap_location_check() + swap_location = self._get_swap_location() + if swap_location is None: + print("Swap file doesn't exist!") + return backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" - enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file) + enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(swap_location) subprocess.call(backup_fstab, shell=True) subprocess.call(enable_persistence, shell=True) print("Swap is now persistent!") @@ -114,12 +116,11 @@ class SwapModule(AbstractModule): print("Temporary swapiness is " + str(options[selected_swapiness])) def _delete_swap(self): - self._swap_location_check() - if self.swap_file is None: + swap_location = self._get_swap_location() + if swap_location is None: return - disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ - "sudo rm {}".format(self.swap_file) - + disable_swapfile = "sudo swapoff {} && ".format(swap_location) + \ + "sudo rm {}".format(swap_location) 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")