Update 'shell.py'
This commit is contained in:
parent
8a8c8ec758
commit
da125b1033
235
shell.py
235
shell.py
|
@ -1,115 +1,120 @@
|
||||||
"""pclish: a simple shell written in Python"""
|
"""pclish: a simple shell written in Python"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import psutil
|
import psutil
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
# Get System Information
|
# Get System Information
|
||||||
RAM = psutil.virtual_memory()
|
RAM = psutil.virtual_memory()
|
||||||
CPU = platform.processor()
|
CPU = platform.processor()
|
||||||
|
|
||||||
# Command Interpretation -- This is the core of the shell
|
# Command Interpretation -- This is the core of the shell
|
||||||
def execute_command(command):
|
def execute_command(command):
|
||||||
"""execute commands and handle piping"""
|
"""execute commands and handle piping"""
|
||||||
try:
|
try:
|
||||||
if "|" in command:
|
if "|" in command:
|
||||||
s_in, s_out = (0, 0)
|
s_in, s_out = (0, 0)
|
||||||
s_in = os.dup(0)
|
s_in = os.dup(0)
|
||||||
s_out = os.dup(1)
|
s_out = os.dup(1)
|
||||||
|
|
||||||
fdin = os.dup(s_in)
|
fdin = os.dup(s_in)
|
||||||
|
|
||||||
for cmd in command.split("|"):
|
for cmd in command.split("|"):
|
||||||
os.dup2(fdin, 0)
|
os.dup2(fdin, 0)
|
||||||
os.close(fdin)
|
os.close(fdin)
|
||||||
|
|
||||||
if cmd == command.split("|")[-1]:
|
if cmd == command.split("|")[-1]:
|
||||||
fdout = os.dup(s_out)
|
fdout = os.dup(s_out)
|
||||||
else:
|
else:
|
||||||
fdin, fdout = os.pipe()
|
fdin, fdout = os.pipe()
|
||||||
|
|
||||||
os.dup2(fdout, 1)
|
os.dup2(fdout, 1)
|
||||||
os.close(fdout)
|
os.close(fdout)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(cmd.strip().split())
|
subprocess.run(cmd.strip().split())
|
||||||
except Exception:
|
except Exception:
|
||||||
print("pclish: command not found: {}".format(cmd.strip()))
|
print("pclish: command not found: {}".format(cmd.strip()))
|
||||||
|
|
||||||
os.dup2(s_in, 0)
|
os.dup2(s_in, 0)
|
||||||
os.dup2(s_out, 1)
|
os.dup2(s_out, 1)
|
||||||
os.close(s_in)
|
os.close(s_in)
|
||||||
os.close(s_out)
|
os.close(s_out)
|
||||||
else:
|
else:
|
||||||
subprocess.run(command.split(" "))
|
subprocess.run(command.split(" "))
|
||||||
except Exception:
|
except Exception:
|
||||||
print("pclish: command not found: {}".format(command))
|
print("pclish: command not found: {}".format(command))
|
||||||
|
|
||||||
|
|
||||||
# Commands - Commands are defined here.
|
# Commands - Commands are defined here.
|
||||||
def pclish_cd(path):
|
def pclish_cd(path):
|
||||||
"""convert to absolute path and change directory"""
|
"""convert to absolute path and change directory"""
|
||||||
try:
|
try:
|
||||||
os.chdir(os.path.abspath(path))
|
os.chdir(os.path.abspath(path))
|
||||||
except Exception:
|
except Exception:
|
||||||
print("cd: no such file or directory: {}".format(path))
|
print("cd: no such file or directory: {}".format(path))
|
||||||
|
|
||||||
|
def pclish_echo
|
||||||
def pclish_help():
|
txt = input("What would you like to echo?:\n")
|
||||||
print("""pclish: here are the commands available
|
echo(txt)
|
||||||
help: shows this page
|
|
||||||
cd: change directory
|
def pclish_help():
|
||||||
ver: displays shell version
|
print("""pclish: here are the commands available
|
||||||
ls: lists files in current dir""")
|
help: shows this page
|
||||||
|
cd: change directory
|
||||||
def pclish_ls():
|
ver: displays shell version
|
||||||
print("""pclish: this feature is not yet supported""")
|
ls: lists files in current dir""")
|
||||||
|
|
||||||
def pclish_ver():
|
def pclish_ls():
|
||||||
print("""pclish: version 0.0.1a.""")
|
print("""pclish: this feature is not yet supported""")
|
||||||
|
|
||||||
def pclish_mkdir():
|
def pclish_ver():
|
||||||
value = input("What would you like to name this new directory:\n")
|
print("""pclish: version 0.0.1a.""")
|
||||||
dir = value
|
|
||||||
os.mkdir(dir)
|
def pclish_mkdir():
|
||||||
|
value = input("What would you like to name this new directory:\n")
|
||||||
def pclish_system():
|
dir = value
|
||||||
print("""System: XCU Python Based Shell Env""")
|
os.mkdir(dir)
|
||||||
print("RAM:")
|
|
||||||
print(RAM)
|
def pclish_system():
|
||||||
print("CPU:")
|
print("""System: XCU Python Based Shell Env""")
|
||||||
print(CPU)
|
print("RAM:")
|
||||||
|
print(RAM)
|
||||||
|
print("CPU:")
|
||||||
def pclish_shtdwnsubsys():
|
print(CPU)
|
||||||
print("Shutting Down XCU Python Based Shell Env")
|
|
||||||
exit()
|
|
||||||
|
def pclish_shtdwnsubsys():
|
||||||
# Shell Prompt Customization and step 2 of command interpritation
|
print("Shutting Down XCU Python Based Shell Env")
|
||||||
def main():
|
exit()
|
||||||
while True:
|
|
||||||
inp = input("shell@system >> ")
|
# Shell Prompt Customization and step 2 of command interpritation
|
||||||
if inp == "exit":
|
def main():
|
||||||
break
|
while True:
|
||||||
elif inp[:3] == "cd ":
|
inp = input("shell@system >> ")
|
||||||
pclish_cd(inp[3:])
|
if inp == "exit":
|
||||||
elif inp == "help":
|
break
|
||||||
pclish_help()
|
elif inp[:3] == "cd ":
|
||||||
elif inp == "ver":
|
pclish_cd(inp[3:])
|
||||||
pclish_ver()
|
elif inp == "help":
|
||||||
elif inp == "ls":
|
pclish_help()
|
||||||
pclish_ls()
|
elif inp == "ver":
|
||||||
elif inp == "mkdir":
|
pclish_ver()
|
||||||
pclish_mkdir()
|
elif inp == "ls":
|
||||||
elif inp == "shtdwnsubsys":
|
pclish_ls()
|
||||||
pclish_shtdwnsubsys()
|
elif inp == "mkdir":
|
||||||
elif inp == "system":
|
pclish_mkdir()
|
||||||
pclish_system()
|
elif inp == "shtdwnsubsys":
|
||||||
else:
|
pclish_shtdwnsubsys()
|
||||||
execute_command(inp)
|
elif inp == "system":
|
||||||
|
pclish_system()
|
||||||
|
elif inp = "echo":
|
||||||
# Main
|
pclish_echo()
|
||||||
if '__main__' == __name__:
|
else:
|
||||||
main()
|
execute_command(inp)
|
||||||
|
|
||||||
|
|
||||||
|
# Main
|
||||||
|
if '__main__' == __name__:
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue