mash/main.py

89 lines
3.6 KiB
Python
Executable File

#!/usr/bin/python
import os
import socket
import subprocess
import shutil
from colorama import Fore
from os.path import expanduser
version = "0.1.0"
user = os.getlogin()
initDirectory = expanduser("~")
currentDirectory = initDirectory
argList = ["", ""]
if os.path.isfile(expanduser("~") + "/.config/mash/mash.conf") == True:
configFile = open(expanduser("~") + "/.config/mash/mash.conf")
else:
print("Config file does not exist. Copying default file to ~/.config/mash/mash.conf")
shutil.copyfile("/etc/mash/mash.conf", (expanduser("~") + "/.config/mash/mash.conf"))
print("Welcome to Mash, " + user + "@" + socket.gethostname() + "! Mash is currently on version " + version)
try:
while True:
os.chdir(currentDirectory)
command = input(Fore.GREEN + user + Fore.WHITE + "@" + socket.gethostname() + Fore.BLUE + " " + currentDirectory + Fore.WHITE + " > ")
argNumber = 0
argList.clear()
for letter in command:
if letter == " ":
argNumber = argNumber + 1
else:
try:
argList[argNumber] = argList[argNumber] + letter
except Exception:
argList.append(letter)
if argList[0] == "help":
print("This is Mash, Max's Shell. Mash has a few built in commands, which you can use to run commands, view files and interact with your system.")
print("Some commands you can run:")
print("")
print("pwd Print directory you are currently in")
print("cd Change your directory.")
print("mat View contents of a file. If you're on a Unix-like OS, use 'cat' instead.")
print("mls View all files in a directory. If you're on a Unix-like OS, use 'ls' instead.")
print("version Print current version of Mash.")
print("exit Exit Mash.")
print("")
print("You can also run commands installed by your system. See your OS's documentation for more information.")
elif argList[0] == "exit":
exit()
elif argList[0] == "version":
print(version)
elif argList[0] == "mat":
matFile = open(argList[1])
print(matFile.read())
elif argList[0] == "mls":
try:
if argList[1] == "":
print(os.listdir(currentDirectory))
else:
argList[1] = mlsDirectory
if mlsDirectory[0] == "/":
print(os.listdir(mlsDirectory))
else:
print(os.listdir(currentDirectory + "/" + mlsDirectory))
except Exception:
print(os.listdir(currentDirectory))
elif argList[0] == "cd":
try:
directoryForCD = argList[1]
if directoryForCD[0] == "~":
currentDirectory = expanduser("~") + directoryForCD.replace("~", "")
if directoryForCD[0] == "/":
currentDirectory = directoryForCD
else:
currentDirectory = (currentDirectory + "/" + directoryForCD)
except Exception:
print("Either a directory was not provided or an error occured.")
else:
try:
subprocess.run(argList)
except Exception:
print("Command", command, "either doesn't exist, isn't in the path or isn't in this directory.")
except Exception:
print("Looks like there was an error. Exiting...")
except KeyboardInterrupt:
print(" detected! Exiting...")