From 58ebd3bc0f00c532e97e9a5571471ffab87934ba Mon Sep 17 00:00:00 2001 From: AL-LCL Date: Fri, 19 May 2023 10:39:49 +0200 Subject: GOD-VIEW --- client/shell.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 client/shell.py (limited to 'client/shell.py') diff --git a/client/shell.py b/client/shell.py new file mode 100644 index 0000000..07b7450 --- /dev/null +++ b/client/shell.py @@ -0,0 +1,70 @@ +''' + Handles the running of commands in + the terminal. Timeouts, encoding & + returning the correct data. + + Verified: 2021 February 6 + * Follows PEP8 + * Tested Platforms + * Windows 10 + * Third Party Modules + * psutil +''' + +from client.state import ClientStatic +from client.error import ClientError +from shared.state import Static +from shared.error import Error +from shared.data import Data + +import subprocess +import threading +import psutil +import os + + +class Shell: + + @staticmethod + @ClientError.general + def run(data, disable_stderr=False): + if Static.WINDOWS: + data = f'chcp {ClientStatic.CODE_PAGE} > nul && {data}' + + process_timer = threading.Timer(ClientStatic.TIMEOUT, + Shell.__timeout) + process_timer.start() + + process = subprocess.run(data, shell=True, + encoding=Static.ENCODING, + errors=Static.ERRORS, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + if process_timer.is_alive(): + process_timer.cancel() + + if disable_stderr: + if process.returncode == 0: + return process.stdout.strip('\r\n') + else: + return ClientStatic.DEFAULT + else: + return Data.parse(''.join(( + process.stdout, + process.stderr + )).strip('\r\n'), raw=True) + else: + raise TimeoutError('Timeout Awaiting Shell Response') + + @staticmethod + @Error.quiet + def __timeout(): + # NOTE : Works well, only lists the + # cmd.exe & the subprocess, for example + # vim.exe. Does not disturb the other + # threads the client is running + for child in psutil.Process( + os.getpid()).children(True): + child.kill() -- cgit v1.2.3