From 8a8c8ec758b5a03cfc1a47a92e74c53ba44942e0 Mon Sep 17 00:00:00 2001 From: JaydenMW Date: Sun, 14 Feb 2021 03:30:20 +0100 Subject: [PATCH] Upload files to '' --- shell.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 shell.py diff --git a/shell.py b/shell.py new file mode 100644 index 0000000..64491d8 --- /dev/null +++ b/shell.py @@ -0,0 +1,115 @@ +"""pclish: a simple shell written in Python""" + +import os +import subprocess +import psutil +import platform + +# Get System Information +RAM = psutil.virtual_memory() +CPU = platform.processor() + +# Command Interpretation -- This is the core of the shell +def execute_command(command): + """execute commands and handle piping""" + try: + if "|" in command: + s_in, s_out = (0, 0) + s_in = os.dup(0) + s_out = os.dup(1) + + fdin = os.dup(s_in) + + for cmd in command.split("|"): + os.dup2(fdin, 0) + os.close(fdin) + + if cmd == command.split("|")[-1]: + fdout = os.dup(s_out) + else: + fdin, fdout = os.pipe() + + os.dup2(fdout, 1) + os.close(fdout) + + try: + subprocess.run(cmd.strip().split()) + except Exception: + print("pclish: command not found: {}".format(cmd.strip())) + + os.dup2(s_in, 0) + os.dup2(s_out, 1) + os.close(s_in) + os.close(s_out) + else: + subprocess.run(command.split(" ")) + except Exception: + print("pclish: command not found: {}".format(command)) + + +# Commands - Commands are defined here. +def pclish_cd(path): + """convert to absolute path and change directory""" + try: + os.chdir(os.path.abspath(path)) + except Exception: + print("cd: no such file or directory: {}".format(path)) + + +def pclish_help(): + print("""pclish: here are the commands available + help: shows this page + cd: change directory + ver: displays shell version + ls: lists files in current dir""") + +def pclish_ls(): + print("""pclish: this feature is not yet supported""") + +def pclish_ver(): + print("""pclish: version 0.0.1a.""") + +def pclish_mkdir(): + value = input("What would you like to name this new directory:\n") + dir = value + os.mkdir(dir) + +def pclish_system(): + print("""System: XCU Python Based Shell Env""") + print("RAM:") + print(RAM) + print("CPU:") + print(CPU) + + +def pclish_shtdwnsubsys(): + print("Shutting Down XCU Python Based Shell Env") + exit() + +# Shell Prompt Customization and step 2 of command interpritation +def main(): + while True: + inp = input("shell@system >> ") + if inp == "exit": + break + elif inp[:3] == "cd ": + pclish_cd(inp[3:]) + elif inp == "help": + pclish_help() + elif inp == "ver": + pclish_ver() + elif inp == "ls": + pclish_ls() + elif inp == "mkdir": + pclish_mkdir() + elif inp == "shtdwnsubsys": + pclish_shtdwnsubsys() + elif inp == "system": + pclish_system() + else: + execute_command(inp) + + +# Main +if '__main__' == __name__: + main()