forked from JaydenMW/pclish
135 lines
3.4 KiB
Python
135 lines
3.4 KiB
Python
# Import Modules
|
|
import os
|
|
import subprocess
|
|
import psutil
|
|
import platform
|
|
import socket
|
|
import getpass
|
|
|
|
# Get System Information
|
|
RAM = psutil.virtual_memory()
|
|
CPU = platform.processor()
|
|
OS = os_name = platform.system()
|
|
|
|
# Shell Version
|
|
VER = "PCLISH v0.0.3.8a"
|
|
|
|
# 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_echo():
|
|
txt = input("What would you like to echo?:\n")
|
|
print(txt)
|
|
|
|
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
|
|
system: shows system information
|
|
mkdir: creates a directory
|
|
shtdwnsubsys: shuts down the sub system
|
|
|
|
note: you have to type mkdir by its self then it will ask for what you want to name the directory""")
|
|
|
|
def pclish_ls():
|
|
print("pclish: this feature is not yet supported")
|
|
|
|
def pclish_ver():
|
|
print(VER)
|
|
|
|
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)
|
|
print("OS:")
|
|
print(OS)
|
|
print("Shell:")
|
|
print(VER)
|
|
|
|
|
|
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(f"{getpass.getuser()}@{platform.node()}>> ")
|
|
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()
|
|
elif inp == "echo":
|
|
pclish_echo()
|
|
else:
|
|
execute_command(inp)
|
|
|
|
|
|
# Main
|
|
if '__main__' == __name__:
|
|
main()
|