pclish/shell.py

131 lines
3.1 KiB
Python
Raw Permalink Normal View History

2021-02-28 21:00:39 +00:00
#!/usr/bin/env python3
2021-03-02 18:09:36 +00:00
# PCLISH - Python Command Line Shell
2021-02-28 21:00:39 +00:00
2021-02-14 02:34:00 +00:00
import os
import subprocess
import psutil
import platform
2021-02-14 15:48:27 +00:00
import socket
2021-02-14 02:34:00 +00:00
RAM = psutil.virtual_memory()
CPU = platform.processor()
2021-02-23 15:03:11 +00:00
OS = platform.system()
HOST = socket.gethostname()
2021-02-23 15:23:10 +00:00
VER = "PCLISH v0.0.6a-hotfix1a"
2021-02-16 20:48:31 +00:00
PROMPT = "shell@{}$ ".format(HOST)
2021-02-14 02:34:00 +00:00
def execute_command(command):
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:
2021-02-23 15:03:11 +00:00
print("pclish: command not found, run the help command for a list of commands: {}".format(cmd.strip()))
2021-02-14 02:34:00 +00:00
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))
def pclish_cd(path):
try:
os.chdir(os.path.abspath(path))
except Exception:
print("cd: no such file or directory: {}".format(path))
2021-02-22 03:48:26 +00:00
def pclish_echo():
TXT = input("ARGS: ")
print(TXT)
2021-02-14 02:34:00 +00:00
2021-02-22 03:48:26 +00:00
def pclish_oscmd():
CMD = input("ARGS: ")
os.system(CMD)
2021-02-16 20:48:31 +00:00
2021-02-14 02:34:00 +00:00
def pclish_help():
2021-02-15 03:46:21 +00:00
print("""pclish: here are the commands available
2021-02-14 02:34:00 +00:00
help: shows this page
cd: change directory
ver: displays shell version
2021-02-15 03:46:21 +00:00
ls: lists files in current dir
system: shows system information
mkdir: creates a directory
shtdwnsubsys: shuts down the sub system
2021-02-20 21:19:53 +00:00
oscmd: allows you to run an OS level command""")
2021-02-14 02:34:00 +00:00
def pclish_ls():
2021-02-14 19:15:47 +00:00
print("pclish: this feature is not yet supported")
2021-02-14 02:34:00 +00:00
def pclish_ver():
2021-02-15 03:46:21 +00:00
print(VER)
2021-02-14 02:34:00 +00:00
2021-02-22 03:48:26 +00:00
def pclish_mkdir():
DIR = input("ARGS: ")
2021-02-20 21:22:37 +00:00
os.mkdir(DIR)
2021-02-14 02:34:00 +00:00
def pclish_system():
2021-02-23 15:03:11 +00:00
print("HOSTNAME:" + HOST)
print("System: PCLISH SUBSYSTEM")
print("RAM:" + RAM)
print("CPU:" + CPU)
print("OS:" + OS)
print("SHELL:" + VER)
2021-02-14 02:34:00 +00:00
def pclish_shtdwnsubsys():
2021-02-23 15:03:11 +00:00
print("Exiting to..." + OS)
2021-02-14 02:34:00 +00:00
exit()
def main():
while True:
2021-02-16 20:48:31 +00:00
inp = input(PROMPT)
2021-02-14 02:34:00 +00:00
if inp == "exit":
break
2021-02-23 15:23:10 +00:00
elif inp == "cd ":
2021-02-14 02:34:00 +00:00
pclish_cd(inp[3:])
elif inp == "help":
pclish_help()
elif inp == "ver":
pclish_ver()
2021-02-22 03:48:26 +00:00
elif inp == "ls":
pclish_ls()
2021-02-22 16:44:43 +00:00
elif inp == "mkdir":
2021-02-22 03:48:26 +00:00
pclish_mkdir()
2021-02-14 02:34:00 +00:00
elif inp == "shtdwnsubsys":
pclish_shtdwnsubsys()
elif inp == "system":
pclish_system()
2021-02-22 16:44:43 +00:00
elif inp == "echo":
2021-02-22 03:48:26 +00:00
pclish_echo()
2021-02-22 16:44:43 +00:00
elif inp == "oscmd":
2021-02-22 03:48:26 +00:00
pclish_oscmd()
2021-02-14 02:34:00 +00:00
else:
execute_command(inp)
2021-02-14 19:15:47 +00:00
# Main
2021-02-14 02:34:00 +00:00
if '__main__' == __name__:
main()